package com.ruoyi.mqtt.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mqtt.service.MqttSendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * MQTT服务的对外接口Controller
 * 提供REST API用于发送MQTT消息和作业指令
 */
@Api(tags = "MQTT消息管理")
@RestController
@RequestMapping("/mqtt")
public class MqttController {

    @Resource
    private MqttSendService mqttSendService;


    /*
    发送MQTT消息
     */
    @ApiOperation("发送MQTT消息")
    @PostMapping("/sendMessage")
    public AjaxResult sendMqttMessage(
            @ApiParam("主题") @RequestParam("topic") String topic,
            @ApiParam("消息内容") @RequestParam("payload") String payload
    ) {
        boolean result = mqttSendService.sendMessage(topic, payload);
        return result ? AjaxResult.success("消息发送成功") : AjaxResult.error("消息发送失败");
    }
    
    // ==================== LD导航系统接口 ====================
    
    @ApiOperation("请求地图列表")
    @PostMapping("/ld/map/list")
    public AjaxResult requestMapList(
            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId
    ) {
        boolean result = mqttSendService.requestMapList();
        return result ? AjaxResult.success("请求发送成功") : AjaxResult.error("请求发送失败");
    }
    
    @ApiOperation("位姿初始化(坐标)")
    @PostMapping("/ld/localization/init")
    public AjaxResult initPose(
//            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId,
            @ApiParam("X坐标") @RequestParam("x") double x,
            @ApiParam("Y坐标") @RequestParam("y") double y,
            @ApiParam("航向角(弧度)") @RequestParam("yaw") double yaw
    ) {
        boolean result = mqttSendService.initPose(x, y, yaw);
        return result ? AjaxResult.success("位姿初始化指令发送成功") : AjaxResult.error("位姿初始化指令发送失败");
    }
    
    @ApiOperation("位姿初始化(路网点ID)")
    @PostMapping("/ld/localization/initByNid")
    public AjaxResult initPoseByNid(
            @ApiParam("路网点ID") @RequestParam("nid") int nid
    ) {
        boolean result = mqttSendService.initPoseByNid(nid);
        return result ? AjaxResult.success("位姿初始化指令发送成功") : AjaxResult.error("位姿初始化指令发送失败");
    }
    
    @ApiOperation("开启导航")
    @PostMapping("/ld/navigation/start")
    public AjaxResult startNavigation(
            @ApiParam("地图名称") @RequestParam("mapName") String mapName
    ) {
        boolean result = mqttSendService.startNavigation(mapName);
        return result ? AjaxResult.success("开启导航指令发送成功") : AjaxResult.error("开启导航指令发送失败");
    }
    
    @ApiOperation("关闭导航")
    @PostMapping("/ld/navigation/stop")
    public AjaxResult stopNavigation(
    ) {
        boolean result = mqttSendService.stopNavigation();
        return result ? AjaxResult.success("关闭导航指令发送成功") : AjaxResult.error("关闭导航指令发送失败");
    }
    
    @ApiOperation("重启导航")
    @PostMapping("/ld/navigation/restart")
    public AjaxResult restartNavigation(
            @ApiParam("地图名称(可选)") @RequestParam(value = "mapName", required = false) String mapName
    ) {
        boolean result = mqttSendService.restartNavigation(mapName);
        return result ? AjaxResult.success("重启导航指令发送成功") : AjaxResult.error("重启导航指令发送失败");
    }
    
    @ApiOperation("前往目标点(路网点ID)")
    @PostMapping("/ld/task/gotoByNid")
    public AjaxResult gotoTargetByNid(
            @ApiParam("地图名称") @RequestParam("mapName") String mapName,
            @ApiParam("目标点ID") @RequestParam("nid") int nid
    ) {
        boolean result = mqttSendService.gotoTargetByNid(mapName, nid);
        return result ? AjaxResult.success("前往目标点指令发送成功") : AjaxResult.error("前往目标点指令发送失败");
    }
    
    @ApiOperation("前往目标点(坐标)")
    @PostMapping("/ld/task/gotoByCoord")
    public AjaxResult gotoTargetByCoord(
            @ApiParam("地图名称") @RequestParam("mapName") String mapName,
            @ApiParam("X坐标") @RequestParam("x") double x,
            @ApiParam("Y坐标") @RequestParam("y") double y
    ) {
        boolean result = mqttSendService.gotoTargetByCoord(mapName, x, y);
        return result ? AjaxResult.success("前往目标点指令发送成功") : AjaxResult.error("前往目标点指令发送失败");
    }
    
