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

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.remote.dto.*;
import cn.iocoder.yudao.module.system.config.poperties.ZnConfigConstant;
import cn.iocoder.yudao.module.system.controller.admin.robot.camera.RobotCameraAddVO;
import cn.iocoder.yudao.module.system.service.remote.RemoteControllerInformationService;
import cn.iocoder.yudao.module.system.service.robot.RobotTaskDetailService;
import cn.iocoder.yudao.module.system.service.robot.camera.RobotCameraService;
import cn.iocoder.yudao.module.system.util.aes.AESEncryptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.CAMERA_DECRYPTION_FAILED;

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


    @Resource
    private RobotTaskDetailService taskDetailService;

    @Resource
    private RemoteControllerInformationService controllerInformationService;

    @Resource
    private RobotCameraService cameraService;

    @Autowired
    private ZnConfigConstant znConfigConstant;

    /**
     * 远遥查询异常的车辆
     * @param remoteTaskQuery
     * @return
     */
    @Override
    public PageResult<RemoteExceptionTaskDetailDTO> getExceptionTaskDetail(RemoteTaskQueryDTO remoteTaskQuery) {
        return taskDetailService.getExceptionTaskDetail(remoteTaskQuery);
    }

    /**
     * 远遥查看摄像头
     * @param robotNo
     * @return
     */
    @Override
    public CommonResult<List<RemoteRobotCameraDTO>> getCarmeraByRobotNo(String robotNo) {
        List<RobotCameraAddVO> cameraByRobotNo = cameraService.getCameraByRobotNo(robotNo);
        if (ObjectUtil.isEmpty(cameraByRobotNo)) {
            return success(new ArrayList<>());
        }
        List<RemoteRobotCameraDTO> list = BeanUtils.toBean(cameraByRobotNo, RemoteRobotCameraDTO.class);

        for (RemoteRobotCameraDTO robotCameraDO : list) {
            try {
                robotCameraDO.setCameraAccount(AESEncryptionUtil.decrypt(robotCameraDO.getCameraAccount(),znConfigConstant.getCameraSecretKey()));
                robotCameraDO.setCameraPassword(AESEncryptionUtil.decrypt(robotCameraDO.getCameraPassword(),znConfigConstant.getCameraSecretKey()));
            } catch (Exception e) {
                throw exception(CAMERA_DECRYPTION_FAILED);
            }
        }

        return success(list);
    }

    /**
     *  远遥车辆任务转移
     * @param data
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> remoteTransferTask(RemoteTaskTransferDTO data) {
        controllerInformationService.remoteTransferTaskToFreeRobot(data);
        return success(true);
    }

}
