package com.webtimer.timer;

import java.util.Timer;
import java.util.TimerTask;

public class CountdownTimer {

	private Timer timer;

	private final int DELAY = 0;
	private final int UPDATE_INTERVAL = 1000;

	private final int DEFAULT_INTERVAL = 90000; //the countdown interval - default: 90 seconds
	
	private static int interval;
	private static int interval2;
	private static int countdown = 3000; //the time left over in the current interval
	private static String comments = "";
	boolean isRunning = false;
	
	/*
	 * Default values to change the interval with.
	 */
	//Todo: move these to a conf file.
	private static int plusTime = 10000;
	private static int minusTime = 10000;
	
	/*
	 * The interval must be between <lowerLimit> and <higherLimit>
	 */
	private static int lowerLimit = 20000;
	private static int higherLimit = 120000;
	
	//////////////////////////////////////////////////////////////////////////////////
	
	/* GETTER AND SETTERS */
	
	public static int getInterval() {
		return interval;
	}

	public static void setInterval(int interval) {
		CountdownTimer.interval = interval;
	}
	
	public static int getInterval2() {
		return interval2;
	}

	public static void setInterval2(int interval) {
		CountdownTimer.interval2 = interval;
	}

	public static int getCountdown() {
		return countdown;
	}

	public static void setCountdown(int countdown) {
		CountdownTimer.countdown = countdown;
	}
	
	public static String getComments(){
		return comments;
	}
	
	public static void setComments(String c){
		CountdownTimer.comments = c;
	}
	
	////////////////////////////////////////////////////////////////////////////////
	
	/*METHODS*/
	
	/*
	 * Start the timer and keep it going.
	 */
	boolean start(){
		interval = this.DEFAULT_INTERVAL;
		interval2 = this.DEFAULT_INTERVAL;
		
		timer = new Timer();
		timer.scheduleAtFixedRate(
				new TimerTask(){
					@Override
					public void run(){
						timeStep();
						System.out.println("Timer is at: " + String.valueOf(countdown));
					}
				},
				this.DELAY,
				this.UPDATE_INTERVAL
				);
		isRunning=true;
		return isRunning;
	}

	/*
	 * Stop the timer.
	 */
	boolean stop(){
		timer.cancel();
		isRunning=false;
		return isRunning;
	}

	/*
	 * Helper method to update the time left over in the current run.
	 * Reset interval when it runs out.
	 */
	private void timeStep(){
		if (countdown <= 0){
			countdown = interval;
			interval = interval2;
			interval2 = this.DEFAULT_INTERVAL;
		}
		countdown -= UPDATE_INTERVAL;
	}
	
	/*
	 * Add a bit of extra time to the next timer.
	 * Must be between the two limits.
	 */
	static void doPlus(){
		if (interval >= CountdownTimer.lowerLimit && interval < CountdownTimer.higherLimit){
		interval += plusTime;
		}
	}
	
	/*
	 * Subtract a bit of extra time from the next timer.
	 * Must be between the two limits.
	 */
	static void doMinus(){
		if (interval > CountdownTimer.lowerLimit && interval <= CountdownTimer.higherLimit){
			interval -= minusTime;
		}
	}
	
	//TODO: DRY-principle for interval2
	
	/*
	 * Add a bit of extra time to the next timer.
	 * Must be between the two limits.
	 */
	static void doPlus2(){
		if (interval2 >= CountdownTimer.lowerLimit && interval2 < CountdownTimer.higherLimit){
		interval2 += plusTime;
		}
	}
	
	/*
	 * Subtract a bit of extra time from the next timer.
	 * Must be between the two limits.
	 */
	static void doMinus2(){
		if (interval2 > CountdownTimer.lowerLimit && interval2 <= CountdownTimer.higherLimit){
			interval2 -= minusTime;
		}
	}
	
	static void clearComments(){
		//TODO: write javadoc
		CountdownTimer.comments = "";
	}
	
	static void addToComments(String c){
		//TODO: write javadoc
		CountdownTimer.comments = c + "<BR>" + CountdownTimer.comments;
	}
}

@@ -838,16 +838,63 @@
 0000;%0A%09%0A
+%09private static int maxLinesUserComments = 5;%0A%0A
 %09///////
@@ -3691,32 +3691,152 @@
 : write javadoc%0A
+%09%09if (CountdownTimer.comments.isEmpty())%7B%0A%09%09%09CountdownTimer.comments = c;%0A%09%09%09return;%0A%09%09%7D%0A%09%09checkAndLimitUserComments();%0A
 %09%09CountdownTimer
@@ -3855,16 +3855,17 @@
 c + %22%3CBR
+/
 %3E%22 + Cou
@@ -3889,10 +3889,763 @@
 nts;%0A%09%7D%0A
-%7D
+%09%0A%09/**%0A%09 * check the number of lines with user comments and limit them (remove the first one) if necessary%0A%09 * %0A%09 * This method checks if the number of lines is not going to exceed the%0A%09 * maximum number when you add one. ie.: this function limits the user%0A%09 * comments to maxLinesUserComments minus one. Although a small effect, but%0A%09 * it limits the memory footprint and should reduce a couple of computing%0A%09 * cycles.%0A%09 */%0A%09static private void checkAndLimitUserComments()%7B%0A%09%09String%5B%5D l_comments = comments.split(%22%3CBR/%3E%22);%0A%09%09if (l_comments.length %3E maxLinesUserComments -1 )%7B%0A%09%09%09comments = l_comments%5BmaxLinesUserComments - 2%5D;%0A%09%09%09for (int i = maxLinesUserComments - 2 -1; i %3E= 0; i--)%7B%0A%09%09%09%09comments = l_comments%5Bi%5D + %22%3CBR/%3E%22 + comments;%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%7D%0A
 %0A

Limit the number of user comments