package net.northking.cctp.se.thread;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 执行线程池
 *
 */
public class ExecutorPool {

    private final static Map<String, Integer> statusMap = new ConcurrentHashMap<>();

    private static int PC_Max_Thread_Pool_Size = 1;

    private static int Mobile_Max_Thread_Pool_Size = 8;

    private static int Api_Max_Thread_Pool_Size = 10;

    private static int PC_Core_Thread_Pool_Size = 1;

    private static int Mobile_Core_Thread_Pool_Size = 8;

    private static int Api_Core_Thread_Pool_Size = 10;

    public static boolean enablePc = true;

    public static boolean enableMobile = true;

    public static boolean enableApi = true;

    /**
     * PC是单线程池
     */
    private static ThreadPoolExecutor PC_POOL;
    /**
     * 移动自动化多线程池
     */
    private static ThreadPoolExecutor MOBILE_POOL;

    /**
     * 接口自动化多线程池
     */
    private static ThreadPoolExecutor API_POOL;

    /**
     * 初始化线程池
     * @param enablePc
     * @param pcSize
     * @param enableMobile
     * @param mobileSize
     * @param enableApi
     * @param apiSize
     */
    public ExecutorPool(Boolean enablePc, Integer pcSize,Integer pcCoreSize,
                        Boolean enableMobile, Integer mobileSize, Integer mobileCoreSize,
                        Boolean enableApi, Integer apiSize ,Integer apiCoreSize) {
        if (null != enablePc) {
            ExecutorPool.enablePc = enableApi;
        }
        if (null != enableMobile) {
            ExecutorPool.enableMobile = enableMobile;
        }
        if (null != enableApi) {
            ExecutorPool.enableApi = enableApi;
        }
        if (null != pcSize) {
            PC_Max_Thread_Pool_Size = pcSize;
        }
        if (null != mobileSize) {
            Mobile_Max_Thread_Pool_Size = mobileSize;
        }
        if (null != apiSize) {
            Api_Max_Thread_Pool_Size = apiSize;
        }
        if (null != pcCoreSize) {
            PC_Core_Thread_Pool_Size = pcCoreSize;
        }
        if (null != mobileCoreSize) {
            Mobile_Core_Thread_Pool_Size = mobileCoreSize;
        }
        if (null != apiCoreSize) {
            Api_Core_Thread_Pool_Size = apiCoreSize;
        }
        PC_POOL = new ThreadPoolExecutor(PC_Core_Thread_Pool_Size, PC_Max_Thread_Pool_Size,
                0L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
        MOBILE_POOL = new ThreadPoolExecutor(Mobile_Core_Thread_Pool_Size, Mobile_Max_Thread_Pool_Size,
                0L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
        API_POOL = new ThreadPoolExecutor(Api_Core_Thread_Pool_Size, Api_Max_Thread_Pool_Size,
                10L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

    public static boolean isPCAvailable() {
        return enablePc && PC_POOL.getActiveCount()  < PC_Max_Thread_Pool_Size;
    }

    public static boolean isMobileAvailable() {
        return enableMobile && MOBILE_POOL.getActiveCount() < Mobile_Max_Thread_Pool_Size;
    }

    public static boolean isApiAvailable() {
        return enableApi && API_POOL.getActiveCount() < Api_Max_Thread_Pool_Size;
    }

    public static ThreadPoolExecutor getPCPool() {
        return PC_POOL;
    }

    public static ThreadPoolExecutor getMobilePool() {
        return MOBILE_POOL;
    }

    public static ThreadPoolExecutor getApiPool() {
        return API_POOL;
    }

    /**
     * 获取各个线程池正在运行的线程数量
     * @return
     */
    public static Map<String, Integer> status() {
        if (enablePc)statusMap.put("pc", PC_POOL.getActiveCount());
        if(enableMobile)statusMap.put("mobile", MOBILE_POOL.getActiveCount());
        if(enableApi)statusMap.put("api", API_POOL.getActiveCount());
        return statusMap;
    }

    /**
     * 获取线程池信息
     * @return
     */
    public static Map<String, Object> info() {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("enablePC", enablePc);
        map.put("pcThread", PC_Max_Thread_Pool_Size);
        if (enablePc)map.put("pc_thread_active", PC_POOL.getActiveCount());
        map.put("enableMobile", enableMobile);
        map.put("mobileThread", Mobile_Max_Thread_Pool_Size);
        if(enableMobile)map.put("mobile_thread_active", MOBILE_POOL.getActiveCount());
        map.put("enableApi", enableApi);
        map.put("apiThread", Api_Max_Thread_Pool_Size);
        if(enableApi)map.put("api_thread_active", API_POOL.getActiveCount());
        return map;
    }

    /**
     * 线程不再接收新的任务
     *
     */
    public static void stopAll() {
        ExecutorPool.enablePc = false;
        ExecutorPool.enableMobile = false;
        ExecutorPool.enableApi = false;
    }

}
