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

    WAIT_TO_DO(10, "待办"),

    WAIT_TO_READ(20, "待阅"),

    ALREADY_DO(30, "已办"),

    ALREADY_READ(40, "已阅"),

    MY_SUBMIT(50, "我发起的");

    @EnumValue
    @JsonValue
    private final Integer status;

    private final String name;

    TaskConditionEnum(Integer value, String name) {
        this.status = value;
        this.name = name;
    }

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

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

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

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