package com.multictrl.modules.business.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.ValidatorUtils;
import com.multictrl.modules.business.dto.remote.debug.ESIMActivate;
import com.multictrl.modules.business.dto.remote.debug.ESIMOperatorSwitch;
import com.multictrl.modules.business.dto.remote.debug.RtkCalibration;
import com.multictrl.modules.business.dto.remote.debug.SIMSlotSwitch;
import com.multictrl.modules.business.service.DJIBaseService;
import com.multictrl.modules.business.service.DebugService;
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/4/24
 */
@RestController
@RequestMapping("business/debug")
@Tag(name = "远程调试", description = "远程调试为在调试的作业流中实现无人值守，即让作业人员无需到现场，在云端就可以下发命令到设备端，" +
        "进行设备的远程排障。远程调试命令可分为命令（cmd）和任务（job）。命令（cmd）一般指命令下发后，设备能即刻回复的行为，而任务（job）" +
        "为任务下发后，设备需要持续动作的行为。")
@ApiOrder(3)
@RequiredArgsConstructor
public class DebugController {
    private final DJIBaseService djiBaseService;
    private final DebugService debugService;

    @Operation(summary = "调试模式开启")
    @PostMapping("/debugModeOpen/{dockSn}")
    @LogOperation("调试模式开启")
    @RequiresPermissions("bus:debug:operation")
    public Result<Object> debugModeOpen(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "debug_mode_open"));
    }

    @Operation(summary = "调试模式关闭")
    @PostMapping("/debugModeClose/{dockSn}")
    @LogOperation("调试模式关闭")
    @RequiresPermissions("bus:debug:operation")
    public Result<Object> debugModeClose(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "debug_mode_close"));
    }

    @Operation(summary = "强制关舱盖",
            description = "机场“设备属性”推送的 drone_in_dock 字段值为 0 时，且通过机场摄像头观看确认飞行器不在舱内后，可调用该指令强制关闭舱盖。否则可能导致桨叶被夹。")
    @PostMapping("/coverForceClose/{dockSn}")
    @LogOperation("强制关舱盖")
    @RequiresPermissions("bus:debug:operation")
    public Result<Object> coverForceClose(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "cover_force_close"));
    }

    @Operation(summary = "打开舱盖")
    @PostMapping("/coverOpen/{dockSn}")
    @LogOperation("打开舱盖")
    @RequiresPermissions("bus:debug:operation")
    public Result<Object> coverOpen(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "cover_open"));
    }

    @Operation(summary = "关闭舱盖")
    @PostMapping("/coverClose/{dockSn}")
    @LogOperation("关闭舱盖")
    @RequiresPermissions("bus:debug:operation")
    public Result<Object> coverClose(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "cover_close"));
    }

    @Operation(summary = "飞行器开机")
    @PostMapping("/droneOpen/{dockSn}")
    @LogOperation("飞行器开机")
    @RequiresPermissions("bus:debug:operation")
    public Result<Object> droneOpen(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "drone_open"));
    }

    @Operation(summary = "飞行器关机")
    @PostMapping("/droneClose/{dockSn}")
    @LogOperation("飞行器关机")
    @RequiresPermissions("bus:debug:operation")
    public Result<Object> droneClose(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "drone_close"));
    }

    @Operation(summary = "eSIM的运营商切换")
    @PostMapping("/eSIMOperatorSwitch/{dockSn}")
    @LogOperation("eSIM的运营商切换")
    @RequiresPermissions("bus:debug:eSIMOperatorSwitch")
    public Result<Object> eSIMOperatorSwitch(@PathVariable String dockSn,
                                             @RequestBody ESIMOperatorSwitch eSimOperatorSwitch) {
        ValidatorUtils.validateEntity(eSimOperatorSwitch);
        return new Result<>().ok(debugService.eSIMOperatorSwitch(dockSn, eSimOperatorSwitch));
    }

    @Operation(summary = "eSIM和SIM切换")
    @PostMapping("/simSlotSwitch/{dockSn}")
    @LogOperation("eSIM和SIM切换")
    @RequiresPermissions("bus:debug:simSlotSwitch")
    public Result<Object> simSlotSwitch(@PathVariable String dockSn,
                                        @RequestBody SIMSlotSwitch simSlotSwitch) {
        ValidatorUtils.validateEntity(simSlotSwitch);
        return new Result<>().ok(debugService.simSlotSwitch(dockSn, simSlotSwitch));
    }

    @Operation(summary = "eSIM激活")
    @PostMapping("/eSimActivate/{dockSn}")
    @LogOperation("eSIM激活")
    @RequiresPermissions("bus:debug:eSimActivate")
    public Result<Object> eSimActivate(@PathVariable String dockSn,
                                       @RequestBody ESIMActivate esimActivate) {
        ValidatorUtils.validateEntity(esimActivate);
        return new Result<>().ok(debugService.eSimActivate(dockSn, esimActivate));
    }

    @Operation(summary = "增强图传开关")
    @PostMapping("/sdrWorkModeSwitch/{dockSn}")
    @LogOperation("增强图传开关")
    @RequiresPermissions("bus:debug:sdrWorkModeSwitch")
    public Result<Object> sdrWorkModeSwitch(@PathVariable String dockSn,
                                            @Parameter(description = "图传模式 {0:仅使用 SDR,1:4G 增强模式},在 4G 增强模式下，SDR 与 4G 会同时使用")
                                            @RequestParam Integer workMode) {
        if (workMode != 0 && workMode != 1) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }
        return new Result<>().ok(debugService.sdrWorkModeSwitch(dockSn, workMode));
    }

    @Operation(summary = "关闭充电")
    @PostMapping("/chargeClose/{dockSn}")
    @LogOperation("关闭充电")
    @RequiresPermissions("bus:debug:chargeClose")
    public Result<Object> chargeClose(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "charge_close"));
    }

    @Operation(summary = "打开充电")
    @PostMapping("/chargeOpen/{dockSn}")
    @LogOperation("打开充电")
    @RequiresPermissions("bus:debug:chargeOpen")
    public Result<Object> chargeOpen(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "charge_open"));
    }

    @Operation(summary = "飞行器数据格式化")
    @PostMapping("/droneFormat/{dockSn}")
    @LogOperation("飞行器数据格式化")
    @RequiresPermissions("bus:debug:droneFormat")
    public Result<Object> droneFormat(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "drone_format"));
    }

    @Operation(summary = "机场数据格式化")
    @PostMapping("/deviceFormat/{dockSn}")
    @LogOperation("机场数据格式化")
    @RequiresPermissions("bus:debug:deviceFormat")
    public Result<Object> deviceFormat(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "device_format"));
    }

    @Operation(summary = "机场重启")
    @PostMapping("/deviceReboot/{dockSn}")
    @LogOperation("机场重启")
    @RequiresPermissions("bus:debug:deviceReboot")
    public Result<Object> deviceReboot(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "device_reboot"));
    }

    @Operation(summary = "电池运行模式切换")
    @PostMapping("/batteryStoreModeSwitch/{dockSn}")
    @LogOperation("电池运行模式切换")
    @RequiresPermissions("bus:debug:batteryStoreModeSwitch")
    public Result<Object> batteryStoreModeSwitch(@PathVariable String dockSn,
                                                 @Parameter(description = "操作 {1:计划模式,2:待命模式}")
                                                 @RequestParam Integer action) {
        if (action != 1 && action != 2) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }
        return new Result<>().ok(debugService.batteryStoreModeSwitch(dockSn, action));
    }

    @Operation(summary = "机场声光报警开关")
    @PostMapping("/alarmStateSwitch/{dockSn}")
    @LogOperation("机场声光报警开关")
    @RequiresPermissions("bus:debug:alarmStateSwitch")
    public Result<Object> alarmStateSwitch(@PathVariable String dockSn,
                                           @Parameter(description = "操作 {0:关闭,1:开启}")
                                           @RequestParam Integer action) {
        if (action != 0 && action != 1) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }
        return new Result<>().ok(debugService.alarmStateSwitch(dockSn, action));
    }

    @Operation(summary = "机场空调工作模式切换")
    @PostMapping("/airConditionerModeSwitch/{dockSn}")
    @LogOperation("机场空调工作模式切换")
    @RequiresPermissions("bus:debug:airConditionerModeSwitch")
    public Result<Object> airConditionerModeSwitch(@PathVariable String dockSn,
                                                   @Parameter(description = "操作 {0:使机场空调进入空闲模式(关闭制冷、制热或除湿)," +
                                                           "1:使机场空调进入制冷模式,2:使机场空调进入制热模式," +
                                                           "3:使机场空调进入除湿模式(除湿包含制冷除湿及制热除湿，由设备端根据自身所处情况自动化处理，无需用户介入}")
                                                   @RequestParam Integer action) {
        if (action != 0 && action != 1 && action != 2 && action != 3) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }
        return new Result<>().ok(debugService.airConditionerModeSwitch(dockSn, action));
    }

    @Operation(summary = "电池保养状态切换")
    @PostMapping("/batteryMaintenanceSwitch/{dockSn}")
    @LogOperation("电池保养状态切换")
    @RequiresPermissions("bus:debug:batteryMaintenanceSwitch")
    public Result<Object> batteryMaintenanceSwitch(@PathVariable String dockSn,
                                                   @Parameter(description = "操作 {0:关闭,1:开启}")
                                                   @RequestParam Integer action) {
        if (action != 0 && action != 1) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }
        return new Result<>().ok(debugService.batteryMaintenanceSwitch(dockSn, action));
    }

    @Operation(summary = "关闭补光灯")
    @PostMapping("/supplementLightClose/{dockSn}")
    @LogOperation("关闭补光灯")
    @RequiresPermissions("bus:debug:supplementLightClose")
    public Result<Object> supplementLightClose(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "supplement_light_close"));
    }

    @Operation(summary = "打开补光灯")
    @PostMapping("/supplementLightOpen/{dockSn}")
    @LogOperation("打开补光灯")
    @RequiresPermissions("bus:debug:supplementLightOpen")
    public Result<Object> supplementLightOpen(@PathVariable String dockSn) {
        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "supplement_light_open"));
    }

    @Operation(summary = "一键标定")
    @PostMapping("/rtkCalibration/{dockSn}")
    @LogOperation("一键标定")
    @RequiresPermissions("bus:debug:rtkCalibration")
    public Result<Object> rtkCalibration(@PathVariable String dockSn,
                                         @RequestBody RtkCalibration rtkCalibration) {
        ValidatorUtils.validateEntity(rtkCalibration);
        return new Result<>().ok(debugService.rtkCalibration(dockSn, rtkCalibration));
    }
}
