package com.gallery.free.wallpaper.environment.ad.async;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public class Async {
    private static ThreadPoolExecutorWrapper sThreadPoolExecutorWrapper;

    private static ThreadPoolExecutorWrapper getThreadPoolExecutorWrapper() {
        if (sThreadPoolExecutorWrapper == null) {
            synchronized (Async.class) {
                if (sThreadPoolExecutorWrapper == null) {
                    sThreadPoolExecutorWrapper = new ThreadPoolExecutorWrapper(12, 12, 10);
//                    if (BuildConfig.DEBUG)
//                        LogUtil.d(LogFilterDef.APP_INIT, LogHelper.getFileLineMethod(1));
                }
            }
        }

        return sThreadPoolExecutorWrapper;
    }

    public static void run(Runnable task) {
        getThreadPoolExecutorWrapper().executeTask(task);
    }

    public static <T> Future<T> submit(Callable<T> task) {
        return getThreadPoolExecutorWrapper().submitTask(task);
    }

    public static boolean isMainThread() {
        return Thread.currentThread() == Looper.getMainLooper().getThread();
    }

    public static void schedule(long delay, Runnable task) {
        getThreadPoolExecutorWrapper().scheduleTask(delay, task);
    }

    public static void scheduleTaskAtFixedRateIgnoringTaskRunningTime(long initialDelay, long period, Runnable task) {
        getThreadPoolExecutorWrapper().scheduleTaskAtFixedRateIgnoringTaskRunningTime(initialDelay, period, task);
    }

    public static void scheduleTaskAtFixedRateIncludingTaskRunningTime(long initialDelay, long period, Runnable task) {
        getThreadPoolExecutorWrapper().scheduleTaskAtFixedRateIncludingTaskRunningTime(initialDelay, period, task);
    }

    public static boolean removeScheduledTask(Runnable task) {
        return getThreadPoolExecutorWrapper().removeScheduledTask(task);
    }

    public static void scheduleTaskOnUiThread(long delay, Runnable task) {
        getThreadPoolExecutorWrapper().scheduleTaskOnUiThread(delay, task);
    }

    public static void removeScheduledTaskOnUiThread(Runnable task) {
        getThreadPoolExecutorWrapper().removeScheduledTaskOnUiThread(task);
    }

    public static void runOnUiThread(Runnable task) {
        getThreadPoolExecutorWrapper().runTaskOnUiThread(task);
    }

    public static Handler getMainHandler() {
        return getThreadPoolExecutorWrapper().getMainHandler();
    }

    /*
    single background thread to execute Job
     */
    public static void scheduleInQueue(Runnable task) {
        getThreadPoolExecutorWrapper().scheduleOnQueue(task);
    }

    public static void shutdown() {
        if (sThreadPoolExecutorWrapper != null) {
            sThreadPoolExecutorWrapper.shutdown();
            sThreadPoolExecutorWrapper = null;
        }
    }
}
