package com.ruoyi.web.controller.robot;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mqtt.service.MqttSendService;
import com.ruoyi.robot.domain.LdMapProject;
import com.ruoyi.robot.service.ILdMapProjectService;
import io.netty.util.internal.ObjectUtil;
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.Arrays;
import java.util.List;
import java.util.Objects;

import static java.util.List.*;

/**
 * ASM远程功能调用控制器
 * 处理地图录制、构建、实时建图等ASM功能
 */
@Api(tags = "ASM远程功能调用")
@RestController
@RequestMapping("/robot/control/asm")
public class AsmController {

    @Resource
    private MqttSendService mqttSendService;

    @Resource
    private ILdMapProjectService  ldMapProjectService;

    // ==================== 录制操作 ====================

    @ApiOperation("开始录制")
    @PostMapping("/record/start")
    public AjaxResult startRecord(
            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId,
            @ApiParam("地图名称") @RequestParam("mapName") String mapName
    ) {
        if (deviceId.isEmpty() || mapName.isEmpty()) {
            return AjaxResult.error("请输入地图名称");
        }
        LdMapProject existMap  = ldMapProjectService.selectMapByName(mapName);
        if (existMap  != null) {
            return AjaxResult.error("地图已存在，请重新输入名称");
        }

        LdMapProject ldMapProject = new LdMapProject();
        ldMapProject.setDeviceId(deviceId);
        ldMapProject.setMapName(mapName);
        ldMapProject.setStatus("recording");
        ldMapProjectService.insertLdMapProject(ldMapProject);

        boolean result = mqttSendService.startRecord(mapName);
        return result ? AjaxResult.success("开始录制指令发送成功") : AjaxResult.error("开始录制指令发送失败");
    }

    @ApiOperation("停止录制")
    @PostMapping("/record/stop")
    public AjaxResult stopRecord(
//            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId,
            @ApiParam("地图名称") @RequestParam("mapName") String mapName
    ) {
        boolean result = mqttSendService.stopRecord();
        LdMapProject ldMapProject = new LdMapProject();
        ldMapProject.setMapName(mapName);
        ldMapProject.setStatus("unavailable");
        ldMapProjectService.updateLdMapProject(ldMapProject);
        return result ? AjaxResult.success("停止录制指令发送成功") : AjaxResult.error("停止录制指令发送失败");
    }

    // ==================== 构建地图操作 ====================

    @ApiOperation("开始构建地图")
    @PostMapping("/build/start")
    public AjaxResult startBuild(
//            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId,
            @ApiParam("地图名称") @RequestParam("mapName") String mapName,
            @ApiParam("构建步骤(可选，如:recon,kfmix,octomap,tilemap,potree)") @RequestParam(value = "buildSteps", required = false) String buildSteps
    ) {
        List<String> steps = null;
        if (buildSteps != null && !buildSteps.isEmpty()) {
            steps = Arrays.asList(buildSteps.split(","));
        }
        boolean result = mqttSendService.startBuild(mapName, steps);
        return result ? AjaxResult.success("开始构建地图指令发送成功") : AjaxResult.error("开始构建地图指令发送失败");
    }

    @ApiOperation("停止构建地图")
    @PostMapping("/build/stop")
    public AjaxResult stopBuild(
//            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId
    ) {
        boolean result = mqttSendService.stopBuild();
        return result ? AjaxResult.success("停止构建地图指令发送成功") : AjaxResult.error("停止构建地图指令发送失败");
    }

    @ApiOperation("地图构建完成")
    @PostMapping("/build/completed")
    public AjaxResult completed(
            @ApiParam("地图名称") @RequestParam("mapName") String mapName,
            @ApiParam("函数名称") @RequestParam(required = false) String funcName
    ) {
        if (StringUtils.isBlank(mapName)) {
            return AjaxResult.error("地图名称不能为空");
        }

        if (StringUtils.isBlank(funcName)) {
            return AjaxResult.success();
        }

        String status = null;

        switch (funcName) {
            case "MapBuilder.start":
                status = "available";
                break;

            case "ASM.map_slam.start":
                status = "slaming";
                break;

            case "ASM.map_slam.stop":
                status = "available";
                break;

            default:
                break;
        }

        // 需要更新状态时再执行
        if (StringUtils.isNotBlank(status)) {
            LdMapProject ldMapProject = new LdMapProject();
            ldMapProject.setMapName(mapName);
            ldMapProject.setStatus(status);

            ldMapProjectService.updateLdMapProject(ldMapProject);
        }

        return AjaxResult.success();
    }

    // ==================== 实时建图操作 ====================

    @ApiOperation("开始实时SLAM")
    @PostMapping("/slam/start")
    public AjaxResult startSlam(
            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId,
            @ApiParam("地图名称") @RequestParam("mapName") String mapName
    ) {
        LdMapProject existMap  = ldMapProjectService.selectMapByName(mapName);
        if (existMap  != null) {
            return AjaxResult.error("地图已存在，请重新输入名称");
        }
        LdMapProject ldMapProject = new LdMapProject();
        ldMapProject.setDeviceId(deviceId);
        ldMapProject.setMapName(mapName);
        ldMapProject.setStatus("slaming");
        ldMapProjectService.insertLdMapProject(ldMapProject);

        boolean result = mqttSendService.startSlam(mapName);
        return result ? AjaxResult.success("开始实时建图指令发送成功") : AjaxResult.error("开始实时建图指令发送失败");
    }

    @ApiOperation("停止实时SLAM")
    @PostMapping("/slam/stop")
    public AjaxResult stopSlam(
//            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId
    ) {
        boolean result = mqttSendService.stopSlam();
        return result ? AjaxResult.success("停止实时建图指令发送成功") : AjaxResult.error("停止实时建图指令发送失败");
    }

    // ==================== 标准导航操作 ====================

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

    @ApiOperation("结束标准导航")
    @PostMapping("/nav/standard/stop")
    public AjaxResult stopNavStandard(
//            @ApiParam("设备ID") @RequestParam("deviceId") String deviceId
    ) {
        boolean result = mqttSendService.stopNavStandard();
        return result ? AjaxResult.success("结束标准导航指令发送成功") : AjaxResult.error("结束标准导航指令发送失败");
    }
}
