package com.weiqi.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SmsTools {
    private static final Logger logger = LoggerFactory.getLogger(SmsTools.class);
    private static int SmsGlobalIndex = -1;
    // 项目启动时通过数据库加载数据，项目运行中通过页面控制修改每家流量
    public static List<com.weiqi.vo.SmsGlobalIndex> sv = new ArrayList<>();
    public static int sendTotalNum = 200000;
    public static boolean isRedisStatistic = true;
    public static boolean isRedisStatisticUP = false;

    // 模版总开关
    public static boolean isTemplateOpen = false;
    // 收集是否开启，如果开启则经过模版到保存到数据库
    public static boolean isMatching = false;
    // 拦截开关，如果开启则将不符合到直接拦截掉
    public static boolean isIntercept = false;

    public static ConcurrentHashMap<String, Long> FLOW_MAP = new ConcurrentHashMap<>();
    public static final String API_INCR_KEY = "api_key:";
    public static final String DISPATHER_INCR_KEY = "dispather_key:";
    public static final String TOOLS_INCR_KEY = "tools_key:";

    public static final String KEY_FREQUENCY_SWITCH_S = "sms:frequencySwitch:s";
    public static final String KEY_FREQUENCY_SWITCH_Y = "sms:frequencySwitch:y";
    public static final String KEY_FREQUENCY_SWITCH_MASTER = "sms:frequencySwitch:MasterSwitch";

    public static final String KEY_ONE_HOUR_LIMIT_S = "ONE_HOUR_LIMIT_S";
    public static final String KEY_SAME_CONTENT_LIMIT_S = "SAME_CONTENT_LIMIT_S";
    public static final String KEY_SENDING_TIMES_LIMIT_S = "SENDING_TIMES_LIMIT_S";
    public static final String KEY_ONE_HOUR_LIMIT_Y = "ONE_HOUR_LIMIT_Y";
    public static final String KEY_SAME_CONTENT_LIMIT_Y = "SAME_CONTENT_LIMIT_Y";
    public static final String KEY_SENDING_TIMES_LIMIT_Y = "SENDING_TIMES_LIMIT_Y";

    public static int ONE_HOUR_LIMIT_S = 1;
    public static int SAME_CONTENT_LIMIT_S = 1;
    public static int SENDING_TIMES_LIMIT_S = 20;
    public static int ONE_HOUR_LIMIT_Y = 1;
    public static int SAME_CONTENT_LIMIT_Y = 1;
    public static int SENDING_TIMES_LIMIT_Y = 20;

    public static String nextServerIndex() {
        int total = 0;
        int size = sv.size();
        if (size == 0)
            return "string.Empty;";// 不匹配任何sp

        for (int i = 0; i < size; i++) {

            sv.get(i).setCurrent(sv.get(i).getCurrent() + sv.get(i).getWeight());

            total += sv.get(i).getWeight();

            if (SmsGlobalIndex == -1 || sv.get(SmsGlobalIndex).getCurrent() < sv.get(i).getCurrent()) {

                SmsGlobalIndex = i;
            }
        }

        sv.get(SmsGlobalIndex).setCurrent(sv.get(SmsGlobalIndex).getCurrent() - total);

        logger.info("sv===={},{}", sv.toString(), sv.get(SmsGlobalIndex).getName());
        return sv.get(SmsGlobalIndex).getName();
    }

    public static void main(String[] args) {

        for (int i = 0; i < 1000; i++) {
            System.out.println(nextServerIndex());
        }
    }

    public void add(String key, int inrc) {
        key = key + System.currentTimeMillis() / 1000;
        if (FLOW_MAP.get(key) == null) {
            synchronized (this) {
                if (FLOW_MAP.get(key) == null) {
                    FLOW_MAP.put(key, 0L);
                }
            }
        }
        synchronized (this) {
            FLOW_MAP.put(key, FLOW_MAP.get(key) + inrc);
        }
    }
}
