package com.cr.viewmodel

import androidx.lifecycle.MutableLiveData
import com.cr.common.CrRemoteControlInfo
import com.cr.cruav.R
import dji.sdk.keyvalue.key.RemoteControllerKey
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/9 14:15
 * 描述：遥控器视图模型
 */
class CrRemoteControlVM : CrViewModel(){
    // define: 2023/3/9 遥控器信息
    var remoteControlInfo = MutableLiveData<CrRemoteControlInfo>()

    /**
     * 初始化
     */
    init {
       remoteControlInfo.postValue(CrRemoteControlInfo(StringUtils.getResStr(ContextUtil.getContext(),
           R.string.value_product_remote_controller)))
        remoteControlInfo.value?.isConnection = false;
        remoteControlInfo.value?.batteryPercent = 0;
    }

    /**
     * 初始化监听
     */
    override fun initListener(){
        // todo: 2023/3/9 遥控器链接监听
        RemoteControllerKey.KeyConnection.create().listen(this){
            it?.let {
                if(it){
                    remoteControlInfo.value?.isConnection = true
                    remoteControlInfo.value?.connectionInfo = StringUtils.getResStr(ContextUtil.getContext(),
                        R.string.value_rc_connection)
                    refresh(remoteControlInfo)
                }else{
                    remoteControlInfo.value?.isConnection = true
                    remoteControlInfo.value?.connectionInfo = StringUtils.getResStr(ContextUtil.getContext(),
                        R.string.value_product_remote_controller)
                    remoteControlInfo.value?.batteryPercent = 0
                    refresh(remoteControlInfo)
                }
            }
        }
        // todo: 2023/3/9 遥控器电池监听
        RemoteControllerKey.KeyBatteryInfo.create().listen(this){
            it?.let {
                remoteControlInfo.value?.batteryPercent = it.batteryPercent
                refresh(remoteControlInfo)
            }
        }
    }
}