package com.cr.common

import dji.sdk.keyvalue.key.DJIActionKeyInfo
import dji.sdk.keyvalue.key.DJIKey
import dji.sdk.keyvalue.key.DJIKey.ActionKey
import dji.sdk.keyvalue.key.DJIKeyInfo
import dji.sdk.keyvalue.key.KeyTools
import dji.sdk.keyvalue.value.common.EmptyMsg
import dji.v5.common.callback.CommonCallbacks
import dji.v5.common.error.IDJIError
import dji.v5.manager.KeyManager

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/8/5 08:51
 * 描述：大疆动作或参数执行管理
 */
class CrKeyManager {
    /**
     * 完成回调
     */
    interface ICompletion<T> {
        /**
         * 成功回调
         */
        fun onSuccess(t:T)

        /**
         * 错误回调
         * @param error String 错误消息
         */
        fun onFailure(error: String)
    }

    /**
     * 静态方法
     */
    companion object {
        /**
         * 执行不带参的动作
         * @param keyInfo DJIActionKeyInfo<P, R>
         * @param callback ICompletion?
         */
        fun <P,R> performAction(keyInfo:DJIActionKeyInfo<P,R>, callback: ICompletion<R>?) {
            var key:ActionKey<P,R> = KeyTools.createKey(keyInfo)
            KeyManager.getInstance().performAction(key,
                object : CommonCallbacks.CompletionCallbackWithParam<R> {
                    // todo: 2023/8/5 执行成功
                    override fun onSuccess(t: R) {
                        callback?.let {
                            it.onSuccess(t)
                        }
                    }

                    // todo: 2023/8/5 执行错误
                    override fun onFailure(error: IDJIError) {
                        callback?.let {
                            it.onFailure(error.description())
                        }
                    }
                })
        }

        /**
         * 设置参数
         * @param keyInfo DJIKeyInfo<P> DJI KEY
         * @param param P 参数
         * @param callback ICompletion<String>? 回调
         */
        fun <P> setValue(keyInfo:DJIKeyInfo<P>,param:P,callback:ICompletion<String>?){
            // todo: 2023/8/5 创建Key
            var key = KeyTools.createKey(keyInfo)
            // todo: 2023/8/5 设置参数
            KeyManager.getInstance().setValue(key,param,object:CommonCallbacks.CompletionCallback{
                // todo: 2023/8/5 设置成功
                override fun onSuccess() {
                    callback?.let {
                        it.onSuccess("成功")
                    }
                }

                // todo: 2023/8/5 设置失败
                override fun onFailure(error: IDJIError) {
                    callback?.let {
                        it.onFailure(error.description())
                    }
                }

            })
        }

        /**
         * 异步从硬件获取参数
         * @param keyInfo DJIKeyInfo<P> DJI KEY
         * @param callback ICompletion<P>? 回调
         */
        fun <P> getValue(keyInfo:DJIKeyInfo<P>,callback:ICompletion<P>?){
            var key = KeyTools.createKey(keyInfo)
            KeyManager.getInstance().getValue(key,object:CommonCallbacks.CompletionCallbackWithParam<P>{
                // todo: 2023/8/5 获取成功
                override fun onSuccess(t: P) {
                    callback?.let {
                        it.onSuccess(t)
                    }
                }

                // todo: 2023/8/5 获取失败
                override fun onFailure(error: IDJIError) {
                    callback?.let {
                        it.onFailure(error.description())
                    }
                }

            })
        }
    }
}