package com.cr.viewmodel

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.cr.common.CrMSDKRegister
import com.cr.cruav.R
import com.cr.data.REGISTER_FAILED
import com.cr.models.CrMSDKInfoModel
import dji.v5.common.error.IDJIError
import dji.v5.common.register.DJISDKInitEvent
import dji.v5.manager.SDKManager
import dji.v5.manager.interfaces.SDKManagerCallback
import dji.v5.utils.common.ContextUtil
import dji.v5.utils.common.StringUtils


/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/7 15:21
 * 描述：
 */
class CrMSDKRegisterVM:CrViewModel() {
    // define: 2023/3/9 注册状态
    var registerInfo = MutableLiveData<CrMSDKRegister>()

    // define: 2023/3/29 MSDK信息模型
    var msdkInfoModel: CrMSDKInfoModel = CrMSDKInfoModel()

    /**
     * 初始化
     */
    init {
        registerInfo.value = CrMSDKRegister(REGISTER_FAILED)
        registerInfo.value?.isRegister = false
    }

    /**
     * 更新注册信息值
     */
    fun refreshRegisterInfo(){
        registerInfo.postValue(registerInfo.value)
    }


    /**
     * App注册
     * @param context Context 上下文
     * @param callback SDKManagerCallback 注册回调
     */
    fun registerApp(context:Context,callback: SDKManagerCallback){
        SDKManager.getInstance().init(context,object :SDKManagerCallback{
            // TODO: 注册成功 
            override fun onRegisterSuccess() {
                registerInfo.value?.registerInfo = msdkInfoModel.getSDKVersion();//StringUtils.getResStr(ContextUtil.getContext(), R.string.registered)
                registerInfo.value?.isRegister = true;
                refreshRegisterInfo()
                callback.onRegisterSuccess()
            }

            // TODO: 注册失败 
            override fun onRegisterFailure(error: IDJIError?) {
                registerInfo.value?.registerInfo = StringUtils.getResStr(ContextUtil.getContext(), R.string.unregistered)
                registerInfo.value?.isRegister = false;
                refreshRegisterInfo()
                callback.onRegisterFailure(error)
            }

            // TODO: 有产品失去链接 
            override fun onProductDisconnect(productId: Int) {
                callback.onProductDisconnect(productId)
            }

            // TODO: 有产品链接 
            override fun onProductConnect(productId: Int) {
                callback.onProductConnect(productId)
            }

            // TODO: 产品变更 
            override fun onProductChanged(productId: Int) {
                callback.onProductChanged(productId)
            }

            // TODO: SDK初始化进度回调 
            override fun onInitProcess(event: DJISDKInitEvent?, totalProcess: Int) {
                callback.onInitProcess(event,totalProcess)
                if(event == DJISDKInitEvent.INITIALIZE_COMPLETE){
                    // TODO: 注册App
                    SDKManager.getInstance().registerApp()
                }
            }

            // TODO: 数据下载回调
            override fun onDatabaseDownloadProgress(current: Long, total: Long) {
                callback.onDatabaseDownloadProgress(current,total)
            }

        })
    }
}