package com.hboxs.serialport.sbc.thread

import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.hboxs.serialport.plc.SerialPortSendQueue
import com.hboxs.serialport.plc.frame.ReadCommandFrame
import com.hboxs.serialport.sbc.VboxSerialPortSendQueue
import com.hboxs.serialport.sbc.frame.VboxCommand
import com.hboxs.serialport.sbc.frame.VboxWriteCommand
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcHomeAddressEnum
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class ThreadGlobalParam: LifecycleObserver {

    private val TAG = "ThreadGeneralParam"
    //初始化
    var jobSbcInit: Job? = null
        get() = field
    var isActiveSbcInit: Boolean = true
        get() = field
        set(value) {
            field = value
        }
    //读取心跳
    var jobHeartbeat: Job? = null
        get() = field
    var isActiveHeartbeat: Boolean = true
        get() = field
        set(value) {
            field = value
        }

    //读取后台数据
    var jobBackData: Job? = null
        get() = field
    var isActiveBackData: Boolean = true
        get() = field
        set(value) {
            field = value
        }

    //关闭错误提醒
    var jobError: Job? = null
        get() = field
    var isError: Boolean = true
        get() = field
        set(value) {
            field = value
        }


    fun startCloseError(isFatal: Boolean) {
        stopCloseError()
        // 启动协程
        jobError = CoroutineScope(Dispatchers.Main).launch {
            while (isError) {
                if (isFatal){
                    VboxSerialPortSendQueue.getInstance()
                        .sendCommand(VboxWriteCommand(VboxCommand.CMD_ERROR, "00060100000000"))
                }else{
                    VboxSerialPortSendQueue.getInstance()
                        .sendCommand(VboxWriteCommand(VboxCommand.CMD_ERROR, "00060200000000"))
                }

                delay(1000) // 每隔2秒重复执行
            }
        }
    }

    fun stopCloseError(){
        jobError?.cancel()
    }

    fun startInit() {
        Log.d("backinfo","调用startInit");
        stopInit()
        isActiveSbcInit=true
        // 启动协程
        jobSbcInit = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveSbcInit) {
                //参数 1 2 31 3 4 5   D400 D401 D402 D404 D405 D406
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_INIT, "0000000100000000"))
                // 执行定时任务的操作
                Log.d(TAG, "startRead: Init")
                delay(1000) // 每隔2秒重复执行
            }
        }
    }

    fun stopInit(){
        isActiveSbcInit=false
        jobSbcInit?.cancel()
    }

    fun startHeartbeat() {
        stopHeartbeat()
        // 启动协程
        jobHeartbeat = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveHeartbeat) {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_HEARTBEAT, "00000000"))
                // 执行定时任务的操作
                Log.d(TAG, "start：Heartbeat")
                delay(1000) // 每隔1秒重复执行
            }
        }
    }

    fun stopHeartbeat() {
        jobHeartbeat?.cancel()
    }

    fun startBackData() {
        stopBackData()
        // 启动协程
        jobBackData = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveBackData) {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_BACK_DATA, "0000FF01"))
                // 执行定时任务的操作
                Log.d(TAG, "start：Heartbeat")
                delay(2000) // 每隔2秒重复执行
            }
        }
    }

    fun stopBackData() {
        jobBackData?.cancel()
    }
    var jobM3: Job? = null
        get() = field
    var isActiveM3: Boolean = true
        get() = field
        set(value) {
            field = value
        }
    fun startSetM3(data:String) {
        stopSetM3()
        // 启动协程
        jobM3 = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveM3) {
                // SerialPortSendQueue.sendCommand(SetCommandFrame(PlcSettingAddressEnum.M3.address), 10, PlcSettingAddressEnum.M3.address)
                // 执行定时任务的操作
                VboxSerialPortSendQueue.getInstance().sendCommand(VboxWriteCommand(VboxCommand.CMD_OPENORCLOSE, "11110101${data}00"))
                Log.d("backinfo","发生开机指令")
                delay(3000) // 每隔0.3秒重复执行

            }
        }
    }
    fun stopSetM3(){
        jobM3?.cancel()
    }

    /**
     * 关掉线程池
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun destroy() {
        //避免线程池关不掉
        stopInit()
        stopSetM3()
        stopCloseError()
        stopHeartbeat()
    }
}