package cn.muchinfo.rma.netcore.socket;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import cn.muchinfo.rma.netcore.packet.Packet40;
import cn.muchinfo.rma.netcore.packet.Packet50;
import cn.muchinfo.rma.view.MyApplication;

public class SocketService extends Service {

    /** 连接 */
    public final static int CMD_CONNECT = 1;
    /** 断开 */
    public final static int CMD_CLOSE = 2;
    /** 发送数据 */
    public final static int CMD_SEND = 3;

    /** 命令类型 */
    public final static String KEY_CMD_TYPE_INT = "KEY_CMD_TYPE";
    /** 报文类型，0：4.0报文 1：5.0报文 */
    public final static String KEY_PACKET_TYPE_INT = "KEY_PACKET_TYPE";
    public final static String KEY_IP_STR = "KEY_IP";
    public final static String KEY_PORT_STR = "KEY_PORT";
    /** 报文 */
    public final static String KEY_PACKET = "KEY_PACKET";

    private MTP2Socket<Packet40> quoteSocket = null;
    private MTP2Socket<Packet50> tradeSocket = null;
    private MTP2SocketListener<Packet40> listener40 = null;
    private MTP2SocketListener<Packet50> listener50 = null;

    /**
     * 初始化
     * @param listener40 4.0报文回调委托
     * @param listener50 5.0报文回调委托
     */
    public SocketService(MTP2SocketListener<Packet40> listener40, MTP2SocketListener<Packet50> listener50) {
        this.listener40 = listener40;
        this.listener50 = listener50;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int cmdType = intent.getIntExtra(SocketService.KEY_CMD_TYPE_INT, 0);
        switch (cmdType) {
            case CMD_CONNECT:   // 连接
                this.connect(intent);
                break;
            case CMD_CLOSE:     // 断开连接
                this.close(intent);
                break;
            case CMD_SEND:      // 发送数据
                break;
            default:
                break;
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    // 连接服务端
    private void connect(Intent intent) {
        int packetType = intent.getIntExtra(SocketService.KEY_PACKET_TYPE_INT, -1);
        String IP = intent.getStringExtra(SocketService.KEY_IP_STR);
        String port = intent.getStringExtra(SocketService.KEY_PORT_STR);
        if (packetType == 0) {
            // 4.0
            try {
                if (null != this.quoteSocket) {
                    this.quoteSocket.close();
                    this.quoteSocket = null;
                }

                this.quoteSocket = new MTP2Socket<Packet40>(0, listener40);
//                this.quoteSocket.conn(IP, port);
            } catch (Exception e) {
                if (null != this.listener40)
                    this.listener40.onError(this.quoteSocket, new Error(e.getMessage()));
            }
        } else if (packetType == 1) {
            // 5.0
            try {
                if (null != this.tradeSocket) {
                    this.tradeSocket.close();
                    this.tradeSocket = null;
                }

                this.tradeSocket = new MTP2Socket<Packet50>(1, listener50);
//                this.tradeSocket.conn(IP, port);
            } catch (Exception e) {
                if (null != this.listener50)
                    this.listener50.onError(this.tradeSocket, new Error(e.getMessage()));
            }
        }
    }

    // 关闭连接的方法
    private void close(Intent intent) {
        int packetType = intent.getIntExtra(SocketService.KEY_PACKET_TYPE_INT, -1);
        if (packetType == 0) {
            this.quoteSocket.close();
            this.quoteSocket = null;
        } else {
            this.tradeSocket.close();
            this.tradeSocket = null;
        }
    }
}
