package cn.stylefeng.workflow.api.enums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.base.ReadableEnum;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

@Getter
public enum SummarySourceEnum implements ReadableEnum<SummarySourceEnum> {

    FORM_VARIABLE(1, "来自表单字段"),

    CUSTOM_VUE_VARIABLE(2, "来自流程变量");

    @EnumValue
    @JsonValue
    private final Integer code;

    private final String name;

    SummarySourceEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static SummarySourceEnum codeToEnum(Integer code) {
        if (null != code) {
            for (SummarySourceEnum item : SummarySourceEnum.values()) {
                if (item.getKey().equals(code)) {
                    return item;
                }
            }
        }
        return null;
    }

    public static String getInstanceStatusName(Integer value) {
        for (SummarySourceEnum instanceStatusEnum : SummarySourceEnum.values()) {
            if (instanceStatusEnum.getCode().equals(value)) {
                return instanceStatusEnum.getName();
            }
        }
        return "";
    }

    @Override
    public Object getKey() {
        return code;
    }

    @Override
    public SummarySourceEnum parseToEnum(String originValue) {
        if (ObjectUtil.isEmpty(originValue)) {
            return null;
        }
        for (SummarySourceEnum value : SummarySourceEnum.values()) {
            if (value.code.equals(Convert.toInt(originValue))) {
                return value;
            }
        }
        return null;
    }
}
