package com.tuoheng.admin.service.accident.forward;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.AccidentInspection;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.AccidentDroneForwardTypeEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AccidentInspectionMapper;
import com.tuoheng.admin.mapper.AccidentMapper;
import com.tuoheng.admin.request.accident.AccidentForwardRequest;
import com.tuoheng.admin.service.accident.forward.check.DroneForwardCheckParamContext;
import com.tuoheng.admin.service.accident.forward.check.DroneForwardCheckParamStrategy;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.websocket.WebSocketService;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 无人机前往业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-04-14
 */
@Slf4j
@Service
public class AccidentForwardFactory {

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private AccidentInspectionMapper accidentInspectionMapper;

    @Autowired
    @Qualifier("originalDroneForward")
    private AccidentForwardService originalDroneForward;

    @Autowired
    @Qualifier("originalDroneContinue")
    private AccidentForwardService originalDroneContinue;

    @Autowired
    @Qualifier("newDroneForward")
    private AccidentForwardService newDroneForward;

    @Autowired
    private WebSocketService webSocketService;

    @Autowired
    @Qualifier("originalDroneForwardCheckParamStrategy")
    private DroneForwardCheckParamStrategy originalDroneForwardCheckParamStrategy;

    @Autowired
    @Qualifier("originalDroneContinueCheckParamStrategy")
    private DroneForwardCheckParamStrategy originalDroneContinueCheckParamStrategy;

    @Autowired
    @Qualifier("newDroneForwardCheckParamStrategy")
    private DroneForwardCheckParamStrategy newDroneForwardCheckParamStrategy;

    /**
     * 无人机前往
     *
     * @return
     */
    public JsonResult forward(AccidentForwardRequest request) {
        log.info("进入无人机前往业务");
        User user = CurrentUserUtil.getUserInfo();
        String tenantId = user.getTenantId();
        JsonResult result = this.checkParam(user.getId(), tenantId, request);
        if (0 != result.getCode()) {
            log.info("无人机前往：校验参数失败：{}", result.getMsg());
            return result;
        }
        Accident accident = (Accident) result.getData();
        if (AccidentDroneForwardTypeEnum.ORIGINAL_DRONE_FORWARD.getCode() == request.getDroneForwardType()) {
            log.info("DSP分析应急事故，原无人机前往");
            DroneForwardCheckParamContext checkParamContext = new DroneForwardCheckParamContext(originalDroneForwardCheckParamStrategy);
            checkParamContext.check(request, accident);
            result = originalDroneForward.forward(user, accident, request);
        } else if (AccidentDroneForwardTypeEnum.NEW_DRONE_FORWARD.getCode() == request.getDroneForwardType()) {
            log.info("DSP分析应急事故，新无人机前往");
            DroneForwardCheckParamContext checkParamContext = new DroneForwardCheckParamContext(originalDroneContinueCheckParamStrategy);
            checkParamContext.check(request, accident);
            result = originalDroneContinue.forward(user, accident, request);
        } else if (AccidentDroneForwardTypeEnum.NEW_DRONE_FORWARD_BY_MANUAL.getCode() == request.getDroneForwardType()) {
            log.info("手动创建应急事故，新无人机前往");
            DroneForwardCheckParamContext checkParamContext = new DroneForwardCheckParamContext(newDroneForwardCheckParamStrategy);
            checkParamContext.check(request, accident);
            result = newDroneForward.forward(user, accident, request);
        } else {
            log.info("进入无人机前往业务，类型错误");
            return JsonResult.error("无人机前往业务，类型错误");
        }

        // 前往后，给webSocket推送最新的数据
        webSocketService.accidentNotice(accident.getTenantId());

        return result;
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param request
     * @return
     */
    private JsonResult checkParam(String userId, String tenantId, AccidentForwardRequest request) {
        if (StringUtils.isEmpty(request.getAccidentId())) {
            throw new ServiceException("应急事故ID为空");
        }
        Accident accident = accidentMapper.selectOne(new LambdaQueryWrapper<Accident>()
                .eq(Accident::getTenantId, tenantId)
                .eq(Accident::getId, request.getAccidentId())
                .eq(Accident::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(accident)) {
            throw new ServiceException("应急事故不存在空");
        }
        return JsonResult.success(accident);
    }

}
