package com.example.makcar

import android.app.Application
import android.content.Context
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import dji.sdk.keyvalue.key.FlightControllerKey
import dji.sdk.keyvalue.key.KeyTools
import dji.v5.common.callback.CommonCallbacks
import dji.v5.common.error.IDJIError
import dji.v5.common.register.DJISDKInitEvent
import dji.v5.manager.KeyManager
import dji.v5.manager.SDKManager
import dji.v5.manager.interfaces.SDKManagerCallback
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import java.nio.channels.Channel


class MyApplication : Application() {

    private val TAG = this::class.simpleName

    /* 一次性状态：注册成功 && 飞控已连 */
    private val _ready = MutableStateFlow(false)
    val ready = _ready.asStateFlow()   // 只读

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        // 在调用 install 前，请勿调用任何 MSDK 相关接口
        // MSDK v5.10.0 之前的版本请使用 com.secneo.sdk.Helper.install(this)
        com.cySdkyc.clx.Helper.install(this)
    }



    override fun onCreate() {
        super.onCreate()
        // 初始化 MSDK，建议初始化逻辑放在 Application 中，当然也可以根据自己的需要放在任意地方。
        SDKManager.getInstance().init(this,object:SDKManagerCallback{
            override fun onInitProcess(event: DJISDKInitEvent?, totalProcess: Int) {
                Log.i(TAG, "onInitProcess: ")
                if (event == DJISDKInitEvent.INITIALIZE_COMPLETE) {
                    SDKManager.getInstance().registerApp()
                }
            }
            override fun onRegisterSuccess() {
                Log.i(TAG, "onRegisterSuccess: ")

                //飞控是否连接
                KeyManager.getInstance().listen(KeyTools.createKey(FlightControllerKey.KeyConnection),this,object: CommonCallbacks.KeyListener<Boolean>{
                    override fun onValueChange(p0: Boolean?, p1: Boolean?) {
                        if (p1 == true && !_ready.value) {
                            Log.d(TAG, "飞控已连接 → 准备就绪")
                            _ready.value = true   // 只发一次
                        }
                    }
                })


            }
            override fun onRegisterFailure(error: IDJIError?) {
                Log.i(TAG, "onRegisterFailure: ")
            }
            override fun onProductConnect(productId: Int) {
                Log.i(TAG, "onProductConnect: ")
            }
            override fun onProductDisconnect(productId: Int) {
                Log.i(TAG, "onProductDisconnect: ")
            }
            override fun onProductChanged(productId: Int)
            {
                Log.i(TAG, "onProductChanged: ")
            }
            override fun onDatabaseDownloadProgress(current: Long, total: Long) {
                Log.i(TAG, "onDatabaseDownloadProgress: ${current/total}")
            }
        })
    }
}