package com.multictrl.modules.business.q20.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.utils.Result;
import com.multictrl.modules.business.q20.dto.*;
import com.multictrl.modules.business.q20.service.Q20CommandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * Q20控制指令接口
 * topic: thing/device/{device_sn}/services (下行)
 * 回复: thing/device/{device_sn}/services_reply (上行)
 *
 * @author 938693313@qq.com
 * @since 1.0.0 2026/5/20
 */
@RestController
@RequestMapping("business/q20/cmd")
@Tag(name = "Q20控制指令")
@ApiOrder(12)
@RequiredArgsConstructor
public class Q20CommandController {

    private final Q20CommandService q20CommandService;

    // ==================== 飞行控制 ====================

    @PostMapping("/takeoff/{deviceSn}")
    @LogOperation("Q20起飞")
    @Operation(summary = "起飞: mode=0绝对高度/1相对高度")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> takeoff(@PathVariable String deviceSn, @RequestBody Q20TakeoffDTO dto) {
        return new Result<>().ok(q20CommandService.takeoff(deviceSn, dto));
    }

    @PostMapping("/land/{deviceSn}")
    @LogOperation("Q20降落")
    @Operation(summary = "降落: mode=0直接降落/1精准降落")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> land(@PathVariable String deviceSn, @RequestBody Q20LandDTO dto) {
        return new Result<>().ok(q20CommandService.land(deviceSn, dto));
    }

    @PostMapping("/stop/{deviceSn}")
    @LogOperation("Q20紧急悬停")
    @Operation(summary = "紧急悬停(value固定为1)")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> stop(@PathVariable String deviceSn) {
        return new Result<>().ok(q20CommandService.stop(deviceSn));
    }

    @PostMapping("/goHome/{deviceSn}")
    @LogOperation("Q20返航")
    @Operation(summary = "返航: mode=1安全高度/2直线飞行/3原路返回")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> goHome(@PathVariable String deviceSn, @RequestBody Q20GoHomeDTO dto) {
        return new Result<>().ok(q20CommandService.goHome(deviceSn, dto));
    }

    @PostMapping("/setHome/{deviceSn}")
    @LogOperation("Q20设置返航点")
    @Operation(summary = "设置返航点坐标")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> setHome(@PathVariable String deviceSn, @RequestBody Q20SetHomeDTO dto) {
        return new Result<>().ok(q20CommandService.setHome(deviceSn, dto));
    }

    @PostMapping("/navigate/{deviceSn}")
    @LogOperation("Q20航点飞行")
    @Operation(summary = "飞往指定坐标: action=noAction/land/precland")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> navigate(@PathVariable String deviceSn, @RequestBody Q20NavigateDTO dto) {
        return new Result<>().ok(q20CommandService.navigate(deviceSn, dto));
    }

    // ==================== 云台相机 ====================

    @PostMapping("/gimbalControl/{deviceSn}")
    @LogOperation("Q20云台控制")
    @Operation(summary = "云台控制: mode=0速度模式/1绝对值模式")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> gimbalControl(@PathVariable String deviceSn, @RequestBody Q20GimbalControlDTO dto) {
        return new Result<>().ok(q20CommandService.gimbalControl(deviceSn, dto));
    }

    @PostMapping("/mountTda/{deviceSn}")
    @LogOperation("Q20挂载TDA数据")
    @Operation(summary = "挂载TDA数据(仅支持deviceID=68的负载)")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> mountTda(@PathVariable String deviceSn, @RequestBody Q20MountTdaDTO dto) {
        return new Result<>().ok(q20CommandService.mountTda(deviceSn, dto));
    }

    @PostMapping("/cameraPhotoTake/{deviceSn}")
    @LogOperation("Q20拍照")
    @Operation(summary = "相机拍照")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> cameraPhotoTake(@PathVariable String deviceSn, @RequestBody Q20CameraDTO dto) {
        return new Result<>().ok(q20CommandService.cameraPhotoTake(deviceSn, dto));
    }

@PostMapping("/cameraRecordingStart/{deviceSn}")
    @LogOperation("Q20开始录像")
    @Operation(summary = "开始录像")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> cameraRecordingStart(@PathVariable String deviceSn, @RequestBody Q20CameraDTO dto) {
        return new Result<>().ok(q20CommandService.cameraRecordingStart(deviceSn, dto));
    }

    @PostMapping("/cameraRecordingStop/{deviceSn}")
    @LogOperation("Q20停止录像")
    @Operation(summary = "停止录像")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> cameraRecordingStop(@PathVariable String deviceSn, @RequestBody Q20CameraDTO dto) {
        return new Result<>().ok(q20CommandService.cameraRecordingStop(deviceSn, dto));
    }