    @ApiOperation("暂停任务")
    @PostMapping("/ld/task/pause")
    public AjaxResult pauseTask() {
        boolean result = mqttSendService.pauseTask();
        return result ? AjaxResult.success("暂停任务指令发送成功") : AjaxResult.error("暂停任务指令发送失败");
    }
    
    @ApiOperation("继续任务")
    @PostMapping("/ld/task/resume")
    public AjaxResult resumeTask() {
        boolean result = mqttSendService.resumeTask();
        return result ? AjaxResult.success("继续任务指令发送成功") : AjaxResult.error("继续任务指令发送失败");
    }
    
    @ApiOperation("取消任务")
    @PostMapping("/ld/task/cancel")
    public AjaxResult cancelTask() {
        boolean result = mqttSendService.cancelTask();
        return result ? AjaxResult.success("取消任务指令发送成功") : AjaxResult.error("取消任务指令发送失败");
    }
    
    @ApiOperation("遇障重规划")
    @PostMapping("/ld/planning/replan")
    public AjaxResult replan() {
        boolean result = mqttSendService.replan();
        return result ? AjaxResult.success("重规划指令发送成功") : AjaxResult.error("重规划指令发送失败");
    }

    // ==================== 急停操作接口 ====================

    @ApiOperation("紧急停止")
    @PostMapping("/emergency/stop")
    public AjaxResult emergencyStop(
            @ApiParam("停止类型") @RequestParam(value = "type", defaultValue = "true") boolean type
    ) {
        boolean result = mqttSendService.emergencyStop(type);
        return result ? AjaxResult.success("急停指令发送成功") : AjaxResult.error("急停指令发送失败");
    }

    @ApiOperation("释放急停")
    @PostMapping("/emergency/release")
    public AjaxResult releaseEmergencyStop() {
        boolean result = mqttSendService.emergencyStopRelease();
        return result ? AjaxResult.success("释放急停指令发送成功") : AjaxResult.error("释放急停指令发送失败");
    }

    @ApiOperation("启动标准导航")
    @PostMapping("/nav/standard/start")
    public AjaxResult startNavStandard(
            @ApiParam("地图名称") @RequestParam("mapName") String mapName
    ) {
        boolean result = mqttSendService.startNavStandard(mapName);
        return result ? AjaxResult.success("启动标准导航指令发送成功") : AjaxResult.error("启动标准导航指令发送失败");
    }

    @ApiOperation("请求路径规划")
    @PostMapping("/planning/plan/request")
    public AjaxResult requestPlanning(
            @ApiParam("地图名称") @RequestParam("mapName") String mapName,
            @ApiParam("目标X坐标") @RequestParam("x") double x,
            @ApiParam("目标Y坐标") @RequestParam("y") double y
    ) {
        boolean result = mqttSendService.requestPlanning( mapName, x, y);
        return result ? AjaxResult.success("路径规划请求发送成功") : AjaxResult.error("路径规划请求发送失败");
    }

    @ApiOperation("启动任务")
    @PostMapping("/task/procedure/start")
    public AjaxResult startTask(
            @ApiParam("地图名称") @RequestParam("mapName") String mapName,
            @ApiParam("任务名称") @RequestParam("taskName") String taskName
    ) {
        boolean result = mqttSendService.startTask(mapName, taskName);
        return result ? AjaxResult.success("任务启动指令发送成功") : AjaxResult.error("任务启动指令发送失败");
    }

    // ==================== 旧版本接口(兼容) ====================
    
 /*
 发送作业指令
 */
    @ApiOperation("发送作业指令(旧版)")
    @PostMapping("/sendTaskCommand")
    public AjaxResult sendTaskCommand(
            @RequestParam("machineCode") String machineCode,
            @RequestParam("jobId") String jobId,
            @RequestParam("action") String action,
            @RequestParam("requestId") String requestId
    ) {
        Map<String, Object> payload = new HashMap<>();
        payload.put("requestId", requestId);
        payload.put("jobId", jobId);
        payload.put("action", action);

        String topic = String.format("device/%s/cmd/job/execute", machineCode);
        boolean result = mqttSendService.sendMessage(topic, JSON.toJSONString(payload));

        return result ? AjaxResult.success("作业指令下发成功") : AjaxResult.error("作业指令下发失败");
    }
}
