package com.gic.haoban.manage.service.util;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.gic.haoban.manage.service.dao.mapper.content.holo.HoloDataSource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ApolloUtils {

    private static final Logger log = LoggerFactory.getLogger(ApolloUtils.class);

    /**
     * 旧的调用发送，是否采用新的消息中心开关1：是 0：否
     * @return
     */
    public static boolean useMessageCenter() {
        Config config = ConfigService.getConfig("COMMON.gic-properties");
        String messageCenterSwitch = config.getProperty("messageCenterSwitch", "0");
        return "1".equals(messageCenterSwitch);
    }

    /**
     * 分享朋友圈压缩开关
     * @return
     */
    public static boolean compressShareEid(String wxEnterpriseId) {
        Config config = ConfigService.getAppConfig();
        String compressShareEid = config.getProperty("compressShareEid", "");
        if (StringUtils.isBlank(compressShareEid)) {
            return true;
        }
        boolean contains = compressShareEid.contains(wxEnterpriseId);
        log.info("分享朋友圈压缩开关:{}", contains);
        return contains;
    }

    /**
     * 导购通知一批次间隔时间
     * @return
     */
    public static Integer notifyBatchInterval() {
        Config config = ConfigService.getAppConfig();
        String notifyBatchInterval = config.getProperty("notifyBatchInterval", "60");
        log.info("导购通知一批次间隔时间:{}", notifyBatchInterval);
        return Integer.parseInt(notifyBatchInterval);
    }

    /**
     * 字体
     * @return
     */
    public static String combinedFonts() {
        Config config = ConfigService.getAppConfig();
        String combinedFonts = config.getProperty("combinedFonts", "");
        return combinedFonts;
    }

    public static Integer combinedFontsType() {
        Config config = ConfigService.getAppConfig();
        String combinedFontsType = config.getProperty("combinedFontsType", "0");
        return Integer.parseInt(combinedFontsType);
    }

    public static boolean combinedCache() {
        Config config = ConfigService.getAppConfig();
        String combinedCache = config.getProperty("combinedCache", "");
        if (StringUtils.isBlank(combinedCache)) {
            return false;
        }
        return true;
    }

    /**
     * 线程池核心线程数
     * @return
     */
    public static Integer combinedCorePoolSize() {
        Config config = ConfigService.getAppConfig();
        String corePoolSize = config.getProperty("combinedCorePoolSize", "20");
        return Integer.parseInt(corePoolSize);
    }

    /**
     * 线程池最大线程数
     * @return
     */
    public static Integer combinedMaximumPoolSize() {
        Config config = ConfigService.getAppConfig();
        String maximumPoolSize = config.getProperty("combinedMaximumPoolSize", "40");
        return Integer.parseInt(maximumPoolSize);
    }

    /**
     * holo连接池最大空闲时间
     * @return
     */
    public static Integer connectionMaxIdleMs() {
        Config config = ConfigService.getAppConfig();
        String connectionMaxIdleMs = config.getProperty(HoloDataSource.HOLO_CONNECTIONMAXIDLEMS, "9000");
        return Integer.parseInt(connectionMaxIdleMs);
    }

    public static boolean filterEid(String eid) {
        Config config = ConfigService.getAppConfig();
        String filterEid = config.getProperty("filterEid", "");
        if (StringUtils.isBlank(filterEid)) {
            return false;
        }
        return filterEid.contains(eid);
    }

    public static List<String> filterMenuCode() {
        Config config = ConfigService.getAppConfig();
        String filterMenuCode = config.getProperty("filterMenuCode", "");
        if (StringUtils.isBlank(filterMenuCode)) {
            return new ArrayList<>();
        }
        return Arrays.stream(filterMenuCode.split(",")).collect(Collectors.toList());
    }
}
