package com.rdiot.yx485.ui.bind.model

import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.scopeNetLife
import androidx.lifecycle.viewModelScope
import com.drake.net.utils.scopeNetLife
import com.drake.serialize.serialize.serialLazy
import com.drake.serialize.serialize.serialLiveData
import com.king.zxing.util.LogUtils
import com.rdiot.yx485.bean.DeviceStatus
import com.rdiot.yx485.bean.LoginData
import com.rdiot.yx485.bean.UserData
import com.rdiot.yx485.net.getDeviceStatus
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

/**
 * 绑定ViewModel
 * @author mR2hao
 * @date 2022/12/1
 */
class BindViewModel : ViewModel() {
    /** 房间名 */
    var roomName: ObservableField<String> = ObservableField("")

    /** 面板编号 */
    var panelNum: ObservableField<String> = ObservableField("") //807D3AE5E5D4

    /** Wifi名字 */
    var wifiSsid: ObservableField<String> = ObservableField("")

    /** Wifi密码 */
    var wifiPwd: ObservableField<String> = ObservableField("")

    /** 绑定分控标志 */
    var isBindSub: Boolean = false

    /** 绑定永续精灵标志 */
    var isBindInfrared: Boolean = false

    /** 定位名字（市） */
    var locationName: String? = null

    /** 设备类型id */
    var deviceTypeId: ObservableField<String> = ObservableField("")

    /** 设备类型code */
    var code: String = ""

    /** 配网倒计时 */
    var countDownTime: MutableLiveData<Int> = MutableLiveData(-1)

    var deiceStatus: MutableLiveData<Int>  = MutableLiveData(0)

    private var countDownJob: Job? = null
    private var checkOnLine: Job? = null
    companion object {
        lateinit var DeviceStatus: DeviceStatus
        const val COUNT_TIME = 120
    }

    fun testMode() {
        LogUtils.w("当前使用testMode")
        LogUtils.e("当前使用testMode")
        LogUtils.w("当前使用testMode")
        LogUtils.e("当前使用testMode")
        LogUtils.w("当前使用testMode")
        LogUtils.e("当前使用testMode")
        roomName = ObservableField("1")
        panelNum = ObservableField("C8F09EE84FB0")
        wifiSsid = ObservableField("RD-100YUN-2.4G")
        wifiPwd = ObservableField("22230920yun")

    }

    fun startCountDown() {
        countDownJob?.cancel()
        countDownTime.value = COUNT_TIME
        countDownJob = viewModelScope.launch {
            while (isActive && countDownTime.value!! > 0) {
                if (countDownTime.value!! <= 0) {
                    countDownJob?.cancel()
                    countDownJob = null
                }
                delay(1000)
                countDownTime.value = countDownTime.value!! - 1
            }
        }
    }

    fun stopCountDown() {
        countDownJob?.cancel()
        countDownJob = null
    }

    fun startCheckOnline() {
        checkOnLine?.cancel()
        countDownTime.value = COUNT_TIME
        checkOnLine = viewModelScope.launch {
            while (isActive) {
                scopeNetLife {
                    LogUtils.d("panelNum ${panelNum.get()}")
                        BindViewModel.DeviceStatus = getDeviceStatus(panelNum.get()).await()
                        deiceStatus.value = BindViewModel.DeviceStatus.status
                }
                delay(1000)
            }
        }
    }

    fun stopCheckOnline() {
        checkOnLine?.cancel()
        countDownJob = null
    }

}