package com.naz.sdkdemo.helper;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.naz.sdkdemo.utils.SerialUtil;

import java.io.IOException;
import java.util.Objects;

import androidx.annotation.NonNull;
import tp.xmaihh.serialport.SerialHelper;
import tp.xmaihh.serialport.bean.ComBean;
import tp.xmaihh.serialport.utils.ByteUtil;

public class SerialPortManager {
    private SerialHelper serialHelper;
    private Context context;
    private Handler mHandler;
    private SerialCallBack mCallBack;

    //初始化SerialHelper对象，设定串口名称和波特率（此处为接收数据）
    public void init(Context context, String serialName, int port, SerialCallBack callBack){
        this.context = context;
        mCallBack = callBack;
        mHandler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                ComBean comBean = (ComBean) msg.obj;
                String rxText;
                rxText = ByteUtil.ByteArrToHex(comBean.bRec);
                mCallBack.dataCallBack(rxText);
            }
        };
        //-----(/dev/ttyACM0  ,  19200)
        if (serialHelper == null){
            serialHelper = new SerialHelper(serialName, port){
                @Override
                protected void onDataReceived(ComBean comBean) {
                    Message message = mHandler.obtainMessage();
                    message.obj = comBean;
                    Log.e("TAG", "onDataReceived: " + message.obj);
//            Toast.makeText(TestActivity.this, "onDataReceived: " + message.obj, Toast.LENGTH_SHORT).show();
                    mHandler.sendMessage(message);
                }
            };
            open();
        }
    }

    public void preValue(String hexStr){
        if (!serialHelper.isOpen()) {
            Toast.makeText(context, "串口未打开", Toast.LENGTH_SHORT).show();
            return;
        }
        serialHelper.send(SerialUtil.hexStringToByteArray(hexStr+SerialUtil.getCRC(Objects.requireNonNull(SerialUtil.hexStringToByteArray(hexStr)))));
    }
    //出标, number 1-12个工位
    public void outOfMark(int number){
        if (!serialHelper.isOpen()) {
            Toast.makeText(context, "串口未打开", Toast.LENGTH_SHORT).show();
            return;
        }
        String hexStr = "";
        switch (number){
            case 1:
                //0x01, 0x03, 0x00, 0x00, 0x00, 0x01, (byte) 0x84, (byte) 0x0A
                hexStr = "010600000001";
                break;
            case 2:
                hexStr = "010600070001";
                break;
            case 3:
                hexStr = "0106000E0001";
                break;
            case 4:
                hexStr = "010600150001";
                break;
            case 5:
                hexStr = "0106001C0001";
                break;
            case 6:
                hexStr = "010600230001";
                break;
            case 7:
                hexStr = "0106002A0001";
                break;
            case 8:
                hexStr = "010600310001";
                break;
            case 9:
                hexStr = "010600380001";
                break;
            case 10:
                hexStr = "0106003F0001";
                break;
            case 11:
                hexStr = "010600460001";
                break;
            case 12:
                hexStr = "0106004D0001";
                break;
        }
        serialHelper.send(SerialUtil.hexStringToByteArray(hexStr+SerialUtil.getCRC(Objects.requireNonNull(SerialUtil.hexStringToByteArray(hexStr)))));
    }

    //开门, number 1-3个门
    public void openDoor(int number, int type){//type 1设置，0查询
        if (!serialHelper.isOpen()) {
            Toast.makeText(context, "串口未打开", Toast.LENGTH_SHORT).show();
            return;
        }
        String hexStr = "";
        switch (number){
            case 1:
                //0x01, 0x03, 0x00, 0x00, 0x00, 0x01, (byte) 0x84, (byte) 0x0A
                if (type == 0){
                    hexStr = "010300000001";//40090~转16进制5A，待验证
                }else {
                    hexStr = "010600590001";//40090~转16进制5A，待验证
                }
                break;
            case 2:
                hexStr = "0106005A0001";
                break;
            case 3:
                hexStr = "0106005B0001";
                break;
        }
        serialHelper.send(SerialUtil.hexStringToByteArray(hexStr+SerialUtil.getCRC(Objects.requireNonNull(SerialUtil.hexStringToByteArray(hexStr)))));
    }

    //打开串口
    private void open(){
        if (serialHelper.isOpen()) {
            return;
        }
        try {
            serialHelper.open();
            if (mCallBack!=null){
                mCallBack.openCallBack(1);
            }
        } catch (Exception e) {
            if (mCallBack!=null){
                mCallBack.openCallBack(0);
            }
            e.printStackTrace();
        }
    }

    public void close(){
        if (serialHelper.isOpen()) {
            serialHelper.close();
        }
        if (mHandler!=null){
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        mCallBack = null;
        context = null;
    }

    public interface SerialCallBack{
        void dataCallBack(String data);
        void openCallBack(int code);//1成功，0失败
        void closeCallBack(int code);//1成功， 0失败
    }
}
