package artistmetrics.actions;
import java.util.Date;

/**
 * An instance of a timer, which has a startTime and a timePassed.
 * When constructed, this class gets the current date, and sets the time passed from that date to 0.
 * @author bdw49
 *
 */
public class Timer {

	private Date startTime; // The time the timer was created
	private long timePassed; // The time that has passed since the timer's creation.
	
	/**
	 * Constructor for the class Timer. Sets the current time and defaults the time passed to 0. 
	 */
	public Timer() {
		startTime = new Date();
		timePassed = 0;
	}
	
	/**
	 * Updates the time passed currently recorded by the timer.
	 * This method is run whenever the timer's time passed needs to be read
	 */
	public void updateTimer()
	{
		Date now = new Date();
		long currentTime = now.getTime();
		long initialTime = startTime.getTime();
		timePassed = (currentTime - initialTime);
	}
	
	/**
	 * Returns the period of time that has passed since this timer's creation (in milliseconds)
	 * @return the time passed since the creation of this timer.
	 */
	public long getPeriod()
	{
		updateTimer();
		return timePassed;
	}
	
	/**
	 * Returns the start time of this timer in the Date format.
	 * @return the start time of this timer
	 */
	public Date getStartTime()
	{
		return startTime;
	}
	
	/**
	 * Returns the time passed by this timer
	 * @return the time passed by this timer, in milliseconds
	 */
	public long getTimePassed()
	{
		return timePassed;
	}
	
	/**
	 * Returns the time passed in a readable string format:
	 * "Time passed: X hours, Y minutes, Z seconds."
	 */
	public String toString()
	{
		long hours = timePassed / 3600000;
		long minutes = (timePassed / 60000) % 60;
		long seconds = (timePassed / 1000) % 60;
		
		return "Time passed: " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds.";  
	}
}