    @PostMapping("/cameraFocalLengthSet/{deviceSn}")
    @LogOperation("Q20设置焦距")
    @Operation(summary = "设置相机焦距: zoom_type=0绝对值/1连续, zoom_mode=0数字/1光学")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> cameraFocalLengthSet(@PathVariable String deviceSn, @RequestBody Q20CameraFocalLengthDTO dto) {
        return new Result<>().ok(q20CommandService.cameraFocalLengthSet(deviceSn, dto));
    }

    // ==================== 直播 ====================

    @PostMapping("/liveStartPush/{deviceSn}")
    @LogOperation("Q20开始直播")
    @Operation(summary = "开始直播推流: url_type=0Agora/1RTMP/2RTSP/3GB28181/4WebRTC")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> liveStartPush(@PathVariable String deviceSn, @RequestBody Q20LiveStartDTO dto) {
        return new Result<>().ok(q20CommandService.liveStartPush(deviceSn, dto));
    }

    @PostMapping("/liveStopPush/{deviceSn}")
    @LogOperation("Q20停止直播")
    @Operation(summary = "停止直播推流")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> liveStopPush(@PathVariable String deviceSn,
                                       @RequestParam int payloadIndex,
                                       @RequestParam int video) {
        return new Result<>().ok(q20CommandService.liveStopPush(deviceSn, payloadIndex, video));
    }

    @PostMapping("/imageSwitch/{deviceSn}")
    @LogOperation("Q20图像切换")
    @Operation(summary = "图像切换(红外/可见光/激光)")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> imageSwitch(@PathVariable String deviceSn, @RequestBody Q20ImageSwitchDTO dto) {
        return new Result<>().ok(q20CommandService.imageSwitch(deviceSn, dto));
    }

    // ==================== 设备控制 ====================

    @PostMapping("/deviceCharge/{deviceSn}")
    @LogOperation("Q20充电控制")
    @Operation(summary = "充电控制: version=1(1.0版本)/2(2.0版本)")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> deviceCharge(@PathVariable String deviceSn,
                                       @RequestParam int value,
                                       @RequestParam int version) {
        return new Result<>().ok(q20CommandService.deviceCharge(deviceSn, value, version));
    }

