package cn.iocoder.yudao.module.remote.controller.admin.robot;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.remote.controller.admin.robot.dto.PositionMapRespDTO;
import cn.iocoder.yudao.module.remote.controller.admin.robot.dto.RemoteRobotChangeModeDTO;
import cn.iocoder.yudao.module.remote.controller.admin.robot.dto.RemoteRobotTaskDoneDTO;
import cn.iocoder.yudao.module.remote.service.robot.RemoteRobotService;
import cn.iocoder.yudao.module.system.api.remote.dto.RemoteRobotDTO;
import cn.iocoder.yudao.module.system.api.remote.dto.RemoteRobotStatusDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 远遥车辆")
@RestController
@RequestMapping("/remote/robot")
@Validated
public class RemoteRobotController {

    @Autowired
    private RemoteRobotService remoteRobotService;

    @PostMapping("/getAreaRobot")
    @Operation(summary = "获取地图区域对应的机器人信息")
    public CommonResult<List<RemoteRobotDTO>> getAreaRobot(@Valid @RequestBody PositionMapRespDTO positionMapRespDTO) {
        List<RemoteRobotDTO> list = remoteRobotService.remoteGetAreaRobot(positionMapRespDTO);
        return success(list);
    }

    @GetMapping("/emergencyStopOrRecovery")
    @Operation(summary = "一键急停or一键恢复地图上所有AGV")
    @Parameter(name = "type", description = "类型(1:停止， 0:恢复)", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('remote:robot:emergencyStopOrRecovery')")
    public CommonResult<Boolean> remoteEmergencyStopOrRecovery(@RequestParam("id") Long id, @RequestParam("type") Integer type) {
        remoteRobotService.remoteEmergencyStopOrRecovery(id,type);
        return success(true);
    }

    @PostMapping("/getRobotRemoteStatus")
    @Operation(summary = "获取机器人远遥模式/急停/协控状态")
    @PreAuthorize("@ss.hasPermission('remote:robot:getRobotRemoteStatus')")
    public CommonResult<RemoteRobotStatusDTO> getRobotRemoteStatus(HttpServletRequest request) {
        return success(remoteRobotService.getRemoteRobotStatus(request));
    }


    @GetMapping("/stop")
    @Operation(summary = "暂停车辆")
    @PreAuthorize("@ss.hasPermission('remote:robot:stop')")
    public CommonResult<Boolean> robotStop(@RequestParam("robotNo") String robotNo) {
        remoteRobotService.robotStop(robotNo);
        return success(true);
    }


    @GetMapping("/recovery")
    @Operation(summary = "恢复车辆")
    @PreAuthorize("@ss.hasPermission('remote:robot:recovery')")
    public CommonResult<Boolean> recovery(@RequestParam("robotNo") String robotNo) {
        remoteRobotService.recovery(robotNo);
        return success(true);
    }

    @PostMapping("/robotChangeMode")
    @Operation(summary = "修改模式")
    @PreAuthorize("@ss.hasPermission('remote:robot:robotChangeMode')")
    public CommonResult<Boolean> robotChangeMode(@Valid @RequestBody RemoteRobotChangeModeDTO data,HttpServletRequest request) {
        remoteRobotService.robotChangeMode(data,request);
        return success(true);
    }

    @GetMapping("/getMap")
    @Operation(summary = "获得仓库点位地图Map")
    @PreAuthorize("@ss.hasPermission('remote:robot:getMap')")
    public CommonResult<Object> getPositionMapList() {
        Object result = remoteRobotService.getPositionMapList();
        return success(result);
    }


    @PostMapping("/disconnect")
    @Operation(summary = "断开连接")
    @PreAuthorize("@ss.hasPermission('remote:robot:disconnect')")
    public CommonResult<Boolean> remoteDisconnect(HttpServletRequest request) {
        remoteRobotService.remoteDisconnect(request);
        return success(true);
    }

    @PostMapping("/taskDone")
    @Operation(summary = "任务完成/取货完成")
    @PreAuthorize("@ss.hasPermission('remote:robot:taskDone')")
    public CommonResult<Boolean> remoteTaskDone(@Valid @RequestBody RemoteRobotTaskDoneDTO remoteRobotTaskDoneDTO,
                                                HttpServletRequest request) {
        remoteRobotService.remoteTaskDone(remoteRobotTaskDoneDTO,request);
        return success(true);
    }

}
