package fr.aumgn.tobenamed.util;

import java.util.concurrent.TimeUnit;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;

import fr.aumgn.tobenamed.TBN;


public abstract class Timer implements Runnable {

    private static final int TICKS_PER_SECONDS = 20;

    private int majorDelay;
    private int minorDelay;

    private Runnable runnable;
    private int seconds;

    private int currentDelay;
    private int taskId;
    private long taskStartTime;
    private int pauseDelay;

    public Timer(int seconds, Runnable runnable) {
        this.majorDelay = TBN.getConfig().getMajorDelay();
        this.minorDelay = TBN.getConfig().getMinorDelay();

        this.seconds = seconds;
        this.runnable = runnable;

        this.currentDelay = 0;
    }

    @Override
    public void run() {
        seconds -= currentDelay;
        if (seconds > majorDelay) {
            scheduleAndPrintTime(majorDelay, ChatColor.YELLOW);
        } else if (seconds > minorDelay) {
            scheduleAndPrintTime(minorDelay, ChatColor.GOLD);
        } else if (seconds > 0) {
            scheduleAndPrintTime(1, ChatColor.RED);
        } else {
            runnable.run();
        }
    }

    private void scheduleAndPrintTime(int delay, ChatColor color) {
        long minutes = TimeUnit.SECONDS.toMinutes(seconds);
        String msg = String.format("%02d:%02d", minutes, seconds % 60);
        sendTimeMessage(color + msg);
        currentDelay = delay;
        taskStartTime = System.currentTimeMillis();
        taskId = TBNUtil.scheduleDelayed(delay * TICKS_PER_SECONDS, this);
    }

    public void pause() {
        Bukkit.getScheduler().cancelTask(taskId);
        pauseDelay = (int) ((System.currentTimeMillis() - taskStartTime ) / 1000);
        seconds -= pauseDelay;
    }

    public void resume() {
        scheduleAndPrintTime(currentDelay - pauseDelay, ChatColor.YELLOW); 
    }

    public abstract void sendTimeMessage(String string);
}

@@ -914,34 +914,16 @@
 jorDelay
-, ChatColor.YELLOW
 );%0A     
@@ -1007,24 +1007,8 @@
 elay
-, ChatColor.GOLD
 );%0A 
@@ -1078,23 +1078,8 @@
 me(1
-, ChatColor.RED
 );%0A 
@@ -1190,25 +1190,8 @@
 elay
-, ChatColor color
 ) %7B%0A
@@ -1350,13 +1350,26 @@
 age(
-color
+getColorFor(delay)
  + m
@@ -1529,32 +1529,298 @@
 , this);%0A    %7D%0A%0A
+    private ChatColor getColorFor(int delay) %7B%0A        if (seconds %3E majorDelay) %7B%0A            return ChatColor.YELLOW;%0A        %7D else if (seconds %3E minorDelay) %7B%0A            return ChatColor.GOLD;%0A        %7D else %7B%0A            return ChatColor.RED;%0A        %7D%0A    %7D%0A%0A
     public void 
@@ -2085,26 +2085,8 @@
 elay
-, ChatColor.YELLOW
 ); %0A

Fix timer colors