package com.cr.viewmodel

import androidx.lifecycle.MutableLiveData
import com.cr.common.CrLinkInfo
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.data.DEFAULT_STR
import dji.sdk.keyvalue.key.AirLinkKey
import dji.sdk.keyvalue.value.airlink.FrequencyBand
import dji.v5.et.create
import dji.v5.et.listen
import dji.v5.utils.common.ContextUtil
import dji.v5.utils.common.StringUtils

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/14 16:24
 * 描述：链路模型
 */
class CrLinkVM :CrViewModel(){
    var linkInfo = MutableLiveData<CrLinkInfo>()

    /**
     * 初始化
     */
    init {
        linkInfo.postValue(CrLinkInfo(false))
        linkInfo.value?.signalQuality = 0
        linkInfo.value?.frequencyBand = DEFAULT_STR
    }


    // todo: 2023/3/14 初始化监听
    override fun initListener() {
        // todo: 2023/3/14 链路链接监听
        AirLinkKey.KeyConnection.create().listen(this){
            it?.let {
                if(it){
                    linkInfo.value?.isConnection = true
                }else{
                    CrUtil.print("链路信息重置")
                    reset()
                }
                refresh(linkInfo)
            }
        }
        // todo: 2023/3/14 链路信号质量监听
        AirLinkKey.KeyLinkSignalQuality.create().listen(this){
            it?.let {
                linkInfo.value?.signalQuality = it*10
                CrUtil.print("链路质量" + linkInfo.value?.signalQuality)
                refresh(linkInfo)
            }
        }
        // todo: 2023/3/14 工作频段监听
        AirLinkKey.KeyFrequencyBand.create().listen(this){
            it?.let {
                when(it){
                    FrequencyBand.BAND_DUAL->
                        linkInfo.value?.frequencyBand = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_link_frequency_band_dual)
                    FrequencyBand.BAND_2_DOT_4G->
                        linkInfo.value?.frequencyBand = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_link_frequency_band_24)
                    FrequencyBand.BAND_1_DOT_4G->
                        linkInfo.value?.frequencyBand = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_link_frequency_band_14)
                    FrequencyBand.BAND_5_DOT_7G->
                        linkInfo.value?.frequencyBand = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_link_frequency_band_57)
                    FrequencyBand.BAND_5_DOT_8G->
                        linkInfo.value?.frequencyBand = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_link_frequency_band_58)
                    FrequencyBand.BAND_5_DOT_2G->
                        linkInfo.value?.frequencyBand = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_link_frequency_band_52)
                    else->
                        linkInfo.value?.frequencyBand = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.cr_string_default_value)
                }
                refresh(linkInfo)
            }
        }
    }

    /**
     * 覆写重置
     */
    override fun reset() {
        linkInfo.value?.isConnection = false
        linkInfo.value?.signalQuality = 0
        linkInfo.value?.frequencyBand = DEFAULT_STR
        refresh(linkInfo)
    }
}