package com.cr.viewmodel

import androidx.lifecycle.MutableLiveData
import com.cr.common.CrAudioUtil
import com.cr.common.CrKeyManager
import com.cr.common.CrRemoteControlInfo
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.event.CrCommonAction
import com.cr.event.EventCommon
import dji.sdk.keyvalue.key.CameraKey
import dji.sdk.keyvalue.key.RemoteControllerKey
import dji.sdk.keyvalue.value.camera.CameraMode
import dji.sdk.keyvalue.value.common.EmptyMsg
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 = false
                    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)
            }
        }
        // todo: 2023/8/4 拍照按钮按下
        RemoteControllerKey.KeyShutterButtonDown.create().listen(this){
            it?.let {
                if(it){
                    photo()
                }
            }
        }
        // todo: 2023/8/5 切换相机模式
        RemoteControllerKey.KeyRCSwitchButtonDown.create().listen(this){
            it?.let {
                if(it){
                    photoOrVideo()
                }
            }
        }
    }

    /**
     * 拍照
     */
    private fun photo(){
        CrKeyManager.getValue(CameraKey.KeyCameraMode,object:CrKeyManager.ICompletion<CameraMode>{
            // todo: 2023/8/5 获取成功
            override fun onSuccess(t: CameraMode) {
                if(t.isPhotoMode){
                    // todo: 2023/8/5 执行拍照动作
                    CrKeyManager.performAction(CameraKey.KeyStartShootPhoto,object:CrKeyManager.ICompletion<EmptyMsg>{
                        // todo: 2023/8/5 成功
                        override fun onSuccess(t: EmptyMsg) {
                            CrAudioUtil.playSound(CrApplication.getContext(),R.raw.cr_shutter_1)
                            // todo: 2023/8/7 拍照成功 保存当前数据帧YUV数据为图片
                            CrApplication.getEventBus().post(EventCommon<String>(CrCommonAction.VIDEO_SAVE_YUV))
                        }

                        // todo: 2023/8/5 错误
                        override fun onFailure(error: String) {
                            CrUtil.showToast("拍照失败:${error}")
                        }
                    })
                }else{
                    CrUtil.showToast("请切换到拍照模式")
                }
            }

            // todo: 2023/8/5 获取失败
            override fun onFailure(error: String) {
                CrUtil.showToast("获取失败:${error}")
            }
        })
    }

    /**
     * 切换拍照或录像
     */
    private fun photoOrVideo(){
        CrKeyManager.getValue(CameraKey.KeyCameraMode,object:CrKeyManager.ICompletion<CameraMode>{
            // todo: 2023/8/5 获取成功
            override fun onSuccess(t: CameraMode) {
                if(t.isPhotoMode){
                    // todo: 2023/8/5 切换到录像模式
                    CrKeyManager.setValue(CameraKey.KeyCameraMode,CameraMode.VIDEO_NORMAL,object:CrKeyManager.ICompletion<String>{
                        // todo: 2023/8/5 设置成功
                        override fun onSuccess(t: String) {
                            CrUtil.showToast("切换到录像模式")
                        }

                        // todo: 2023/8/5 设置失败
                        override fun onFailure(error: String) {
                            CrUtil.showToast("切换失败:${error}")
                        }

                    })
                }else if(t.isVideoMode){
                    // todo: 2023/8/5 切换到拍照模式
                    CrKeyManager.setValue(CameraKey.KeyCameraMode,CameraMode.PHOTO_NORMAL,object:CrKeyManager.ICompletion<String>{
                        // todo: 2023/8/5 切换成功
                        override fun onSuccess(t: String) {
                            CrUtil.showToast("切换到拍照模式")
                        }

                        override fun onFailure(error: String) {
                            CrUtil.showToast("切换失败:${error}")
                        }

                    })
                }
            }

            // todo: 2023/8/5 获取失败
            override fun onFailure(error: String) {
                CrUtil.showToast("获取失败:${error}")
            }
        })
    }
}