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.Q20ParamsDTO;
import com.multictrl.modules.business.q20.service.Q20ParamService;
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/param")
@Tag(name = "Q20参数指令")
@ApiOrder(11)
@RequiredArgsConstructor
public class Q20ParamController {

    private final Q20ParamService q20ParamService;

    // ==================== 高度限制 ====================

    @PostMapping("/setHeightLimit/{deviceSn}")
    @LogOperation("设置高度限制")
    @Operation(summary = "设置高度限制(m)，0=无限制")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setHeightLimit(@PathVariable String deviceSn, @RequestParam float value) {
        return new Result<>().ok(q20ParamService.setHeightLimit(deviceSn, value));
    }

    @GetMapping("/getHeightLimit/{deviceSn}")
    @Operation(summary = "获取当前高度限制(m)")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getHeightLimit(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getHeightLimit(deviceSn));
    }

    // ==================== 距离限制 ====================

    @PostMapping("/setDistanceLimit/{deviceSn}")
    @LogOperation("设置距离限制")
    @Operation(summary = "设置距离限制(m)，0=无限制")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setDistanceLimit(@PathVariable String deviceSn, @RequestParam float value) {
        return new Result<>().ok(q20ParamService.setDistanceLimit(deviceSn, value));
    }

    @GetMapping("/getDistanceLimit/{deviceSn}")
    @Operation(summary = "获取当前距离限制(m)")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getDistanceLimit(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getDistanceLimit(deviceSn));
    }

    // ==================== 围栏动作 ====================

    @PostMapping("/setGeofenceAction/{deviceSn}")
    @LogOperation("设置围栏动作")
    @Operation(summary = "设置围栏动作: goContinue/warning/hover/goBack/terminate/landing")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setGeofenceAction(@PathVariable String deviceSn, @RequestParam String value) {
        return new Result<>().ok(q20ParamService.setGeofenceAction(deviceSn, value));
    }

    @GetMapping("/getGeofenceAction/{deviceSn}")
    @Operation(summary = "获取当前围栏动作")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getGeofenceAction(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getGeofenceAction(deviceSn));
    }

    // ==================== 返航高度 ====================

    @PostMapping("/setReturnHeight/{deviceSn}")
    @LogOperation("设置返航高度")
    @Operation(summary = "设置返航高度(m)，相对home点高度")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setReturnHeight(@PathVariable String deviceSn, @RequestParam float value) {
        return new Result<>().ok(q20ParamService.setReturnHeight(deviceSn, value));
    }

    @GetMapping("/getReturnHeight/{deviceSn}")
    @Operation(summary = "获取当前返航高度(m)")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getReturnHeight(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getReturnHeight(deviceSn));
    }

    // ==================== 返航类型 ====================

    @PostMapping("/setReturnType/{deviceSn}")
    @LogOperation("设置返航类型")
    @Operation(summary = "设置返航类型: directReturn(直接返航) / originalReturn(原路返航)")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setReturnType(@PathVariable String deviceSn, @RequestParam String value) {
        return new Result<>().ok(q20ParamService.setReturnType(deviceSn, value));
    }

    @GetMapping("/getReturnType/{deviceSn}")
    @Operation(summary = "获取当前返航类型")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getReturnType(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getReturnType(deviceSn));
    }

    // ==================== 电池低电阈值 ====================

    @PostMapping("/setBatteryLowThreshold/{deviceSn}")
    @LogOperation("设置低电阈值")
    @Operation(summary = "设置低电阈值（比例，如0.3=30%）")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setBatteryLowThreshold(@PathVariable String deviceSn, @RequestParam float value) {
        return new Result<>().ok(q20ParamService.setBatteryLowThreshold(deviceSn, value));
    }

    @GetMapping("/getBatteryLowThreshold/{deviceSn}")
    @Operation(summary = "获取当前低电阈值")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getBatteryLowThreshold(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getBatteryLowThreshold(deviceSn));
    }

    // ==================== 严重低电阈值 ====================

    @PostMapping("/setBatteryCriticalThreshold/{deviceSn}")
    @LogOperation("设置严重低电阈值")
    @Operation(summary = "设置严重低电阈值（比例）")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setBatteryCriticalThreshold(@PathVariable String deviceSn, @RequestParam float value) {
        return new Result<>().ok(q20ParamService.setBatteryCriticalThreshold(deviceSn, value));
    }

    @GetMapping("/getBatteryCriticalThreshold/{deviceSn}")
    @Operation(summary = "获取严重低电阈值")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getBatteryCriticalThreshold(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getBatteryCriticalThreshold(deviceSn));
    }

    // ==================== 紧急低电阈值 ====================

    @PostMapping("/setBatteryEmergencyThreshold/{deviceSn}")
    @LogOperation("设置紧急低电阈值")
    @Operation(summary = "设置紧急低电阈值（比例）")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setBatteryEmergencyThreshold(@PathVariable String deviceSn, @RequestParam float value) {
        return new Result<>().ok(q20ParamService.setBatteryEmergencyThreshold(deviceSn, value));
    }

    @GetMapping("/getBatteryEmergencyThreshold/{deviceSn}")
    @Operation(summary = "获取紧急低电阈值")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getBatteryEmergencyThreshold(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getBatteryEmergencyThreshold(deviceSn));
    }

    // ==================== 低电动作 ====================

    @PostMapping("/setLowBatteryAction/{deviceSn}")
    @LogOperation("设置低电动作")
    @Operation(summary = "设置低电动作: warning(警告) / landing(降落) / goBackCritAndLandEmerg(低电返航·紧急降落)")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setLowBatteryAction(@PathVariable String deviceSn, @RequestParam String value) {
        return new Result<>().ok(q20ParamService.setLowBatteryAction(deviceSn, value));
    }

    @GetMapping("/getLowBatteryAction/{deviceSn}")
    @Operation(summary = "获取当前低电动作")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getLowBatteryAction(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getLowBatteryAction(deviceSn));
    }

    // ==================== 精准降落 ====================

    @PostMapping("/setAprilTagId/{deviceSn}")
    @LogOperation("设置精准降落二维码标识")
    @Operation(summary = "设置精准降落二维码标识")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setAprilTagId(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20ParamService.setAprilTagId(deviceSn, value));
    }

    @GetMapping("/getAprilTagId/{deviceSn}")
    @Operation(summary = "获取精准降落二维码标识")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getAprilTagId(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getAprilTagId(deviceSn));
    }

    @PostMapping("/setPrecisionLandMode/{deviceSn}")
    @LogOperation("设置精准降落模式")
    @Operation(summary = "设置精准降落模式: noPrecisionLanding / opportunisticPrecisionLanding / requiredPrecisionLanding")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setPrecisionLandMode(@PathVariable String deviceSn, @RequestParam String value) {
        return new Result<>().ok(q20ParamService.setPrecisionLandMode(deviceSn, value));
    }

    @GetMapping("/getPrecisionLandMode/{deviceSn}")
    @Operation(summary = "获取精准降落模式")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getPrecisionLandMode(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getPrecisionLandMode(deviceSn));
    }

    // ==================== 避障 ====================

    @PostMapping("/setObstacleAvoidanceEnable/{deviceSn}")
    @LogOperation("设置避障开关")
    @Operation(summary = "设置避障开关: 0=关闭 1=开启")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setObstacleAvoidanceEnable(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20ParamService.setObstacleAvoidanceEnable(deviceSn, value));
    }

    @GetMapping("/getObstacleAvoidanceEnable/{deviceSn}")
    @Operation(summary = "获取避障开关状态")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getObstacleAvoidanceEnable(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getObstacleAvoidanceEnable(deviceSn));
    }

    @PostMapping("/setObstacleAvoidanceDistance/{deviceSn}")
    @LogOperation("设置避障距离")
    @Operation(summary = "设置避障距离(m)")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setObstacleAvoidanceDistance(@PathVariable String deviceSn, @RequestParam float value) {
        return new Result<>().ok(q20ParamService.setObstacleAvoidanceDistance(deviceSn, value));
    }

    @GetMapping("/getObstacleAvoidanceDistance/{deviceSn}")
    @Operation(summary = "获取避障距离(m)")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getObstacleAvoidanceDistance(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getObstacleAvoidanceDistance(deviceSn));
    }

    // ==================== 视觉传感器 ====================

    @PostMapping("/setVisualSensorEnable/{deviceSn}")
    @LogOperation("设置视觉传感器开关")
    @Operation(summary = "设置视觉传感器开关: 0=关闭 1=开启")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setVisualSensorEnable(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20ParamService.setVisualSensorEnable(deviceSn, value));
    }

    @GetMapping("/getVisualSensorEnable/{deviceSn}")
    @Operation(summary = "获取视觉传感器开关状态")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getVisualSensorEnable(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getVisualSensorEnable(deviceSn));
    }

    // ==================== RTK ====================

    @PostMapping("/setRtkEnable/{deviceSn}")
    @LogOperation("设置RTK开关")
    @Operation(summary = "设置RTK开关: 0=关闭 1=开启（重启后生效）")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setRtkEnable(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20ParamService.setRtkEnable(deviceSn, value));
    }

    @GetMapping("/getRtkEnable/{deviceSn}")
    @Operation(summary = "获取RTK开关状态")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getRtkEnable(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getRtkEnable(deviceSn));
    }

    // ==================== 多载控制 ====================

    @PostMapping("/setUatMultCtrlEnable/{deviceSn}")
    @LogOperation("设置多载控制开关")
    @Operation(summary = "设置多载控制开关: 0=关闭 1=开启（重启后生效）")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setUatMultCtrlEnable(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20ParamService.setUatMultCtrlEnable(deviceSn, value));
    }

    @GetMapping("/getUatMultCtrlEnable/{deviceSn}")
    @Operation(summary = "获取多载控制开关状态")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getUatMultCtrlEnable(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getUatMultCtrlEnable(deviceSn));
    }

    // ==================== 降落伞 ====================

    @PostMapping("/setParachuteSafetyEnable/{deviceSn}")
    @LogOperation("设置降落伞安全开关")
    @Operation(summary = "设置降落伞安全开关: 0=关闭 1=开启")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setParachuteSafetyEnable(@PathVariable String deviceSn, @RequestParam int value) {
        return new Result<>().ok(q20ParamService.setParachuteSafetyEnable(deviceSn, value));
    }

    @GetMapping("/getParachuteSafetyEnable/{deviceSn}")
    @Operation(summary = "获取降落伞安全开关状态")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getParachuteSafetyEnable(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getParachuteSafetyEnable(deviceSn));
    }

    // ==================== 批量 ====================

    @PostMapping("/setParams/{deviceSn}")
    @LogOperation("批量设置参数")
    @Operation(summary = "批量设置参数（null字段不下发）")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> setParams(@PathVariable String deviceSn, @RequestBody Q20ParamsDTO dto) {
        return new Result<>().ok(q20ParamService.setParams(deviceSn, dto));
    }

    @GetMapping("/getParams/{deviceSn}")
    @Operation(summary = "批量获取所有参数")
    @RequiresPermissions("bus:q20:param")
    public Result<Object> getParams(@PathVariable String deviceSn) {
        return new Result<>().ok(q20ParamService.getParams(deviceSn));
    }
}
