package com.xixun.communicate;

import android.os.RemoteException;

import com.xixun.joey.uart.BytesData;
import com.xixun.joey.uart.IUartListener;

import java.util.Arrays;

/**
 * Created by Administrator on 2018/03/24.
 */

public class AidlSerialPort {
        LedCardCommunication ledCardCommunication=null;
        String aidlDeviceName=null;
        int iBaudRate=0;

        public AidlSerialPort(LedCardCommunication ledCardCommunication,String aidlDeviceName,int iBaudRate)
        {
            this.ledCardCommunication=ledCardCommunication;
            this.aidlDeviceName=aidlDeviceName;
            this.iBaudRate=iBaudRate;

            if (aidlDeviceName==null) return;
            if (ledCardCommunication==null) return;
            if (ledCardCommunication.clientsManage==null) return;
            if (ledCardCommunication.clientsManage.ms==null) return;
            if (ledCardCommunication.clientsManage.ms.serialPorts==null) return;

            new Thread() {
                public void run() {
                    Process();
                }
            }.start();
        }

        void Process()
        {
            try {
                if (iBaudRate>0) ledCardCommunication.clientsManage.ms.serialPorts.config(aidlDeviceName,iBaudRate);
                bReady=true;
                System.out.println("-------------------IUartListener");
                ledCardCommunication.clientsManage.ms.serialPorts.read(aidlDeviceName, new IUartListener.Stub() {
                    @Override
                    public void onReceive(BytesData DataBuffer) throws RemoteException {

                        if (NeedExit()) return;
                        if (GetReady()==false) return;

                        if (DataBuffer==null)
                        {
                            ledCardCommunication.Request_Exit();
                            return;
                        }

                        byte [] dataBuffer=DataBuffer.getData();
                        System.out.println(Arrays.toString(dataBuffer));
                        if (dataBuffer==null)
                        {
                            ledCardCommunication.Request_Exit();
                            return;
                        }

                        if (dataBuffer.length<=0)
                        {
                            ledCardCommunication.Request_Exit();
                            return;
                        }

                        if (FunctionCallBack!=null) {
                            FunctionCallBack.LastReceDataProcess(dataBuffer, dataBuffer.length);
                            System.out.println("-----------ReceBuffer1");
                        }
                        else
                        {
                            String receive = "";
                            for (byte data : DataBuffer.getData()) {
                                receive += "0x" + Integer.toHexString(data & 0xFF) + " ";
                            }
                            System.out.println(receive);
                        }
                        dataBuffer=null;
                    }
                });
            } catch (RemoteException e) {
                System.out.println(Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                        +   "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
                        +   "Line" +Thread.currentThread().getStackTrace()[2].getLineNumber() + ":\r\n"
                        +   "    Error:" + e.getMessage() + "\r\n\r\n"
                );
                ledCardCommunication.Request_Exit();
            }
        }

    public void TransData_Byte(byte[] DataBuffer, int iLength) {
        if (NeedExit()) return;
        if (GetReady() == false) return;
        if (ledCardCommunication.clientsManage.ms.serialPorts == null) return;

        // 复制实际需要的字节缓冲。
        byte[] DataBufferTemp = new byte[iLength];
        System.arraycopy(DataBuffer, 0, DataBufferTemp, 0, iLength);
        //System.out.println(aidlDeviceName + " Aidl Trans iLength= " + iLength);

        try {
            ledCardCommunication.clientsManage.ms.serialPorts.write(aidlDeviceName, DataBufferTemp);
        } catch (RemoteException e) {
            System.out.println(Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                    + "Function: " + Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
                    + "Line" + Thread.currentThread().getStackTrace()[2].getLineNumber() + ":\r\n"
                    + "    Error:" + e.getMessage() + "\r\n\r\n"
            );
            ledCardCommunication.Request_Exit();
        }
    }
        IFunctionCallBack FunctionCallBack=null;
        public void SetCallFunc(IFunctionCallBack FunctionCallBack) {
            this.FunctionCallBack = FunctionCallBack;
        }

        boolean bReady=false;
        public  boolean GetReady() { return bReady; }

        Boolean bNeedExit=false;
        boolean NeedExit() { return bNeedExit; }

        public void Exit()
        {    if (bNeedExit) return;
            bNeedExit=true;
            bReady=false;
            System.out.println(aidlDeviceName+ " "+ Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                        +   "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName()
                );
        }
    }
