package com.multictrl.modules.business.service.impl;

import cn.hutool.json.JSONObject;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.exception.RenException;
import com.multictrl.common.utils.CacheUtils;
import com.multictrl.modules.business.dto.remote.control.LightFineTuningSet;
import com.multictrl.modules.business.service.DJIBaseService;
import com.multictrl.modules.business.service.RemoteService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 远程控制
 *
 * @author Sdy
 * @since 1.0.0 2026/5/21
 */
@Service
@RequiredArgsConstructor
public class RemoteServiceImpl implements RemoteService {
    private final DJIBaseService djiBaseService;

    @Override
    public String drcCameraNightModeSet(String dockSn, Integer mode) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("mode", mode);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_night_mode_set", data);
    }

    @Override
    public String drcCameraDenoiseLevelSet(String dockSn, Integer level) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("level", level);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_denoise_level_set", data);
    }

    @Override
    public String drcCameraNightVisionEnable(String dockSn, Boolean enable) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("enable", enable);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_night_vision_enable", data);
    }

    @Override
    public String drcInfraredFillLightEnable(String dockSn, Boolean enable) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("enable", enable);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_infrared_fill_light_enable", data);
    }

    @Override
    public String drcInfraredFillLightEnable(String dockSn, Integer brightness) {
        JSONObject data = getDataIncludeLightIndex(dockSn);
        data.set("group", 0);//{"0":"主探照灯"}
        data.set("brightness", brightness);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_light_brightness_set", data);
    }

    @Override
    public String drcLightModeSet(String dockSn, Integer mode) {
        JSONObject data = getDataIncludeLightIndex(dockSn);
        data.set("group", 0);//{"0":"主探照灯"}
        data.set("mode", mode);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_light_mode_set", data);
    }

    @Override
    public String drcLightFineTuningSet(String dockSn, LightFineTuningSet lightFineTuningSet) {
        JSONObject data = getDataIncludeLightIndex(dockSn);
        data.set("position", lightFineTuningSet.getPosition());
        data.set("value", lightFineTuningSet.getValue());
        data.set("saved", lightFineTuningSet.getSaved());
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_light_fine_tuning_set", data);
    }

    @Override
    public String drcLightCalibration(String dockSn) {
        JSONObject data = getDataIncludeLightIndex(dockSn);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_light_calibration", data);
    }

    @Override
    public String drcNightLightsStateSet(String dockSn, Integer state) {
        JSONObject data = new JSONObject();
        data.set("night_lights_state", state);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_night_lights_state_set", data);
    }

    @Override
    public String drcStealthStateSet(String dockSn, Integer state) {
        JSONObject data = new JSONObject();
        data.set("stealth_state", state);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_stealth_state_set", data);
    }

    @Override
    public String drcCameraApertureValueSet(String dockSn, Integer value) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("camera_type", CacheUtils.get(BusinessConstant.UAV_VIDEO_TYPE + dockSn));
        data.set("aperture_value", value);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_aperture_value_set", data);
    }

    @Override
    public String drcCameraShutterSet(String dockSn, Integer value) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("camera_type", CacheUtils.get(BusinessConstant.UAV_VIDEO_TYPE + dockSn));
        data.set("shutter_value", value);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_shutter_set", data);
    }

    @Override
    public String drcCameraIsoSet(String dockSn, Integer value) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("camera_type", CacheUtils.get(BusinessConstant.UAV_VIDEO_TYPE + dockSn));
        data.set("iso_value", value);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_iso_set", data);
    }

    @Override
    public String drcCameraMechanicalShutterSet(String dockSn, Integer state) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("camera_type", CacheUtils.get(BusinessConstant.UAV_VIDEO_TYPE + dockSn));
        data.set("mechanical_shutter_state", state);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_mechanical_shutter_set", data);
    }

    @Override
    public String drcCameraDewarpingSet(String dockSn, Integer state) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("camera_type", CacheUtils.get(BusinessConstant.UAV_VIDEO_TYPE + dockSn));
        data.set("dewarping_state", state);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_dewarping_set", data);
    }

    @Override
    public String drcCameraPhotoFormatSet(String dockSn, Integer format) {
        JSONObject data = getDataIncludeCameraIndex(dockSn);
        data.set("photo_format", format);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_camera_photo_format_set", data);
    }

    //获取包含index的data
    private JSONObject getDataIncludeCameraIndex(String dockSn) {
        JSONObject data = new JSONObject();
        Object cameraIndex = CacheUtils.get(BusinessConstant.UAV_CAMERA_INDEX + dockSn);
        if (cameraIndex != null) {
            data.set("payload_index", cameraIndex);
        } else {
            throw new RenException(ErrorCode.UAV_CAMERA_INDEX_NOT_EXIST);
        }
        return data;
    }

    //获取包含index的data
    private JSONObject getDataIncludeLightIndex(String dockSn) {
        JSONObject data = new JSONObject();
        Object lightIndex = CacheUtils.get(BusinessConstant.UAV_LIGHT_INDEX + dockSn);
        if (lightIndex != null) {
            data.set("psdk_index", lightIndex);
        } else {
            throw new RenException(ErrorCode.UAV_LIGHT_INDEX_NOT_EXIST);
        }
        return data;
    }
}
