package com.device.comm.mylibrary;

import android.content.Context;
import android.util.Log;

import com.android.pj11.PJ11UartJni;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;

import java.text.SimpleDateFormat;


import static com.device.comm.mylibrary.RNYASEEGlucoseModule.readDeviceStatus;
import static com.device.comm.mylibrary.RNYASEEGlucoseModule.uartState;
import static com.device.comm.mylibrary.RNYASEEGlucoseModule.yaseeGlucoseModuel;

public abstract class RNM10BaseCommandHandler {
    final protected static String TAG = "RN_M10_BASE_MODULE";
    protected Context mContext;
    protected Integer resultType = 0;
    protected static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
    // 新添加的字段
    protected volatile boolean shouldRun = false;
    private CheckDeviceThread checkDeviceThread = null;
    public KOJNativeEventManager mEventManager;


    public RNM10BaseCommandHandler(ReactApplicationContext context) {
        this.mContext = context;
        mEventManager = new KOJNativeEventManager(context);

    }


//    public void start(){
//        yaseeGlucoseModuel.findDevice = false;
//        startCheckThread();
//    }



    protected CheckDeviceThread createCheckDeviceThread() {
        return new CheckDeviceThread();
    }

    public void startCheckThread() {
        shouldRun = true;
        if (checkDeviceThread == null || !checkDeviceThread.isAlive()) {
            checkDeviceThread = createCheckDeviceThread();
            checkDeviceThread.start();
        }
    }
    public void stopCheckThread() {
        shouldRun = false;
    }


    protected class CheckDeviceThread extends Thread {
        @Override
        public void run() {
            while (shouldRun) {
                if(yaseeGlucoseModuel.findDevice){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    continue;
                }
                try {
                    String status = readDeviceStatus();
                    RNLog.d(TAG,"device status:"+status);
                    if(status.startsWith("0")){
                        Thread.sleep(1000);
                        continue;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

                RNLog.d(TAG,"device uart status:"+uartState);
                if(uartState!=0){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    continue;

                }
                sendHeartbeat();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected boolean checkCommand(byte[] checkBytes){
        if(checkBytes.length<5){
            return false;
        }

        if (HexUtils.getUnsignedByte(checkBytes[0]) != 0xAA && HexUtils.getUnsignedByte(checkBytes[1]) != 0x55) {
            //不是帧头continue
            RNLog.d(TAG,"not a start");
            return false;
        }

        return true;
    }

    protected void sendAck(byte command){
        yaseeGlucoseModuel.sendAck(command);
        RNLog.d(TAG,"send ack "+(command&0xff));
    }

    protected int countVerify(byte[] dataList){
        int verifyData = 0;
        for(byte data:dataList){
            verifyData = verifyData + (data&0xff);
        }
        return verifyData;

    }

    //过滤ffffff
    protected static String filterByte(String str) {
        if (str.length() ==8 && str.substring(0, 6).equals("ffffff")) {
            str = str.substring(6, 8);
        }
        return str;
    }

    protected void sendHand(){
        yaseeGlucoseModuel.sendHand();
        Log.d(TAG,"send Hand");
    }

    protected void sendHeartbeat(){
        yaseeGlucoseModuel.sendHeartbeat();
        Log.d(TAG,"send  Heart beat");
    }
    protected void sendSleep(){
        yaseeGlucoseModuel.sendSleep();

        Log.d(TAG,"send sleep");
    }

    protected synchronized void postDelay(Runnable runnable,long delayMillis){
        final UpdateLoop updateLoop = BleDeviceManager.get(mContext).getMainUpdateLoop();
        updateLoop.postDelay(runnable,delayMillis);
    }

    protected abstract void commandHandler(byte[] data, int length);
    protected abstract void sendStatus(String status,Integer command,Integer extraParameter);
    protected abstract void deviceStatusChanged(int status);
    protected abstract void sendInitStatus();
}