package com.cr.viewmodel

import androidx.lifecycle.MutableLiveData
import com.cr.common.CrFlightControlInfo
import com.cr.cruav.R
import com.cr.data.DEFAULT_STR
import dji.sdk.keyvalue.key.BatteryKey
import dji.sdk.keyvalue.key.FlightControllerKey
import dji.sdk.keyvalue.key.KeyTools
import dji.sdk.keyvalue.key.ProductKey
import dji.sdk.keyvalue.value.flightcontroller.CompassCalibrationState
import dji.sdk.keyvalue.value.flightcontroller.IMUCalibrationState
import dji.sdk.keyvalue.value.product.ProductType
import dji.v5.et.create
import dji.v5.et.listen
import dji.v5.manager.KeyManager
import dji.v5.utils.common.ContextUtil
import dji.v5.utils.common.StringUtils

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/9 16:00
 * 描述：飞行器视图模型
 */
class CrFlightControlVM : CrViewModel() {
    // define: 2023/3/9 飞行器信息
    var flightControlInfo = MutableLiveData<CrFlightControlInfo>()

    /**
     * 初始化
     */
    init {
        flightControlInfo.value = CrFlightControlInfo()
        flightControlInfo.value?.batteryPercent = 0
        flightControlInfo.value?.gpsCount = DEFAULT_STR
        flightControlInfo.value?.productName = DEFAULT_STR
        flightControlInfo.value?.imuState = DEFAULT_STR
        flightControlInfo.value?.compassState = DEFAULT_STR
    }

    /**
     * 初始化监听
     */
    override fun initListener(){
        // todo: 2023/3/9 飞行器链接监听
        FlightControllerKey.KeyConnection.create().listen(this){
            it?.let {
                if(it){
                    flightControlInfo.value?.isConnection = true
                    var type = KeyManager.getInstance().getValue(KeyTools.createKey(ProductKey.KeyProductType),
                        ProductType.UNKNOWN)
                    flightControlInfo.value?.productName = type.toString()
                    refresh(flightControlInfo)
                }else{
                    flightControlInfo.value?.isConnection = false
                    flightControlInfo.value?.productName = DEFAULT_STR
                    flightControlInfo.value?.gpsCount = DEFAULT_STR
                    flightControlInfo.value?.imuState = DEFAULT_STR
                    flightControlInfo.value?.compassState = DEFAULT_STR
                    refresh(flightControlInfo)
                }
            }
        }
        // todo: 2023/3/9 飞行器电池监听
        BatteryKey.KeyConnection.create().listen(this){
            it?.let {
                if(!it){
                    flightControlInfo.value?.batteryPercent = 0
                    refresh(flightControlInfo)
                }
            }
        }
        // todo: 2023/3/9 飞行器电池电量百分比监听
        BatteryKey.KeyChargeRemainingInPercent.create().listen(this){
            it?.let {
                flightControlInfo.value?.batteryPercent = it
                refresh(flightControlInfo)
            }
        }
        // todo: 2023/3/9 卫星数量监听
        FlightControllerKey.KeyGPSSatelliteCount.create().listen(this){
            it?.let {
                flightControlInfo.value?.gpsCount = it.toString()
                refresh(flightControlInfo)
            }
        }
        // todo: 2023/3/9 IMU校准监听
        FlightControllerKey.KeyIMUCalibrationInfo.create().listen(this){
            it?.let {
                var state = it.getCalibrationState()
                when(state){
                    IMUCalibrationState.CALIBRATING ->
                        flightControlInfo.value?.imuState = "校准中"
                    IMUCalibrationState.SUCCESSFUL ->
                        flightControlInfo.value?.imuState = "成功"
                    IMUCalibrationState.FAILED ->
                        flightControlInfo.value?.imuState = "失败"
                    IMUCalibrationState.NONE ->
                        flightControlInfo.value?.imuState = "正常"
                    else->
                        flightControlInfo.value?.imuState = "未知"
                }
                refresh(flightControlInfo)
            }
        }
        // todo: 2023/3/14 指南针状态监听
        FlightControllerKey.KeyCompassCalibrationStatus.create().listen(this){
            it?.let {
                when(it){
                    CompassCalibrationState.FAILED->
                        flightControlInfo.value?.compassState = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_compass_status_failed)
                    CompassCalibrationState.IDLE->
                        flightControlInfo.value?.compassState = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_compass_status_idle)
                    CompassCalibrationState.SUCCEEDED->
                        flightControlInfo.value?.compassState = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_compass_status_success)
                    CompassCalibrationState.HORIZONTAL->
                        flightControlInfo.value?.compassState = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_compass_status_horizontal)
                    CompassCalibrationState.VERTICAL->
                        flightControlInfo.value?.compassState = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_compass_status_vertical)
                    else->
                        flightControlInfo.value?.compassState = StringUtils.getResStr(ContextUtil.getContext(),
                            R.string.value_compass_status_idle)
                }
                refresh(flightControlInfo)
            }
        }
    }
}