package com.hboxs.serialport.sbc;

import android.util.Log;


import com.hboxs.serialport.plc.util.AsciiUtils;
import com.hboxs.serialport.plc.util.HexUtils;
import com.hboxs.serialport.plc.util.HexadecimalUtil;
import com.hboxs.serialport.sbc.frame.VboxCommand;
import com.hboxs.serialport.sbc.frame.VboxCommandFrame;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

import android_serialport_api.SerialPort;


public final class VboxSerialPortManager {
    private static final String TAG = "SerialPortManager";
    /**
     * 串口设备
     */
    private SerialPort mSerialPort;

    /**
     * 输出流
     */
    private OutputStream mOutputStream;
    private VboxSerialPortReadThread mReadThread;


    /**
     * 不允许生成对象
     */
    private VboxSerialPortManager() {
    }

    public void sendCommandFrame(VboxCommandFrame frame) {
        try {
            Log.d(TAG, "sendCommandFrame: " + frame);
            sendData(frame.getData());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 静态内部类控制单例
     */
    private static class SerialPortManagerHolder {
        final static VboxSerialPortManager instance = new VboxSerialPortManager();
    }

    /**
     * 获取串口设备管理器单例
     *
     * @return 串口设备管理器
     */
    public static VboxSerialPortManager getInstance() {
        return SerialPortManagerHolder.instance;
    }

    /**
     * 打开串口
     *
     * @param device 串口设备
     * @return 串口
     */
    public SerialPort open(VboxSerialPortDevice device) {
        return open(device.getPath(), device.getBaudRate());
    }

    private SerialPort open(String devicePath, String baudRate) {
        Log.d(TAG, "1111open: open dev:");
        try {
            // 打开串口
            Log.d(TAG, "open dev: " + devicePath + ":" + baudRate);
            File device = new File(devicePath);
            int bauRate = Integer.parseInt(baudRate);
            mSerialPort = new SerialPort(device, bauRate, 0, 0, 8, 1);
            //开始读数据 开启读线程
            mReadThread = new VboxSerialPortReadThread(mSerialPort.getInputStream());
            mReadThread.start();

            //获得输出流，发送数据
            mOutputStream = mSerialPort.getOutputStream();
            return mSerialPort;

        } catch (IOException e) {
            Log.d(TAG, "open error : " + e);
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 发送数据
     *
     * @param data 数据
     */
    public void sendData(String data) throws IOException {
        //我想将 AA+01+
        String hexStr = VboxCommand.STX +data +VboxCommand.ETX;
        try {
            byte[] bOutArray = Hex.decodeHex(hexStr.toCharArray());
           // byte[] bOutArray = hexStr.toLowerCase().getBytes(StandardCharsets.UTF_8);
            //byte[] bOutArray =HexUtils.stringToHexBytes(hexStr);
            mOutputStream.write(bOutArray);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Log.d("backinfo","hexStr:"+hexStr);
/*        byte[] bOutArray = hexStr.toLowerCase().getBytes(StandardCharsets.UTF_8);
        //byte[] bOutArray =HexUtils.stringToHexBytes(hexStr);
        mOutputStream.write(bOutArray);*/
    }


    /**
     * 关闭串口
     */
    public void close() {
        if (mReadThread != null) {
            mReadThread.close();
        }

        if (mOutputStream != null) {
            try {
                mOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (mSerialPort != null) {
            mSerialPort.close();
            mSerialPort = null;
        }
    }
}
