package com.ruoyi.sim.service.impl;

import com.ruoyi.sim.domain.Fault;
import com.ruoyi.sim.domain.RealExamFault;
import com.ruoyi.sim.domain.Sim;
import com.ruoyi.sim.domain.SimMsg;
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 java.util.Objects;

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

    private static final Logger l = LoggerFactory.getLogger(CommReceiveService.class);
    @Autowired
    private RealExamService realExamService;
    @Autowired
    private RealExamFaultService realExamFaultService;
    @Autowired
    private SimService simService;

    /**
     * 只要返回信息，即认为在线。
     *
     * @param sm
     * @param s
     */
    public void checkOneSimState(SimMsg sm, Sim s) {
        if (Objects.isNull(sm)) {

        }
        if (!StringUtils.isEmpty(sm.getReceiveMsg())) {
            Sim f = simService.selectSimBySimId(s.getSimId());
            simService.updateSimStateBySimId(s.getSimId(), Sim.State.ONLINE);
        }
    }

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

        //
        //
        if (reF != null) {
            reF.setRefState(RealExamFault.State.CLEARED);
            realExamFaultService.updateRealExamFault(reF);
        }
    }

    /**
     * 设置出题值。
     *
     * @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);
        reF.setRefState(RealExamFault.State.WRITTEN);
        reF.setSimFaultQuestionValue(faultQuestionValue);
        realExamFaultService.updateRealExamFault(reF);
    }

    /**
     * 设置答题值。
     *
     * @param sm
     * @param s
     * @param reF
     * @param f
     * @param refState RealExamFault.State.LOOP_READ
     */
    public void setFaultAnswerValue(SimMsg sm, Sim s, RealExamFault reF, Fault f, String refState) {
        // check
        if (reF == null) {
            l.info("reF null!");
            return;
        }
        //
        String faultAnswerValue = parseGetData(sm.getReceiveMsg());
        // todo:
        if (StringUtils.isBlank(faultAnswerValue)) {
            l.warn("faultAnswerValue is empty!");
            return;
        }
        l.info("faultAnswerValue = {}", faultAnswerValue);
        reF.setRefState(refState);
        reF.setSimFaultAnswerValue(faultAnswerValue);
        realExamFaultService.updateRealExamFault(reF);
    }

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

}
