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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.ISxEsopCenterExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu3.SxEsopCenterDataModel;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/7/12 9:59
 **/
@Slf4j
@Service
public class SxEsopCenterExtService implements ISxEsopCenterExtService {

    @Override
    public SxEsopCenterDataModel exec(SxEsopCenterDataModel data) {

        ESOP_CENTER_ACTION_TYPE actionType = getActionType(data);

        if (actionType.value == ESOP_CENTER_ACTION_TYPE.COMMON.value)
            new SxEsopCenterActionWatchService(new SxEsopCenterActionCommonService()).exec(data);

        else if (actionType.value == ESOP_CENTER_ACTION_TYPE.AUTO_CHANGE.value)
            new SxEsopCenterActionWatchService(new SxEsopCenterActionAutoChangeService(new SxEsopCenterActionCommonService())).exec(data);

        else new SxEsopCenterActionWatchService(new SxEsopCenterActionHandChangeService(new SxEsopCenterActionCommonService())).exec(data);

        return data.feedBackMsg(actionType.feedBackMsg);

    }

    private ESOP_CENTER_ACTION_TYPE getActionType(SxEsopCenterDataModel data) {
        ESOP_CENTER_ACTION_TYPE actionType = ESOP_CENTER_ACTION_TYPE.getByValue(data.getActionType());
        if (null == actionType)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("作业指导书:入参[actionType]数值异常")
                    .build();
        return actionType;
    }

    private enum ESOP_CENTER_ACTION_TYPE {

        COMMON(1, "默认", "初始化成功!"),
        AUTO_CHANGE(2, "自动切替", "自动切替监听中..."),
        HAND_CHANGE(3, "手动切替", "手动切替成功!");

        private int value;
        private String description;
        private String feedBackMsg;

        private int getValue() {
            return value;
        }

        private ESOP_CENTER_ACTION_TYPE(int value, String description, String feedBackMsg) {
            this.value = value;
            this.description = description;
            this.feedBackMsg = feedBackMsg;
        }

        private static ESOP_CENTER_ACTION_TYPE getByValue(int value) {
            for (ESOP_CENTER_ACTION_TYPE actionType : values()) {
                if (actionType.getValue() == value) {
                    return actionType;
                }
            }
            return null;
        }
    }
}
