package com.cr.viewmodel

import androidx.lifecycle.MutableLiveData
import com.cr.common.CrMSDKInfo
import com.cr.data.DEFAULT_STR
import com.cr.models.CrMSDKInfoModel
import dji.sdk.keyvalue.key.FlightControllerKey
import dji.sdk.keyvalue.key.ProductKey
import dji.v5.et.create
import dji.v5.et.get
import dji.v5.et.listen
import dji.v5.manager.SDKManager
import dji.v5.manager.ldm.LDMManager

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/7 16:11
 * 描述：提供SDKInfo View相关接口，操作MSDKInfoModel相关功能
 */
class CrMSDKInfoVM : CrViewModel() {
    var msdkInfo = MutableLiveData<CrMSDKInfo>()
    var msdkInfoModel:CrMSDKInfoModel = CrMSDKInfoModel()

    /**
     * 初始化
     */
    init {
        // TODO: 初始化MSDK版本信息
        msdkInfo.postValue(CrMSDKInfo(msdkInfoModel.getSDKVersion()))
        // TODO: 初始化编译版本信息
        msdkInfo.value?.buildVer = msdkInfoModel.getBuildVersion()
        // TODO: 是否启用调试
        msdkInfo.value?.isDebug = msdkInfoModel.isDebug()
        // TODO: 初始化分组信息
        msdkInfo.value?.packageProductCategory = msdkInfoModel.getPackageProductCategory()
        // TODO: 是否启用LDM
        msdkInfo.value?.isLDMEnabled = LDMManager.getInstance().isLDMEnabled.toString()
        // TODO: 是否载入LDM监听
        msdkInfo.value?.isLDMLicenseLoaded = LDMManager.getInstance().isLDMLicenseLoaded.toString()
    }

    /**
     * 更新MSDK信息
     */
    private fun refreshMSDKInfo(){
        msdkInfo.postValue(msdkInfo.value)
    }

    /**
     * 初始化监听 必须在App注册成功后调用
     */
    override fun initListener(){
        if(!SDKManager.getInstance().isRegistered){
            return
        }
        // TODO: 飞行器链接
        FlightControllerKey.KeyConnection.create().listen(this){
            ProductKey.KeyFirmwareVersion.create().get({
                msdkInfo.value?.firmwareVer = it?: DEFAULT_STR
                refreshMSDKInfo()
            }){
                msdkInfo.value?.firmwareVer = DEFAULT_STR
                refreshMSDKInfo()
            }
        }
        // TODO: 产品链接
        ProductKey.KeyProductType.create().listen(this){
            it?.let {
                msdkInfo.value?.productType = it
                refreshMSDKInfo()
            }
        }
    }
}