package com.centreproject.common.enums;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 数据同步状态枚举
 * @author zhonglei.xu
 */
@Getter
public enum SyncStatus {
    RUNNING("0", "执行中"),
    SUCCESS("1", "成功"),
    FAILED("2", "失败"),
    RETRYING("3", "重试中");

    private final String code;
    private final String desc;

    private static final Map<String, SyncStatus> CODE_MAP = new HashMap<>();

    static {
        Arrays.stream(values()).forEach(e -> CODE_MAP.put(e.code, e));
    }

    SyncStatus(String code, String desc) {
        this.code = Objects.requireNonNull(code);
        this.desc = Objects.requireNonNull(desc);
    }

    /**
     * 通过编码获取枚举实例
     * @param code 状态编码
     * @return 对应的枚举实例，未找到时返回null
     */
    public static SyncStatus getByCode(String code) {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        return CODE_MAP.get(code.trim());
    }

    /**
     * 检查是否为成功状态
     */
    public boolean isSuccess() {
        return this == SUCCESS;
    }

    /**
     * 检查是否为失败状态
     */
    public boolean isFailed() {
        return this == FAILED;
    }

    /**
     * 检查是否为运行中状态
     */
    public boolean isRunning() {
        return this == RUNNING;
    }

    /**
     * 获取状态描述（带编码）
     */
    public String getFullDesc() {
        return this.code + " - " + this.desc;
    }

    /**
     * 验证状态编码是否有效
     */
    public static boolean isValidCode(String code) {
        return CODE_MAP.containsKey(code);
    }
}