package com.multictrl.modules.business.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.ValidatorUtils;
import com.multictrl.modules.business.dto.remote.control.LightFineTuningSet;
import com.multictrl.modules.business.service.DJIBaseService;
import com.multictrl.modules.business.service.RemoteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * 远程控制
 *
 * @author Sdy
 * @since 1.0.0 2026/5/21
 */
@RestController
@RequestMapping("business/remote")
@Tag(name = "远程控制", description = "远程控制是开启DRC后执行的特定通道命令，提高命令执行速度")
@ApiOrder(11)
@RequiredArgsConstructor
public class RemoteController {
    private final RemoteService remoteService;
    private final DJIBaseService djiBaseService;

    @LogOperation("夜景模式设置")
    @PostMapping("/drcCameraNightModeSet/{dockSn}")
    @Operation(summary = "夜景模式设置")
    @RequiresPermissions("bus:remote:drcCameraNightModeSet")
    public Result<Object> drcCameraNightModeSet(@PathVariable String dockSn,
                                                @Parameter(name = "mode", description = "{0:关闭,1:开启,2:自动(自动模式根据进光量自动开启)}")
                                                @RequestParam Integer mode) {

        return new Result<>().ok(remoteService.drcCameraNightModeSet(dockSn, mode));
    }

    @LogOperation("降噪等级设置")
    @PostMapping("/drcCameraDenoiseLevelSet/{dockSn}")
    @Operation(summary = "降噪等级设置")
    @RequiresPermissions("bus:remote:drcCameraDenoiseLevelSet")
    public Result<Object> drcCameraDenoiseLevelSet(@PathVariable String dockSn,
                                                   @Parameter(name = "level", description = "{2:增强降噪 15fps,3:超强降噪 5fps}(仅手动开启夜景模式后生效)")
                                                   @RequestParam Integer level) {

        return new Result<>().ok(remoteService.drcCameraDenoiseLevelSet(dockSn, level));
    }

    @LogOperation("黑白夜视使能")
    @PostMapping("/drcCameraNightVisionEnable/{dockSn}")
    @Operation(summary = "黑白夜视使能")
    @RequiresPermissions("bus:remote:drcCameraNightVisionEnable")
    public Result<Object> drcCameraNightVisionEnable(@PathVariable String dockSn,
                                                     @Parameter(name = "enable", description = "{false:关闭,true:开启}(仅变焦7x以上生效)")
                                                     @RequestParam Boolean enable) {

        return new Result<>().ok(remoteService.drcCameraNightVisionEnable(dockSn, enable));
    }

    @LogOperation("近红外补光使能")
    @PostMapping("/drcInfraredFillLightEnable/{dockSn}")
    @Operation(summary = "近红外补光使能")
    @RequiresPermissions("bus:remote:drcInfraredFillLightEnable")
    public Result<Object> drcInfraredFillLightEnable(@PathVariable String dockSn,
                                                     @Parameter(name = "enable", description = "{false:关闭,true:开启}(仅变焦7x以上生效)")
                                                     @RequestParam Boolean enable) {

        return new Result<>().ok(remoteService.drcInfraredFillLightEnable(dockSn, enable));
    }

