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

    TEXT(10, "文本输入"),

    USER_ID(20, "用户"),

    ORG_ID(21, "组织机构"),

    ROLE_ID(22, "角色"),

    POSITION_ID(23, "职务"),

    DICT_ID(30, "字典");

    @EnumValue
    @JsonValue
    private final Integer code;

    private final String name;

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

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

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

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