package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.step.jx;

import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.mes.pcn.websocket.StationWebSocket;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.ButtonBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * @Description : 工步业务-本体包装校验条码有效性工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxNoumenonSnLineOffQCheckStepService")
public class SxNoumenonSnLineOffQCheckStepService extends BaseStepService {

    @Resource(name = MesPcnExtConstWords.REDIS_RES_PCN)
    private ImppRedis redisMesPcn;

    // 返回前端按钮 确认按钮弹框
    private static final String PASS_BUTTON_CODE = "PASS";
    private static final String PASS_BUTTON_NAME = "通过";
    private static final String NOT_PASS_BUTTON_CODE = "NON_PASS";
    private static final String NOT_PASS_BUTTON_NAME = "不通过";

    @Override
    public StepResult init(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();
        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue());


        this.sendMessage(reqBean, resultBean);
        return super.init(reqBean);
    }

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("SxNoumenonSnLineOffQCheckStepService[execute] 参数：{}", reqBean);

        StepResult stepResult = StepResult.getNonComplete();

        if (!StringUtils.isEmpty(reqBean.getButtonCode())) {
            if (reqBean.getButtonCode().equals(PASS_BUTTON_CODE)) {

                // 将model中的buttonCode password移除
                removeStepContent(reqBean);

                stepResult = StepResult.getSuccessComplete();

                return stepResult;
            } else {

                // 点击不通过，结束工步。直接进入放行信号

                removeStepContent(reqBean);

                stepResult = StepResult.getSuccessComplete();

                stepResult.setNextTriggerEvent("NON_PASS");

                return stepResult;
            }
        }

        sendButton(reqBean);
        // 发送按钮组件，工步未完成
        return stepResult;
    }

    private void sendButton(StationRequestBean reqBean) {
        List<ButtonBean> buttonBeanList = new ArrayList<>();
        buttonBeanList.add(ButtonBean.builder().buttonCode(PASS_BUTTON_CODE).buttonName(PASS_BUTTON_NAME).buttonDesc("通过按钮事件").build());
        buttonBeanList.add(ButtonBean.builder().buttonCode(NOT_PASS_BUTTON_CODE).buttonName(NOT_PASS_BUTTON_NAME).buttonDesc("不通过按钮事件").build());

        StationResultBean resultBean = new StationResultBean();
        resultBean.setResultList(buttonBeanList);
        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue());
        resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.BUTTON.getValue());
        resultBean.setMessage(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请进行外观检查,并录入外观检结果"));
        this.sendMessage(reqBean, resultBean);


    }

    private void removeStepContent(StationRequestBean reqBean) {
        reqBean.setButtonCode(null);
        StationResultBean resultBean = new StationResultBean();
        resultBean.setResultList(null);
        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue());
        resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.BUTTON.getValue());
        this.sendMessage(reqBean, resultBean);
    }


}