    @LogOperation("强制降落（慎用! 急停可取消）")
    @PostMapping("/drcForceLanding/{dockSn}")
    @Operation(summary = "强制降落", description = "调用后，无论是否有障碍飞行器都会直接降到地面，用 drone_emergency_stop 命令可取消。降落完成后只能人工拾取飞行器，慎用!")
    @RequiresPermissions("bus:remote:drcForceLanding")
    public Result<Object> drcForceLanding(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeDrcAndReturnResult(dockSn, "drc_force_landing"));
    }

    @LogOperation("急停")
    @PostMapping("/droneEmergencyStop/{dockSn}")
    @Operation(summary = "急停", description = "可取消强制降落")
    @RequiresPermissions("bus:remote:droneEmergencyStop")
    public Result<Object> droneEmergencyStop(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeDrcAndReturnResult(dockSn, "drone_emergency_stop"));
    }

    @LogOperation("紧急降落（慎用! 急停可取消）")
    @PostMapping("/drcEmergencyLanding/{dockSn}")
    @Operation(summary = "紧急降落", description = "调用后，飞行器会开始降落，在开启避障时可能会因触发避障而中止。用 drone_emergency_stop 命令可取消。降落完成后只能人工拾取飞行器，慎用!")
    @RequiresPermissions("bus:remote:drcEmergencyLanding")
    public Result<Object> drcEmergencyLanding(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeDrcAndReturnResult(dockSn, "drc_emergency_landing"));
    }

    @LogOperation("探照灯—亮度设置")
    @PostMapping("/drcLightBrightnessSet/{dockSn}")
    @Operation(summary = "探照灯—亮度设置")
    @RequiresPermissions("bus:remote:drcLightBrightnessSet")
    public Result<Object> drcLightBrightnessSet(@PathVariable String dockSn,
                                                @Parameter(name = "brightness", description = "亮度 {max:100,min:1}")
                                                @RequestParam Integer brightness) {

        return new Result<>().ok(remoteService.drcInfraredFillLightEnable(dockSn, brightness));
    }

    @LogOperation("探照灯—模式设置")
    @PostMapping("/drcLightModeSet/{dockSn}")
    @Operation(summary = "探照灯—模式设置")
    @RequiresPermissions("bus:remote:drcLightModeSet")
    public Result<Object> drcLightModeSet(@PathVariable String dockSn,
                                          @Parameter(name = "mode", description = "模式{ 0: 关闭, 1: 常亮, 2: 爆闪, 3: 快速爆闪, 4: 交替爆闪 }")
                                          @RequestParam Integer mode) {

        return new Result<>().ok(remoteService.drcLightModeSet(dockSn, mode));
    }

    @LogOperation("探照灯—左右角度微调")
    @PostMapping("/drcLightFineTuningSet/{dockSn}")
    @Operation(summary = "探照灯—左右角度微调")
    @RequiresPermissions("bus:remote:drcLightFineTuningSet")
    public Result<Object> drcLightFineTuningSet(@PathVariable String dockSn, @RequestBody LightFineTuningSet lightFineTuningSet) {
        ValidatorUtils.validateEntity(lightFineTuningSet);

        return new Result<>().ok(remoteService.drcLightFineTuningSet(dockSn, lightFineTuningSet));
    }

    @LogOperation("探照灯—云台校准")
    @PostMapping("/drcLightCalibration/{dockSn}")
    @Operation(summary = "探照灯—云台校准")
    @RequiresPermissions("bus:remote:drcLightCalibration")
    public Result<Object> drcLightCalibration(@PathVariable String dockSn) {

        return new Result<>().ok(remoteService.drcLightCalibration(dockSn));
    }

    @LogOperation("DRC初始状态订阅")
    @PostMapping("/drcInitialState/{dockSn}")
    @Operation(summary = "DRC初始状态订阅")
    @RequiresPermissions("bus:remote:drcInitialState")
    public Result<Object> drcInitialState(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeDrcAndReturnResult(dockSn, "drc_initial_state_subscribe"));
    }

    @LogOperation("夜航灯设置")
    @PostMapping("/drcNightLightsStateSet/{dockSn}")
    @Operation(summary = "夜航灯设置")
    @RequiresPermissions("bus:remote:drcNightLightsStateSet")
    public Result<Object> drcNightLightsStateSet(@PathVariable String dockSn,
                                                 @Parameter(name = "state", description = "状态 {0:关闭 1:开启}")
                                                 @RequestParam Integer state) {

        return new Result<>().ok(remoteService.drcLightModeSet(dockSn, state));
    }

    @LogOperation("飞行控制—隐蔽模式")
    @PostMapping("/drcStealthStateSet/{dockSn}")
    @Operation(summary = "飞行控制—隐蔽模式（关闭所有飞机灯光）", description = "设置飞机的隐蔽模式，用于关闭所有飞机灯光")
    @RequiresPermissions("bus:remote:drcStealthStateSet")
    public Result<Object> drcStealthStateSet(@PathVariable String dockSn,
                                             @Parameter(name = "state", description = "隐蔽模式 {0:关闭 1:开启}")
                                             @RequestParam Integer state) {

        return new Result<>().ok(remoteService.drcStealthStateSet(dockSn, state));
    }

    @LogOperation("负载控制—相机光圈")
    @PostMapping("/drcCameraApertureValueSet/{dockSn}")
    @Operation(summary = "负载控制—相机光圈")
    @RequiresPermissions("bus:remote:drcCameraApertureValueSet")
    public Result<Object> drcCameraApertureValueSet(@PathVariable String dockSn,
                                                    @Parameter(name = "value", description = "光圈值 {0:F_AUTO 50:F_AUTO ...}")
                                                    @RequestParam Integer value) {

        return new Result<>().ok(remoteService.drcCameraApertureValueSet(dockSn, value));
    }

    @LogOperation("负载控制—相机快门")
    @PostMapping("/drcCameraShutterSet/{dockSn}")
    @Operation(summary = "负载控制—相机快门")
    @RequiresPermissions("bus:remote:drcCameraShutterSet")
    public Result<Object> drcCameraShutterSet(@PathVariable String dockSn,
                                              @Parameter(name = "value", description = "快门设置 {0:1/8000s 1:1/6400s ...}")
                                              @RequestParam Integer value) {

        return new Result<>().ok(remoteService.drcCameraShutterSet(dockSn, value));
    }

    @LogOperation("负载控制—ISO设置")
    @PostMapping("/drcCameraIsoSet/{dockSn}")
    @Operation(summary = "负载控制—ISO设置")
    @RequiresPermissions("bus:remote:drcCameraIsoSet")
    public Result<Object> drcCameraIsoSet(@PathVariable String dockSn,
                                          @Parameter(name = "value", description = "ISO值 {0:ISO_AUTO 1:ISO_50 ...}")
                                          @RequestParam Integer value) {

        return new Result<>().ok(remoteService.drcCameraIsoSet(dockSn, value));
    }

    @LogOperation("负载控制—机械快门")
    @PostMapping("/drcCameraMechanicalShutterSet/{dockSn}")
    @Operation(summary = "负载控制—机械快门", description = "支持用户手动关闭机械快门，提高设备作业寿命，Matrice 3D广角模式下机械快门开关")
    @RequiresPermissions("bus:remote:drcCameraMechanicalShutterSet")
    public Result<Object> drcCameraMechanicalShutterSet(@PathVariable String dockSn,
                                                        @Parameter(name = "state", description = "是否使能机械快门 {0:关闭 1:开启}")
                                                        @RequestParam Integer state) {

        return new Result<>().ok(remoteService.drcCameraMechanicalShutterSet(dockSn, state));
    }

    @LogOperation("负载控制—镜头去畸变")
    @PostMapping("/drcCameraDewarpingSet/{dockSn}")
    @Operation(summary = "负载控制—镜头去畸变", description = "能让用户的广角视角不再有暗角，Matrice 3D广角模式的镜头去畸变")
    @RequiresPermissions("bus:remote:drcCameraDewarpingSet")
    public Result<Object> drcCameraDewarpingSet(@PathVariable String dockSn,
                                                @Parameter(name = "state", description = "是否使能去畸变 {0:关闭 1:开启}")
                                                @RequestParam Integer state) {

        return new Result<>().ok(remoteService.drcCameraDewarpingSet(dockSn, state));
    }

    @LogOperation("红外照片格式设置")
    @PostMapping("/drcCameraPhotoFormatSet/{dockSn}")
    @Operation(summary = "红外照片格式设置", description = "用于设置红外照片格式，需要直播镜头先切换为红外才可有效使用")
    @RequiresPermissions("bus:remote:drcCameraPhotoFormatSet")
    public Result<Object> drcCameraPhotoFormatSet(@PathVariable String dockSn,
                                                  @Parameter(name = "format", description = "照片格式 {7:RJPEG 16:DLT664}")
                                                  @RequestParam Integer format) {

        return new Result<>().ok(remoteService.drcCameraPhotoFormatSet(dockSn, format));
    }
}
