package net.micode.notes.gtask.remote;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

//需要架包：commons-io.jar

//练习Runnable，实现多线程同步下载图片
public class Network implements Runnable { //继承Thread：extends Thread


    private static final String HOST = "192.168.43.231";
    private static final int PORT = 4000;

    private int type; //网络图片地址
    private String parameter1; //保存的文件名
    private String parameter2;

    private InputStream input;
    private OutputStream output; //确定输出流
    String inTemp = null;

    //构造器
    public Network(int type, String parameter1, String parameter2) {
        this.type = type;
        this.parameter1 = parameter1;
        this.parameter2 = parameter2;
    }
    public String returnname(){
        return parameter1;
    }
    public String getInTemp() {
        return inTemp;
    }

    @Override
    public void run() {
        try {

            SocketAddress endpoint = new InetSocketAddress(HOST, PORT);
            Socket socket = new Socket();

            try {
                socket.connect(endpoint, 2000);

            } catch (IOException e) {
                e.printStackTrace();
                inTemp = "timeout";
                return;
            }


            BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            output = socket.getOutputStream();//输出流
            JSONObject object = new JSONObject();
            PrintWriter pw = new PrintWriter(output);//初始化输出数据
            String objStr = "";

            switch (type){
                case 1:
                case 2:
                case 3:
                    object = Json(type,parameter1,parameter2);
                    objStr = object.toString();

                    pw.println(objStr);
                    pw.flush();//刷新

                    inTemp = socketIn.readLine();
                    System.out.println(inTemp);
                    pw.println("bye");
                    pw.flush();//刷新
                    break;


                default:
                    throw new IllegalStateException("Unexpected value: " + type);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public JSONObject Json(int type,String parameter1, String parameter2) throws JSONException {
        JSONObject object = new JSONObject();
        //传入操作类型
        object.put("type",type);
        //传入参数1
        object.put("parameter1",parameter1);
        //传入参数2
        object.put("parameter2",parameter2);

        String objStr = object.toString();

        //array
//        List<Integer> integers = Arrays.asList(1,2,3);
//        object.put("list",integers);
//        //null
//        object.put("null",null);
        System.out.println(objStr);
        return object;
    }

}

