package cn.iocoder.yudao.module.system.api.remote;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.api.remote.dto.RemoteRobotDTO;
import cn.iocoder.yudao.module.system.api.remote.dto.RemoteRobotStatusDTO;
import cn.iocoder.yudao.module.system.api.remote.dto.RemoteRobotTransferDTO;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.dto.PositionMapDTO;
import cn.iocoder.yudao.module.system.service.positionmap.PositionMapService;
import cn.iocoder.yudao.module.system.service.remote.RemoteControllerInformationService;
import cn.iocoder.yudao.module.system.service.robot.RobotInformationService;
import cn.iocoder.yudao.module.system.service.robot.mapstop.RobotMapStopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class RemoteRobotApiImpl implements RemoteRobotApi {

    @Resource
    private RobotMapStopService mapStopService;

    @Resource
    private PositionMapService positionMapService;

    @Resource
    private RemoteControllerInformationService controllerInformationService;

    @Autowired
    private RobotInformationService robotInformationService;

    /**
     * 远遥获取地图和车辆信息
     *
     * @param mapId
     * @return
     */
    @Override
    public List<RemoteRobotDTO> remoteGetAreaRobot(Long mapId) {
        return positionMapService.getRemoteAreaRobotByMapId(mapId);
    }

    /**
     * 远遥车辆暂停
     *
     * @param mapId
     * @param type
     */
    @Override
    public CommonResult<Boolean> remoteEmergencyStopOrRecovery(Long mapId, Integer type) {
        positionMapService.emergencyStopOrRecovery(mapId, type, true);
        return CommonResult.success(true);
    }

    /**
     * 获取机器人远遥模式/急停/协控状态
     *
     * @param ip
     * @return
     */
    @Override
    public RemoteRobotStatusDTO getRemoteRobotStatus(String remoteIp) {
        return controllerInformationService.getRemoteRobotStatus(remoteIp);
    }

    /**
     * 远遥暂停车辆
     *
     * @param robotNo
     * @return
     */
    @Override
    public CommonResult<Boolean> robotStop(String robotNo) {
        mapStopService.remoteStopRobot(robotNo);
        return CommonResult.success(true);
    }

    /**
     * 远遥恢复车辆
     *
     * @param robotNo
     * @return
     */
    @Override
    public CommonResult<Boolean> recovery(String robotNo) {
        mapStopService.remoteRecoveryRobot(robotNo);
        return CommonResult.success(true);
    }

    /**
     * 远遥切换模式
     *
     * @param remoteMode
     * @param remoteIp
     * @return
     */
    @Override
    public CommonResult<RemoteRobotTransferDTO> robotChangeMode(Integer remoteMode, String remoteIp, String robotNo, int remoteControllerPort, String remoteControllerIp) {
        RemoteRobotTransferDTO dto = controllerInformationService.robotChangeMode(remoteMode, remoteIp, robotNo,remoteControllerPort,remoteControllerIp);
        return CommonResult.success(dto);
    }

    @Override
    public Object getRemotePositionMapList() {
        Map<Integer, List<PositionMapDTO>> result = positionMapService.getPositionMapList();
        return result;
    }

    /**
     * 断开连接
     *
     * @param remoteIp
     * @return
     */
    @Override
    public CommonResult<RemoteRobotTransferDTO> remoteDisconnect(String remoteIp) {
        RemoteRobotTransferDTO data = controllerInformationService.remoteDisconnect(remoteIp);
        return CommonResult.success(data);
    }

    /**
     * 任务完成/取货完成
     *
     * @param remoteIp
     * @param mode
     * @return
     */
    @Override
    public CommonResult<Boolean> remoteTaskDone(String remoteIp, Integer mode) {
        controllerInformationService.remoteTaskDone(remoteIp, mode);
        return CommonResult.success(true);
    }

    @Override
    public String getMacByRobotNo(String robotNo) {
        return robotInformationService.getMacByRobotNo(robotNo);
    }

    @Override
    public String getRobotNoByMac(String mac) {
        return robotInformationService.getRobotNoByMac(mac);
    }


}
