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

import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IShippingDispatchService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.function.IFsmModuleFunctionService;
import cn.estsh.i3plus.mes.pcn.util.StringUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRouteOptParam;
import cn.estsh.i3plus.pojo.mes.model.StationCustomDialogBean;
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 com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Optional;

/**
 * @Description : 客制化弹框会话工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesCustomDialogStepService")
public class MesCustomDialogStepService extends BaseStepService {

    @Autowired
    private IFsmModuleFunctionService fsmModuleFunctionService;

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    @Override
    public void title(StationRequestBean reqBean) {
        this.sendMessage(reqBean, new StationResultBean().resultObj(MesPcnExtConstWords.STEP_DISABLE_SCAN), String.format("工步: %s", reqBean.getStepName()), MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_TITLE, MesPcnEnumUtil.STATION_DATA_TYPE.TITLE);
    }

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //弹框已经关闭
        if (reqBean.getStepDialogStatus()) {
            reqBean.setButtonCode(null);
            reqBean.setStepDialogStatus(false);
            return stepDynamicsCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), stepResult, true, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT,  "弹框会话关闭!");
        }

        //获取工步参数
        Optional<Map<String, MesProdRouteOptParam>> stepParamMap = getStepParams(reqBean);

        String customPageName = (null != stepParamMap && stepParamMap.isPresent() && stepParamMap.get().containsKey(MesPcnExtConstWords.CUSTOM_PAGE_NAME)) ? stepParamMap.get().get(MesPcnExtConstWords.CUSTOM_PAGE_NAME).getParamValue() : null;

        if (StringUtils.isEmpty(customPageName)) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), String.format("弹框会话工步未配置工步参数[%s]!", MesPcnExtConstWords.CUSTOM_PAGE_NAME));

        //获取工步弹框需要渲染的数据; 无须渲染动态数据时无须重写stepDialogDispatch方法且接口方法已默认返回true可以直接弹框
        StationCustomDialogBean scdBean = fsmModuleFunctionService.stepDialogDispatch(reqBean, new StationCustomDialogBean(customPageName));

        //当前不需要弹框
        if (null == scdBean || !scdBean.getIsDialog()) return stepResult;

        if (null != scdBean.getObj())
            log.info("工厂{}生产线{}工位{}:FSM STATE DISPATCHER --- DO STEP --- {} EXEC --- StationCustomDialogBean={}",
                    reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), StringUtil.toLowerCaseFirst(this.getClass().getSimpleName()), JSONObject.toJSONString(scdBean));

        //清除剩余扫描队列
        shippingDispatchService.clearScanQueue(reqBean);

        //发送弹框数据
        this.sendMessage(reqBean, resultBean.busiType(MesPcnEnumUtil.STATION_BUSI_TYPE.CUSTOM_COMPONENT.getValue()).dataType(MesPcnEnumUtil.STATION_DATA_TYPE.CUSTOM_DIALOG.getValue()).customPageName(customPageName).resultObj(scdBean.getObj()));

        //弹框业务同步处理
        if (!scdBean.getIsAsyn()) stepSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "弹框会话开启!");

        //弹框业务异步处理
        return stepDynamicsCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), stepResult, true, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT,  "弹框会话异步开启!");

    }

}
