package com.aros.apron.manager;

import androidx.annotation.NonNull;

import com.aros.apron.entity.PayloadInfo;
import com.aros.apron.tools.LogUtil;

import java.util.Map;

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.manager.KeyManager;
import dji.v5.manager.aircraft.payload.PayloadCenter;
import dji.v5.manager.aircraft.payload.PayloadIndexType;
import dji.v5.manager.aircraft.payload.data.PayloadWidgetInfo;
import dji.v5.manager.aircraft.payload.listener.PayloadBasicInfoListener;
import dji.v5.manager.aircraft.payload.listener.PayloadDataListener;
import dji.v5.manager.aircraft.payload.listener.PayloadWidgetInfoListener;
import dji.v5.manager.interfaces.IPayloadManager;

public class PlayLoadManager {

    private String TAG = this.getClass().getSimpleName();

    private PlayLoadManager() {
    }

    public static PlayLoadManager getInstance() {
        return PlayLoadManagerHolder.INSTANCE;
    }

    private static class PlayLoadManagerHolder {
        private static final PlayLoadManager INSTANCE = new PlayLoadManager();
    }

    public void init() {
        Boolean isConnect = KeyManager.getInstance().getValue(KeyTools.createKey(FlightControllerKey.KeyConnection));
        if (isConnect != null && isConnect) {
            Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
            if (payloadManager != null) {
                IPayloadManager iPayloadManager = payloadManager.get(PayloadIndexType.PORT_1);
                if (iPayloadManager != null) {


                    iPayloadManager.addPayloadWidgetInfoListener(new PayloadWidgetInfoListener() {
                        @Override
                        public void onPayloadWidgetInfoUpdate(PayloadWidgetInfo info) {
                            LogUtil.log(TAG,"负载信息"+info.toString());
                        }
                    });

                    iPayloadManager.pullWidgetInfoFromPayload(new CommonCallbacks.CompletionCallback() {
                        @Override
                        public void onSuccess() {
                            LogUtil.log(TAG,"拉取成功");
                        }

                        @Override
                        public void onFailure(@NonNull IDJIError idjiError) {
                            LogUtil.log(TAG,"拉取成功");
                        }
                    });




                } else {
                    LogUtil.log(TAG, "监听EXTERNAL PSDK数据失败:设备未连接");
                }


            }
        }

    }
}
