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 VariableSourceEnum implements ReadableEnum<VariableSourceEnum> {

    INPUT_OR_SELECT(10, "手动输入或选择"),

    AUTO_GENERATE(20, "自动生成");

    @EnumValue
    @JsonValue
    private final Integer code;

    private final String name;

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

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

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

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

}
