package com.module.backstage.activity.setting.coreparameter

import android.util.Log
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.hboxs.serialport.sbc.VBoxMessage
import com.hboxs.serialport.sbc.frame.VboxCommand
import com.module.backstage.R
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcParamAddressEnum
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class ActionParameterViewModel @Inject constructor(private val mRepo: ActionParameterRepository) :
    BaseViewModel() {
    private var _readData1Success = MutableLiveData<List<Int>>()//是否读取所有数据成功
    val readData1Success: LiveData<List<Int>> get() = _readData1Success


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

    }

    fun getGeneralParamData(): MutableList<PlcParamAddressEnum> {
        return mRepo.arrayList
    }

    fun getGeneralParamTwoData(): MutableList<PlcParamAddressEnum> {
        return mRepo.arrayList2
    }

    fun getGeneralParamThreeData(): MutableList<PlcParamAddressEnum> {
        return mRepo.arrayList3
    }


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

    public fun startData() {
        mRepo.threadGeneralParam.startData1(16)
    }

    fun disposeData(messageEvent: VBoxMessage) {
        // Log.d("11111111111111111",messageEvent.cmd+"GeneralParamFragmentVM:messageEvent.data:"+messageEvent.data)
        when (messageEvent.cmd) {
            VboxCommand.CMD_GETDATAOB -> {
                if (messageEvent.data.trim().startsWith("FF01")) {
                    Log.d(VboxCommand.CMD_GETDATA, "获取数据成功")
                    var data = messageEvent.data.substring(4, 108)
                    var list = parseHexString(data)
                    _readData1Success.value = list
                    mRepo.threadGeneralParam.closeData1()
                    Log.d("backinfo", "list:" + Gson().toJson(list))
                }
            }


            VboxCommand.CMD_WRITEDATAOB -> {
                Log.d(VboxCommand.CMD_WRITEDATA, "更新数据关闭")
                ToastUtil.switchToastStyleToSuccess(UiUtil.getStringRes(R.string.base_settingsuceess))
                mRepo.threadGeneralParam.closeWriteData()
            }

            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)
    }


}