package com.module.backstage.vm

import android.util.Log
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcParamAddressEnum
import com.hboxs.serialport.plc.frame.ResponseFrame
import com.hboxs.serialport.plc.message.Message
import com.hboxs.serialport.plc.util.AsciiUtils
import com.quyunshuo.androidbaseframemvvm.common.util.ByteUtils
import com.hboxs.serialport.plc.util.HexUtils
import com.hboxs.serialport.plc.util.HexadecimalUtil
import com.hboxs.serialport.sbc.VBoxMessage
import com.hboxs.serialport.sbc.frame.VboxCommand
import com.module.backstage.R
import com.module.backstage.repo.CloseReParamFragmentRepo
import com.module.backstage.repo.GeneralParamFragmentRepo
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcCloseAddressEnum
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.common.util.CheckUtlis
import com.quyunshuo.androidbaseframemvvm.common.util.LogUtil
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

/**
 * HomeFragment 的 VM 层
 *
 * @author Qu Yunshuo
 * @since 2021/8/5 11:11 下午
 */
@HiltViewModel//@HiltViewModel 注解的作用是将 ViewModel 标记为可以由 Hilt 管理的依赖项。当你将 @HiltViewModel 注解添加到 ViewModel 类上时，Hilt 会自动处理 ViewModel 的创建和注入过程。这样，你就不需要手动调用 ViewModelProvider 来获取 ViewModel 实例了。
class CloseReParamFragmentVM @Inject constructor(private val mRepo: CloseReParamFragmentRepo) :
    BaseViewModel() {
    private val TAG = "GeneralParamFragmentVM"

    //读取到的数据
//    var paramDataList: MutableList<String> = mutableListOf()
    var paramDataList = mutableMapOf<String, String>()

    //    val generalParamData: MutableList<String> get() = _generalParamData
    private var _readAllDataSuccess = MutableLiveData<List<Int>>()//是否读取所有数据成功
    val readAllDataSuccess: LiveData<List<Int>> get() = _readAllDataSuccess


    /**
     * 获取文章数据
     */
    fun getArticleData() {

    }

    fun getCloseReParamData(): MutableList<PlcCloseAddressEnum> {
        return mRepo.arrayList
    }


    fun getThreadGeneralParam(): LifecycleObserver {
        return mRepo.threadGeneralParam
    }

    public fun startData() {

        mRepo.threadGeneralParam.startData20()
    }

    fun disposeData(messageEvent: VBoxMessage) {
        when (messageEvent.cmd) {
            VboxCommand.CMD_GETDATATWENTY -> {
                Log.d(
                    "11111111111111111",
                    messageEvent.cmd + "GeneralParamFragmentVM:messageEvent.data:" + messageEvent.data
                )
                if (CheckUtlis.checkHex(messageEvent.data.trim())) {
                    var staute = messageEvent.data.trim().substring(106, 110).toString()
                    if ("FF01".equals(staute)) {
                        Log.d(VboxCommand.CMD_GETDATA, "获取数据成功")
                        var data = messageEvent.data.substring(0, 104)
                        var list = parseHexString(data)
                        _readAllDataSuccess.value = list
                        mRepo.threadGeneralParam.closeData()
                        Log.d("backinfo", "list:" + Gson().toJson(list))
                    }
                } else {
                    ToastUtil.switchToastStyleToWarn("校验失败")
                }


                //   LogUtil.d("backinfo","list:"+Gson().toJson(list))
            }

            VboxCommand.CMD_WRITEDATATWENTY -> {
                if (CheckUtlis.checkHex(messageEvent.data.trim())) {
                    Log.d(VboxCommand.CMD_WRITEDATA, "更新数据关闭")
                    var data = messageEvent.data.substring(0, 104)
                    var list = parseHexString(data)
                    _readAllDataSuccess.value = list
                    Log.d("backinfo", "list:" + Gson().toJson(list))

                    mRepo.threadGeneralParam.closeWriteData()
                  //  startData()
                } else {
                    ToastUtil.switchToastStyleToWarn("校验失败")
                }
            }

            else -> {}
        }
    }

    fun parseHexString(input: String): List<Int> {
        return input.chunked(4).map { chunk ->
            require(chunk.length == 4) { "Chunk '$chunk' must be 4 characters long" }
            val firstHex = chunk.substring(0, 2)
            val secondHex = chunk.substring(2, 4)
            val firstValue = firstHex.toInt(16)
            val secondValue = secondHex.toInt(16)
            firstValue * 256 + secondValue
        }
    }

    fun sqWriteData(data: String) {
        mRepo.threadGeneralParam.writeData(data)
    }


}