package com.djhk.common.enums;

import lombok.Getter;

/**
 * ip请求限制相关枚举
 */
public enum IpBlackLevleEnum {
    LEVEL_ONE(1,null,2,600L,300L,"IP锁定5分钟，并且10分钟内如果再次锁定，将升级为level2"),
    LEVEL_TWO(2,1,3,3000L,1500L,"IP锁定15分钟，并且30分钟内如果再次锁定，将升级为level3"),
    LEVEL_THREE(3,2,null,48 * 60 * 60L,24 * 60 * 60L,"IP锁定1天，并且2天内如果再次锁定，将继续锁定1天"),
    ;

    @Getter
    private Integer level;

    @Getter
    private Integer preLevel;

    @Getter
    private Integer nextLevel;

    @Getter
    private Long levelKeyTimeOut;

    @Getter
    private Long blackKeyTimeOut;

    @Getter
    private String desc;

    private IpBlackLevleEnum(Integer level, Integer preLevel, Integer nextLevel, Long levelKeyTimeOut, Long blackKeyTimeOut, String desc) {
        this.level = level;
        this.preLevel = preLevel;
        this.nextLevel = nextLevel;
        this.levelKeyTimeOut = levelKeyTimeOut;
        this.blackKeyTimeOut = blackKeyTimeOut;
        this.desc = desc;
    }

    public static IpBlackLevleEnum getByLevel(Integer level) {
        if (level == null) {
            return null;
        }

        for (IpBlackLevleEnum levleEnum : IpBlackLevleEnum.values()) {
            if (level.equals(levleEnum.getLevel())) {
                return levleEnum;
            }
        }

        return null;
    }

    public static IpBlackLevleEnum getByPreLevel(Integer preLevel) {
        if (preLevel == null) {
            return LEVEL_ONE;
        }

        for (IpBlackLevleEnum levleEnum : IpBlackLevleEnum.values()) {
            if (preLevel.equals(levleEnum.getPreLevel())) {
                return levleEnum;
            }
        }

        return LEVEL_THREE;
    }
}
