package com.app.baselibrary.utils;

import android.util.Log;

import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * Date : 2018/5/8.
 * Author:  cxh
 * 计时器工具
 */

public class TimerTaskHelper {

    private static TimerTaskHelper instance;

    ScheduledExecutorService service = Executors.newScheduledThreadPool(8);

    private HashMap<String, Future> mFutures = new HashMap<>();

    public static TimerTaskHelper getInstance() {

        if (instance == null) {
            synchronized (TimerTaskHelper.class) {
                if (instance == null) {
                    instance = new TimerTaskHelper();
                }
            }
        }

        return instance;
    }

    private TimerTaskHelper() {
    }

    public void start(String name, Runnable timerTask, long delay, long period) {
        if (mFutures.containsKey(name)) {
            Future future = mFutures.get(name);
            if (future != null && !future.isCancelled() && !future.isDone()) {
                Log.e("start", "存活着。。。。。"+name);
                return;
            }
        }
        Log.e("start", "执行。。。。。"+name);
        ScheduledFuture<?> future = service.scheduleAtFixedRate(timerTask, delay, period,
                TimeUnit.MILLISECONDS);
        mFutures.put(name, future);
    }

    public void start(String name, Runnable timerTask, long delay) {
        if (mFutures.containsKey(name)) {
            Future future = mFutures.get(name);
            if (future != null && !future.isCancelled() && !future.isDone()) {
                Log.e("start", "存活着。。。。。"+name);
                return;
            }
        }
        ScheduledFuture<?> future = service.schedule(timerTask, delay,
                TimeUnit.MILLISECONDS);
        mFutures.put(name, future);
    }

    public boolean isRun(String name){
        if (mFutures.containsKey(name)) {
            Future future = mFutures.get(name);
            if (future != null && !future.isCancelled() && !future.isDone()) {
                return true;
            }
        }
        return false;
    }

    public void stop(String name) {
        try {
            if (mFutures.containsKey(name)) {
                Future future = mFutures.remove(name);
                if (future != null && !future.isDone() && !future.isCancelled()) {
                    Log.e("start", "关闭。。。。。"+name);
                    future.cancel(true);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stopAll() {
        for (String name : mFutures.keySet()) {
            try {
                Future future = mFutures.remove(name);
                if (future != null && !future.isDone() && !future.isCancelled()) {
                    future.cancel(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
