package cd.casic.ci.process.engine.scheduler.enums;

import lombok.Getter;

import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@Getter
public enum PipelineSchedulingStatusEnum {
    INIT("0","初始化"),
    READY("1","就绪"),
    RUNNING("2","运行"),
    SUSPEND("3","挂起"),
    STOP("-1","停止"),
    HAPPY_ENDING("4","执行成功"),
    BAD_ENDING("5","执行失败")
    ;

    private String code;
    private String msg;
    private static final Map<PipelineSchedulingStatusEnum, Set<PipelineSchedulingStatusEnum>> TRANSITIONS = new EnumMap<>(PipelineSchedulingStatusEnum.class);

    static {
        TRANSITIONS.put(INIT, Set.of(READY, SUSPEND, BAD_ENDING, STOP));
        TRANSITIONS.put(READY, Set.of(READY,RUNNING, SUSPEND, BAD_ENDING, STOP));
        TRANSITIONS.put(RUNNING, Set.of(RUNNING,SUSPEND, HAPPY_ENDING, BAD_ENDING, STOP));
        TRANSITIONS.put(SUSPEND, Set.of(SUSPEND,INIT, READY, BAD_ENDING, RUNNING,STOP));
        //...初始化其他状态转移关系
    }

    PipelineSchedulingStatusEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;

    }
    public static Boolean canGoto(PipelineSchedulingStatusEnum from, PipelineSchedulingStatusEnum to){
        try {
            if (Objects.isNull(from) || Objects.isNull(to)) {
                return false;
            }
            return TRANSITIONS.get(from).contains(to);
        } catch (Exception e){
            return false;
        }
    }
    public static PipelineSchedulingStatusEnum getByCode(String code){
        for (PipelineSchedulingStatusEnum value : values()) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        return null;
    }
}
