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.hboxs.serialport.plc.frame.RstCommandFrame
import com.hboxs.serialport.plc.frame.SetCommandFrame
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 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
import java.lang.String

class ThreadHomeParam : LifecycleObserver {

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

    var jobM1: Job? = null
        get() = field
    var isActiveM1: Boolean = true
        get() = field
        set(value) {
            field = value
        }
    var jobD91: Job? = null
        get() = field
    var isActiveD91: Boolean = true
        get() = field
        set(value) {
            field = value
        }

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

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun startSetD170() {
        /* stopSetD170()
         // 启动协程
         jobD170 = CoroutineScope(Dispatchers.Main).launch {
             while (isActiveD170) {
                 SerialPortSendQueue.sendCommand(ReadCommandFrame(PlcHomeAddressEnum.D170.address,2), 2, PlcHomeAddressEnum.D170.address)
                 // 执行定时任务的操作
                 delay(500) // 每隔2秒重复执行
             }
         }*/
    }

    fun stopSetD170() {
        jobD170?.cancel()
    }

    /**
     * productId商品id,count总共要制作的数量
     * */
    fun startSetM231(productId: Int, count: Int) {
        Log.d("backinfo","productId:sssssssssssssssssssssssss"+productId);
        stopSetM231()
        val hexString = StringUtils.intTo4DigitHexStrict(productId)
        val numberhex = StringUtils.intToHex(count, 2)

        // 启动协程
        jobD91 = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveD91) {
                Log.d("backinfo", "开始制作冰沙:" + "2121" + hexString + "00")
                VboxSerialPortSendQueue.getInstance().sendCommand(
                    VboxWriteCommand(
                        VboxCommand.CMD_MAKE,
                        "212101" + hexString + "00"+numberhex+"000000"
                    )
                )
                //SerialPortSendQueue.sendCommand(WriteCommandFrame(PlcHomeAddressEnum.D91.address,value), 2, PlcHomeAddressEnum.D91.address)
                // 执行定时任务的操作
                delay(2000) // 每隔2秒重复执行
            }
        }
    }

    fun testP(p: kotlin.String) {
        VboxSerialPortSendQueue.getInstance()
            .sendCommand(VboxWriteCommand(VboxCommand.CMD_MAKE, "FF12" + p + "0000000000"))

    }

    fun stopSetM231() {
        jobD91?.cancel()
    }


    fun startSetM1() {
        stopSetM1()
        // 启动协程
        jobM1 = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveM1) {
                SerialPortSendQueue.sendCommand(
                    SetCommandFrame(PlcHomeAddressEnum.M1.address),
                    2,
                    PlcHomeAddressEnum.M1.address
                )
                // 执行定时任务的操作
                delay(200) // 每隔2秒重复执行
            }
        }
    }

    fun stopSetM1() {
        jobM1?.cancel()
    }

    fun startRstM1() {
        stopRstM1()
        // 启动协程
        jobRM1 = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveRM1) {
                SerialPortSendQueue.sendCommand(
                    RstCommandFrame(PlcHomeAddressEnum.M1.address),
                    2,
                    PlcHomeAddressEnum.RM1.address
                )
                // 执行定时任务的操作
                delay(200) // 每隔2秒重复执行
            }
        }
    }

    fun stopRstM1() {
        jobRM1?.cancel()
    }

    /**
     * 关掉线程池
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun destroy() {
        stopSetM231()
        stopSetD170()
        stopSetM1()
    }


}