package com.example.upbot;

import static com.example.upbot.ClientActivity.handler;

import android.annotation.SuppressLint;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;

public class UPbotComm {
    @SuppressLint("StaticFieldLeak")
    private static volatile UPbotComm instance;
    private static Socket mSocket;
    private static boolean status = false;
    private static String ip;
    private static SocketConnectThread socketConnectThread = null;
    @SuppressLint("StaticFieldLeak")
    private UPbotComm() {
    }

    public SocketConnectThread  threadInit(){
         socketConnectThread = new SocketConnectThread();
        return socketConnectThread;
    }
    public static UPbotComm getInstance() {
        if (instance == null) {
            synchronized (UPbotComm.class) {
                if (instance == null) {
                    instance = new UPbotComm();
                }
            }
        }
        return instance;
    }
    class SocketConnectThread extends Thread{

        @Override
        public void run(){
            try {
                //指定ip地址和端口号
                Log.d("UPBOTCOMM", ip);
                mSocket = new Socket(ip, 19890);

                if(mSocket != null){
                    status = true;
                }

                //获取输出流、输入流
//                mOutStream = mSocket.getOutputStream();
//                mInStream = mSocket.getInputStream();
            } catch (Exception e) {
                e.printStackTrace();
            }
            startReader(mSocket);
        }

    }
    public boolean connect(String Uip){
        ip = Uip;
        if(socketConnectThread == null){
            threadInit();
        }
        socketConnectThread.start();
        return status;
    }
    /**
     * 发送消息
     * @param msg
     */
    public void sendMessage(final String msg) {
        if (msg.isEmpty() || !status){
            return;
        }
        new Thread() {
            @Override
            public void run() {
                try {
                    DataOutputStream writer = new DataOutputStream(mSocket.getOutputStream());
                    writer.writeUTF(msg); // 写一个UTF-8的信息
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    public void sendMessage(final byte[] msg) {
        if (msg == null || msg.length == 0 || !status) {
            return;
        }
        new Thread() {
            @Override
            public void run() {
                try {
                    DataOutputStream writer = new DataOutputStream(mSocket.getOutputStream());
                    writer.write(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public boolean getConnectStatus(){
        return status;
    }
    /**
     * 接收消息
     */
    private void startReader(final Socket socket) {
        new Thread(){
            @Override
            public void run() {
                DataInputStream reader;
                try {
                    // 获取读取流
                    reader = new DataInputStream(socket.getInputStream());
                    while (true) {
                        // 读取数据
                        String msg = reader.readUTF();
                        Message message = new Message();
                        message.what = 1;
                        message.obj=msg;
                        handler.sendMessage(message);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    protected void onDestroy() {
        if(mSocket!=null){
            try {
                mSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
