package com.hboxs.serialport.plc.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.quyunshuo.androidbaseframemvvm.base.addressenum.PlcParamAddressEnum
import com.hboxs.serialport.plc.frame.ReadCommandFrame
import com.hboxs.serialport.plc.frame.WriteCommandFrame
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.utils.commons.StringUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class ThreadGeneralParam : LifecycleObserver {

    private val TAG = "ThreadGeneralParam"


    var jobWriteData: Job? = null
        get() = field

    var isActiveWriteData: Boolean = true
        get() = field
        set(value) {
            field = value
        }


    var jobError: Job? = null
        get() = field
    var isError: Boolean = true
        get() = field
        set(value) {
            field = value
        }


    var jobdata01: Job? = null
        get() = field
    var isdata01: Boolean = true
        get() = field
        set(value) {
            field = value
        }

    var jobdata02: Job? = null
        get() = field
    var isdata02: Boolean = true
        get() = field
        set(value) {
            field = value
        }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun startRead() {
       /* startData1()
        startData2()*/
        //    destroy()

        // 启动协程
        /*  job230 = CoroutineScope(Dispatchers.Main).launch {
              while (isActive230) {
                  //参数 1 2 31 3 4 5   D400 D401 D402 D404 D405 D406
                  SerialPortSendQueue.sendCommand(ReadCommandFrame(PlcParamAddressEnum.D230.address, 14), 400, PlcParamAddressEnum.D230.address)
                  // 执行定时任务的操作
                  Log.d(TAG, "startRead: 230")
                  delay(100) // 每隔2秒重复执行
              }
          }
          job447 = CoroutineScope(Dispatchers.Main).launch {
              while (isActive447) {
                  SerialPortSendQueue.sendCommand(ReadCommandFrame(PlcParamAddressEnum.D447.address, 4), 400, PlcParamAddressEnum.D447.address)
                  // 执行定时任务的操作
                  Log.d(TAG, "startRead: 447")
                  delay(100) // 每隔2秒重复执行
              }
          }*/
    }

    public fun startData1(dataPosition: Int) {
        var position = StringUtils.intTo4DigitHexStrict(dataPosition)
        closeData1()
        jobdata01 = CoroutineScope(Dispatchers.Main).launch {
            while (isdata01) {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_GETDATA, "000000010002" + position))
                // 执行定时任务的操作
                Log.d(TAG, "jobdata01")
                delay(500) // 每隔2秒重复执行
            }
        }
    }

    public fun startData2() {
        closeData2()
        jobdata02 = CoroutineScope(Dispatchers.Main).launch {
            while (isdata02) {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_GETDATATWO, "00000101000200"))
                // 执行定时任务的操作
                Log.d(TAG, "jobdata02")
                delay(500) // 每隔2秒重复执行
            }
        }
    }

    public fun closeData2() {
        jobdata02?.cancel()
    }

    public fun closeData1() {
        jobdata01?.cancel()
    }

    fun writeData(data: String) {
        closeWriteData()
        jobWriteData = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveWriteData) {
                Log.d("backinfo", "发生的数据：" + data)
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_WRITEDATA, data))
                delay(1000) // 每隔2秒重复执行
            }
        }
    }

    fun closeWriteData() {
        jobWriteData?.cancel()
    }

    fun startTestError(error: String) {
        stopError()
        jobError = CoroutineScope(Dispatchers.Main).launch {
            // while (isError) {
            Log.d("backinfo", "error:" + error)
            VboxSerialPortSendQueue.getInstance()
                .sendCommand(VboxWriteCommand(VboxCommand.CMD_ERROR, error))
            //写入
            delay(3000) // 每隔2秒重复执行
            // }
        }
    }

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


    /**
     * 关掉线程池
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun destroy() {
        //避免线程池关不掉
        closeData1()
        closeData2()
        stopError()
        closeWriteData()
    }


}