package cn.axzo.msg.center.notices.service.impl;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

/**
 * @author yanglin
 */
@Setter
@Getter
@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "user.cellphone")
public class MnsProperties {

    /**
     * 是否开启重复发送限制
     */
    private boolean sendLimitOn = true;

    /**
     * 不验证重复发送的内部模板编码
     */
    private String limitWhitelist;

    /**
     * 业务上每天发送重复内容的次数限制
     */
    private long bizLimitPerDay = 10;

    /**
     * 验证码15分钟内发送的内容都是一样的
     */
    private int verifyCodeLimitWindowMinutes = 15;

    /**
     * 验证码每15分钟可以重复发送的次数
     */
    private long verifyCodeLimitPerWindow = 20;

    @SuppressWarnings("BooleanMethodIsAlwaysInverted")
    public boolean shouldLimit(String templateNo) {
        return sendLimitOn && !isInLimitWhitelist(templateNo);
    }

    private boolean isInLimitWhitelist(String templateNo) {
        return StringUtils.isNotBlank(limitWhitelist)
                && limitWhitelist.contains(templateNo);
    }
}