    @PostMapping("/deviceBoot/{deviceSn}")
    @LogOperation("Q20重启/关机")
    @Operation(summary = "设备控制: value=0关机/1重启/2硬重启/3锁定")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> deviceBoot(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20CommandService.deviceBoot(deviceSn, value));
    }

    @PostMapping("/safetySwitch/{deviceSn}")
    @LogOperation("Q20安全开关")
    @Operation(summary = "安全开关控制")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> safetySwitch(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20CommandService.safetySwitch(deviceSn, value));
    }

    // ==================== 负载 ====================

    @PostMapping("/throwingLock/{deviceSn}")
    @LogOperation("Q20投掷锁控制")
    @Operation(summary = "投掷锁/解锁: value=0解锁/1锁定")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> throwingLock(@PathVariable String deviceSn, @RequestBody Q20ThrowingDTO dto) {
        return new Result<>().ok(q20CommandService.throwingLock(deviceSn, dto));
    }

    @PostMapping("/throwingExecution/{deviceSn}")
    @LogOperation("Q20执行投掷")
    @Operation(summary = "执行投掷动作")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> throwingExecution(@PathVariable String deviceSn, @RequestParam long deviceId) {
        return new Result<>().ok(q20CommandService.throwingExecution(deviceSn, deviceId));
    }

    @PostMapping("/halyarding/{deviceSn}")
    @LogOperation("Q20绞车控制")
    @Operation(summary = "绞车控制")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> halyarding(@PathVariable String deviceSn, @RequestBody Q20HalyardingDTO dto) {
        return new Result<>().ok(q20CommandService.halyarding(deviceSn, dto));
    }

    @PostMapping("/logistics/{deviceSn}")
    @LogOperation("Q20物流控制")
    @Operation(summary = "物流控制")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> logistics(@PathVariable String deviceSn,
                                    @RequestParam int value,
                                    @RequestParam boolean force) {
        return new Result<>().ok(q20CommandService.logistics(deviceSn, value, force));
    }

    // ==================== 虚拟摇杆 ====================

    @PostMapping("/drc/{deviceSn}")
    @Operation(summary = "虚拟摇杆控制(高频指令，不等回复)")
    @RequiresPermissions("bus:q20:cmd")
    public void drc(@PathVariable String deviceSn, @RequestBody Q20DrcDTO dto) {
        q20CommandService.drc(deviceSn, dto);
    }

    // ==================== 安全/降落伞 ====================

    @PostMapping("/setAutoParachute/{deviceSn}")
    @LogOperation("Q20设置自动开伞")
    @Operation(summary = "设置自动开伞开关")
    @RequiresPermissions("bus:q20:parachute")
    public Result<Object> setAutoParachute(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20CommandService.setAutoParachute(deviceSn, value));
    }

    @GetMapping("/getEmergencyStopOtp/{deviceSn}")
    @Operation(summary = "获取紧急停止OTP验证码")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> getEmergencyStopOtp(@PathVariable String deviceSn) {
        return new Result<>().ok(q20CommandService.getEmergencyStopOtp(deviceSn));
    }

    @PostMapping("/emergencyStop/{deviceSn}")
    @LogOperation("Q20紧急停止")
    @Operation(summary = "紧急停止(需先获取OTP)")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> emergencyStop(@PathVariable String deviceSn, @RequestBody Q20EmergencyStopDTO dto) {
        return new Result<>().ok(q20CommandService.emergencyStop(deviceSn, dto));
    }

    @GetMapping("/getParachuteOtp/{deviceSn}")
    @Operation(summary = "获取开伞OTP验证码")
    @RequiresPermissions("bus:q20:parachute")
    public Result<Object> getParachuteOtp(@PathVariable String deviceSn) {
        return new Result<>().ok(q20CommandService.getParachuteOtp(deviceSn));
    }

    @PostMapping("/openParachute/{deviceSn}")
    @LogOperation("Q20开伞")
    @Operation(summary = "开伞(otp=99时为强制开伞)")
    @RequiresPermissions("bus:q20:parachute")
    public Result<Object> openParachute(@PathVariable String deviceSn, @RequestBody Q20OpenParachuteDTO dto) {
        return new Result<>().ok(q20CommandService.openParachute(deviceSn, dto));
    }

    @PostMapping("/confirmParachuteSafety/{deviceSn}")
    @LogOperation("Q20确认开伞安全")
    @Operation(summary = "确认开伞安全")
    @RequiresPermissions("bus:q20:parachute")
    public Result<Object> confirmParachuteSafety(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20CommandService.confirmParachuteSafety(deviceSn, value));
    }

    // ==================== 状态/信息 ====================

    @GetMapping("/queryStatus/{deviceSn}")
    @Operation(summary = "查询飞行状态: query_value=liftoff/arrival等")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> queryStatus(@PathVariable String deviceSn, @RequestBody Q20QueryStatusDTO dto) {
        return new Result<>().ok(q20CommandService.queryStatus(deviceSn, dto));
    }

    @GetMapping("/config/{deviceSn}")
    @Operation(summary = "获取设备配置")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> getConfig(@PathVariable String deviceSn) {
        return new Result<>().ok(q20CommandService.getConfig(deviceSn));
    }

    @PostMapping("/stereoImage/{deviceSn}")
    @LogOperation("Q20立体图像开关")
    @Operation(summary = "立体图像开关: 0=开启/1=关闭")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> stereoImage(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20CommandService.stereoImage(deviceSn, value));
    }

    @GetMapping("/version/{deviceSn}")
    @Operation(summary = "获取设备固件版本")
    @RequiresPermissions("bus:q20:cmd")
    public Result<Object> version(@PathVariable String deviceSn) {
        return new Result<>().ok(q20CommandService.version(deviceSn));
    }

    // ==================== 日志 ====================

    @GetMapping("/logCount/{deviceSn}")
    @Operation(summary = "获取日志数量")
    @RequiresPermissions("bus:q20:log")
    public Result<Object> logCount(@PathVariable String deviceSn) {
        return new Result<>().ok(q20CommandService.logCount(deviceSn));
    }

    @PostMapping("/logList/{deviceSn}")
    @Operation(summary = "获取日志列表")
    @RequiresPermissions("bus:q20:log")
    public Result<Object> logList(@PathVariable String deviceSn, @RequestBody Q20LogListDTO dto) {
        return new Result<>().ok(q20CommandService.logList(deviceSn, dto));
    }

    @PostMapping("/logData/{deviceSn}")
    @LogOperation("Q20上传日志")
    @Operation(summary = "上传日志到OSS")
    @RequiresPermissions("bus:q20:log")
    public Result<Object> logData(@PathVariable String deviceSn, @RequestBody Q20LogDataDTO dto) {
        return new Result<>().ok(q20CommandService.logData(deviceSn, dto));
    }

    // ==================== OTA（暂时停用，先注释） ====================

//    @PostMapping("/otaUpgrade/{deviceSn}")
//    @LogOperation("Q20 OTA升级")
//    @Operation(summary = "固件OTA升级")
//    @RequiresPermissions("bus:q20:ota")
//    public Result<Object> otaUpgrade(@PathVariable String deviceSn, @RequestBody Q20OtaUpgradeDTO dto) {
//        return new Result<>().ok(q20CommandService.otaUpgrade(deviceSn, dto));
//    }
}
