package com.ruoyi.sim.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sim.domain.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.sim.service.impl.CommConst.*;

@Service
// 多实例
// 异步调用
// @Scope("prototype")
public class CommReceiveService {

    private static final Logger l = LoggerFactory.getLogger(CommReceiveService.class);

    @Autowired
    private RealExamFaultService realExamFaultService;
    @Autowired
    private SimService simService;
    @Autowired
    private DebugFaultService debugFaultService;

    /**
     * 只要返回信息，即认为在线。
     *
     * @param sm
     * @param s
     */
    public void checkOneSimState(SimMsg sm, Sim s) {
        if (s == null) {
            l.warn("s is null");
            return;
        }
        if (StringUtils.isNotBlank(sm.getReceiveMsg())) {
            simService.updateSimStateBySimId(s.getSimId(), Sim.State.ONLINE);
        }
    }

    public void clearOneFault(SimMsg sm, Sim s, RealExamFault reF, Fault f) {
        // check

        //
        //
        if (reF != null) {
            realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.CLEARED);
        }
    }

    /**
     * 设置出题值。
     *
     * @param sm
     * @param s
     * @param reF
     * @param f
     */
    public void setFaultQuestionValue(SimMsg sm, Sim s, RealExamFault reF, Fault f) {
        // check

        //
        String faultQuestionValue = parseGetData(sm.getReceiveMsg());
        // todo:
        if (StringUtils.isBlank(faultQuestionValue)) {
            l.warn("faultQuestionValue is empty!");
            return;
        }
        l.info("faultQuestionValue = {}", faultQuestionValue);
        if (reF != null) {
            // 修改关联状态。
            reF.setSimFaultQuestionValue(faultQuestionValue);
            realExamFaultService.updateRealExamFault(reF);
            if (RealExamFault.Flag.YES.equals(reF.getFlag())) {
                realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.WRITTEN);
            } else if (RealExamFault.Flag.NO.equals(reF.getFlag())) {
                realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.LOOP_READ);
            }
        } else {
            DebugFault df = debugFaultService.exist(s.getSimId(), f.getFaultId());
            if (df == null) {
                df = new DebugFault();
                df.setSimFaultQuestionValue(faultQuestionValue);
                debugFaultService.insertDebugFault(df);
            } else {
                df.setSimFaultQuestionValue(faultQuestionValue);
                debugFaultService.updateDebugFault(df);
            }
        }
    }

    /**
     * 设置答题值。
     *
     * @param sm
     * @param s
     * @param reF      debug模式为null
     * @param f
     * @param refState 轮询时候为null debug模式为null
     */
    public void setFaultAnswerValue(SimMsg sm, Sim s, RealExamFault reF, Fault f, String refState) {
        // check
        //
        String faultAnswerValue = parseGetData(sm.getReceiveMsg());
        // todo:
        if (StringUtils.isBlank(faultAnswerValue)) {
            l.warn("faultAnswerValue is empty!");
            return;
        }
        l.info("faultAnswerValue = {}", faultAnswerValue);
        if (reF != null && refState != null) {
            if (StringUtils.isNotBlank(refState)) {
                reF.setRefState(refState);
            }
            reF.setSimFaultAnswerValue(faultAnswerValue);
            realExamFaultService.updateRealExamFault(reF);
        } else {
            DebugFault df = debugFaultService.exist(s.getSimId(), f.getFaultId());
            if (df == null) {
                df = new DebugFault();
                df.setSimFaultAnswerValue(faultAnswerValue);
                debugFaultService.insertDebugFault(df);
            } else {
                df.setSimFaultAnswerValue(faultAnswerValue);
                debugFaultService.updateDebugFault(df);
            }
        }
    }

    /**
     * 截取
     *
     * @param receiveMsg
     * @return
     */
    public String parseGetData(String receiveMsg) {
        if (StringUtils.isEmpty(receiveMsg)) {
            return "";
        }
        return StringUtils.substring(receiveMsg, 10, 18);
    }

    /**
     * 开始考试 检查 故障部位 检查
     *
     * @param sm
     * @param s
     * @param f
     * @return
     */
    public AjaxResult getOneFaultCheck(SimMsg sm, Sim s, Fault f) {
        String checkValue = parseGetData(sm.getReceiveMsg());
        if (s != null && isFaultCheckPass(sm, s, f)) {
            return AjaxResult.success(f);
        }
        if (BLANK_CONTENT.equals(checkValue)) {
            l.info("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;");
            return AjaxResult.error(
                    "故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;"
                    , f);
        } else {
            return AjaxResult.success(f);
        }
    }

    /**
     * 是否在故障部位白名单中。
     *
     * @param sm
     * @param s
     * @param f
     * @return true 在白名单中。
     */
    public boolean isFaultCheckPass(SimMsg sm, Sim s, Fault f) {
        String simType = s.getSimType();
        String faultRealGZBWId = f.getFaultId();
        if (Sim.TYPE_0001.equals(simType)) {

        }
        if (Sim.TYPE_0002.equals(simType)) {
            // 0002型 01故障部位进行特殊处理。
            if ("0002GZBW0001".equals(faultRealGZBWId)) {
                return true;
            }
        }
        if (Sim.TYPE_0003.equals(simType)) {
            // 干燥管
            if ("0003GZBW0007".equals(faultRealGZBWId)) {
                return true;
            }
            // 检测剂
            if ("0003GZBW0006".equals(faultRealGZBWId)) {
                return true;
            }
        }
        // finally
        return false;
    }
}
