package com.doverfuelingsolutions.issp.driver.scan;


import com.doverfuelingsolutions.issp.driver.printer.PrintStatusListen;
import com.doverfuelingsolutions.issp.driver.serialPort.DataReceivedListener;
import com.doverfuelingsolutions.issp.driver.serialPort.SerialControl;
import com.doverfuelingsolutions.issp.driver.serialPort.SerialHelper;
import com.doverfuelingsolutions.issp.utils.ValidateUtil;
import com.doverfuelingsolutions.issp.utils.log.DFSLog;

public class ComScanKeyManager implements ScanManager, DataReceivedListener {
    private final static String TAG = "ComScanKeyManager";
    SerialControl serialControl;//串口
    ScanValueListener scanValueListener;

    public ComScanKeyManager(String port, int rate, ScanValueListener scanValueListener) throws Exception {
        serialControl = new SerialControl(port, rate, this, scanValueListener); //串口号和波特率
//         serialControl.setbLoopData(PrintCmd.GetStatus4());
        OpenComPort(serialControl);     //打开串口
    }


    @Override
    public void onDataReceived(byte[] data, ScanValueListener scanValueListener) {
        if (data == null || data.length <= 2) return;
//        try {
//            String s = new String(data, 0, data.length - 2, StandardCharsets.ISO_8859_1);//不知这里为何要减2，暂时先这样，后面研究下
//            if (s.length() != 18 || !RegexUtils.isNumeric(s)) {
//                BaseLogUtils.i(MyLogTag.SCAN_COM, "other");
//
//                return;//纯数字且18位才可能是收款码
//            }
//            if (null != scanValueListener) {
//                this.scanValueListener = scanValueListener;
//                stop();
//                BaseLogUtils.i(MyLogTag.SCAN_COM, Thread.currentThread().toString());
//                scanValueListener.onScanValue(s);
//            } else {
//                BaseLogUtils.i(MyLogTag.SCAN_COM, "scanValueListener is null");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            BaseLogUtils.i(MyLogTag.SCAN_COM, e.toString());
//        }
        try {
            boolean isGo = data[data.length - 1] == 10 && data[data.length - 2] == 13;//旧的扫描器判断
            String code = null;
            if (isGo) {
                code = new String(data, 0, data.length - 2);//旧的扫描器
            }
            if (data[data.length - 1] == 13) {
                code = new String(data, 0, data.length - 1);//新的扫描器
            }
            if (code == null) return;
            if (null != scanValueListener) {
                this.scanValueListener = scanValueListener;
                stop();

                if (code.length() != 18 || !ValidateUtil.Companion.isPositiveInt(code)) {
                    scanValueListener.onScanValue(null);//非正确付款码时
                    return;//纯数字且18位才可能是收款码
                }
                scanValueListener.onScanValue(code);
            } else {
                DFSLog.Companion.e("scanValueListener is null");
            }
        } catch (Exception e) {
            DFSLog.Companion.e(e);
        }


    }


    private void OpenComPort(SerialHelper ComPort) throws Exception {
        try {
            ComPort.open();
        } catch (Exception e) {
            DFSLog.Companion.e(e);
            throw e;
        }
    }


    public void start() {
        if (serialControl == null) {
            DFSLog.Companion.e("ComScanKeyManager.start", "serialControl is null");
            return;
        }
        try {
            serialControl.sendHex("07C60408008800FE9F");
            serialControl.sendHex("04E40400FF14");
        } catch (Exception e) {
            DFSLog.Companion.e(e);
        }
    }

    public void stop() {
        if (serialControl == null) {
            DFSLog.Companion.e("ComScanKeyManager.start", "serialControl is null");
            return;
        }
        try {
            serialControl.sendHex("04E50400FF13");
        } catch (Exception e) {
            DFSLog.Companion.e(e);
        }
    }

    /**
     * 摧毁界面后
     */
    public void destroy() {
        if (null != scanValueListener) scanValueListener = null;
        if (null != serialControl) {
            serialControl.close();
            serialControl = null;
        }
    }

    @Override
    public void onDataReceived(byte[] data, PrintStatusListen printStatusListen) {

    }

    @Override
    public void onDataReceived(byte[] data) {

    }

}
