package cn.stylefeng.team.portal.api.enums;

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

    ITEM_1(1, "内部路由"),

    ITEM_2(2, "外部链接");

    @EnumValue
    @JsonValue
    private final Integer code;

    private final String message;

    LinkRedirectTypeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static LinkRedirectTypeEnum codeToEnum(String code) {
        if (null != code) {
            for (LinkRedirectTypeEnum enumItem : LinkRedirectTypeEnum.values()) {
                if (enumItem.getCode().toString().equals(code)) {
                    return enumItem;
                }
            }
        }
        return null;
    }

    public static String codeToMessage(String code) {
        if (null != code) {
            for (LinkRedirectTypeEnum enumItem : LinkRedirectTypeEnum.values()) {
                if (enumItem.getCode().toString().equals(code)) {
                    return enumItem.getMessage();
                }
            }
        }
        return "未知";
    }

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

    @Override
    public Object getName() {
        return this.message;
    }

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

}