package cn.tee3.manager.util;

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

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MyTimer {
    private static boolean isStop;
    private static Handler handler = new Handler();

    public static void start(int intervalTime, Task task) {
        if (task == null) {
            return;
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isStop) {
                    isStop = false;
                    return;
                }
                task.onTask();
                handler.postDelayed(this, intervalTime);
            }
        }, intervalTime);
    }

    public static void stop() {
        isStop = true;
        handler.removeCallbacksAndMessages(null);
    }

    public static void delayTask(Looper looper, int intervalTime, Task task) {
        if (task == null) {
            return;
        }
        new Handler(looper).postDelayed(new Runnable() {
            @Override
            public void run() {
                task.onTask();
            }
        }, intervalTime);
    }

    public interface Task {
        void onTask();
    }


    private static List<TimerBean> timerBeanList = new ArrayList<>();

    public static TimerBean singlePostTask(TimerTask task, long delay) {
        Timer timer = new Timer();
        timer.schedule(task, delay);
        TimerBean timerBean = new TimerBean(timer, task);
        timerBeanList.add(timerBean);
        return timerBean;
    }

    public static TimerBean cyclePostTask(TimerTask task, long delay, long period) {
        Timer timer = new Timer();
        timer.schedule(task, delay, period);
        TimerBean timerBean = new TimerBean(timer, task);
        timerBeanList.add(timerBean);
        return timerBean;
    }

    public static void cancel(TimerBean timerBean) {
        if (timerBean.getTimer() != null && timerBean.getTask() != null) {
            timerBean.getTask().cancel();
            timerBean.getTimer().cancel();
            timerBeanList.remove(timerBean);
        }
    }

    public static void release() {
        for (TimerBean timerBean : timerBeanList) {
            if (timerBean.getTimer() != null && timerBean.getTask() != null) {
                timerBean.getTask().cancel();
                timerBean.getTimer().cancel();
            }
        }
        timerBeanList.clear();
    }

    public static class TimerBean {
        private Timer timer;
        private TimerTask task;

        public TimerBean(Timer timer, TimerTask task) {
            this.timer = timer;
            this.task = task;
        }

        public Timer getTimer() {
            return timer;
        }

        public void setTimer(Timer timer) {
            this.timer = timer;
        }

        public TimerTask getTask() {
            return task;
        }

        public void setTask(TimerTask task) {
            this.task = task;
        }
    }
}
