package com.siwei.recyclebox.deviceUtils;

import android.app.PendingIntent;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.hoho.android.usbserial.driver.Ch34xSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.util.SerialInputOutputManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import okio.ByteString;

/**
 * 所有串口设备的基类，每有一个新的串口设备添加就要实现本接口
 * 基础属性有
 */
public abstract class BaseDeviceEntity {

//    Class<? extends UsbSerialDriver> driverClass;
    private static final String TAG = "BaseDeviceEntity";
    public UsbSerialDriver driver;  //设备的驱动。
    public UsbSerialPort port;
//    public int vendorId;    //设备id
    public UsbManager mUsbManager;

    public BaseDeviceEntity(UsbManager usbManager,UsbSerialDriver driver){
        this.mUsbManager = usbManager;
        this.driver = driver;
        this.port = driver.getPorts().get(0);
        try {
            connect(mUsbManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 串口的连接参数：
     * baudRate: 波特率
     * dataBits: 数据位 一般为8
     * stopBits: 停止位 一般为1
     * parity: 奇偶校验 UsbSerialPort.PARITY_ODD、UsbSerialPort.PARITY_EVEN、UsbSerialPort.PARITY_NONE
     */
    abstract int getBaudRate();
    abstract int getDataBits();
    abstract int getStopBits();
    abstract int getParity();
    abstract int getVendorId();
    ExecutorService executorService = Executors.newSingleThreadExecutor();

    public void connect(UsbManager usbManager) throws IOException {
        System.out.println("connectDevice SerialPort");
        if(driver.getDevice().getVendorId() != getVendorId()){
            Log.e(TAG,"该设备有误");
            throw new IllegalArgumentException("vendorId 不正确 deviceVendorId:"+driver.getDevice().getVendorId()+"  vendorId:"+getVendorId());
        }
        UsbDeviceConnection connection = usbManager.openDevice(port.getDriver().getDevice());
        port.open(connection);
        port.setParameters(getBaudRate(), getDataBits(), getStopBits(), getParity());//连接参数

        /*SerialInputOutputManager inputOutputManager = new SerialInputOutputManager(port, new SerialInputOutputManager.Listener() {
            @Override
            public void onNewData(byte[] data) {
                System.out.println("inputOutputManager onNewData:" + ByteString.of(data)  );
                onDataReceived(data);
//                requestFilePermissions.setValue(data);
//                requestFilePermissions.call();
//                byte[] weightBytes = new byte[2];
//                weightBytes[0] = data[3];
//                weightBytes[1] = data[4];
//                latestWeight = ByteUtil.byte2short(weightBytes);
            }
            @Override
            public void onRunError(Exception e) {
                e.printStackTrace();
                executorService.shutdown();

            }
        });
        executorService.execute(inputOutputManager);*/
    }

    abstract void onDataReceived(byte[] response);
//    abstract Observer getObserver();
    public void close(){
        try {
            port.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
