package com.gmscheduler.model;

/**
 * A <code>Timeframe</code> object represents a day and time range (start time and stop time). The time
 * frame is used to represent a chunk of time.
 * 
 * @author Peter Deschere
 * @author Rebecca Beale
 * @author Jason Fillo
 * @version 1.1
 */
public class Timeframe {
	//class constructor
	public Timeframe(String day, String start, String end) {
		this.day = new String(day);
		this.start = new String(start);
		this.end = new String(end);
	}
	
	//class constructor
	public Timeframe(String day, Double start, Double end) {
		this.day = new String(day);
		this.start = toString(start);
		this.end = toString(end);		
	}
	
	//copy constructor
	public Timeframe(Timeframe u) {
		day = new String(u.getDay());
		start = new String(u.getStart());
		end = new String(u.getEnd());
		if (!(u.getId() == null)) {id = new Long(u.getId());}
		if (!(u.getScheduleid() == null)) {scheduleid = new Long(u.getScheduleid());}
	}
	
	//default constructor
	public Timeframe() {
		day = null;
		start = null;
		end = null;
	}

	
	/**
	 * Returns the schedule id.
	 * 
	 * @return the id of the parent <code>Schedule</code>
	 */
	public Long getScheduleid() {
		return scheduleid;
	}
	
	/**
	 * Sets the parent <code>Schedule</code>'s id to the Timeframe. Required for Hibernate.
	 * 
	 * @param id the parent <code>Schedule</code>'s id
	 */
	public void setScheduleid(Long id) {
		if(id==null) {this.scheduleid = new Long(0);} else {this.scheduleid = id;}
	}
		
	/**
	 * Returns the id.
	 * 
	 * @return the id of the <code>Timeframe</code>
	 */
	public Long getId() {
		return id;
	}
	
	/**
	 * Sets the id. Required by hibernate.
	 * 
	 * @param id the id of the <code>Timeframe</code> in the database
	 */	
	private void setId(Long id) {
		this.id = id;
	}
	
	/**
	 * Returns the day.
	 * 
	 * @return the day of the <code>Timeframe</code>
	 */
	public String getDay() {
		return day;
	}

	/**
	 * Returns the start time.
	 * 
	 * @return the start time of the <code>Timeframe</code>
	 */
	public String getStart() {
		return start;
	}

	/**
	 * Returns the end.
	 * 
	 * @return the end of the <code>Timeframe</code>
	 */
	public String getEnd() {
		return end;
	}
	
	/**
	 * Sets the Day.
	 * 
	 * @param day the new day
	 */
	public void setDay(String day) {
		this.day = new String(day);
	}
	
	/**
	 * Sets the start time.
	 * 
	 * @param start the new start
	 */
	public void setStart(String start) {
		this.start = new String(start);
	}
	
	/**
	 * Sets the end time.
	 * 
	 * @param end the new end
	 */
	public void setEnd(String end) {
		this.end = new String(end);
	}

	public String toStringMilitary() {
		if (day==null || start==null || end == null) {
			return "";
			} else {
			return day + " " + start + "-" + end;
			}
	}
	
	
	public String toString() {
		Integer x = 0;
		Integer y = 0;
		String s = "";
		String t = "";
		
		if (day==null || start==null || end == null) {
			return "";
			} else {
			x = new Integer(start.substring(0,start.indexOf(":")));
			y = new Integer(end.substring(0,end.indexOf(":")));
			if (x > 12) {
				x = x-12;
				s = "PM";
			} else {
				s = "AM";
			}
				
			if (y > 12) {
				y = y-12;
				t = "PM";
			} else {
				t = "AM";
			}

			
			return day + " " + x.toString() + start.substring(start.indexOf(":")) + s +
				   "-" + y.toString() + end.substring(end.indexOf(":")) + t;
			}
	}
		
	/**
	 * Returns the start.
	 * 
	 * @return the start of the <code>Timeframe</code> as a <code>Double</code>
	 */
	public Double getStartDouble() {
		return this.toDouble(this.getStart());
	}
	
	/**
	 * Returns the end.
	 * 
	 * @return the end of the <code>Timeframe</code> as a <code>Double</code>
	 */
	public Double getEndDouble() {
		return this.toDouble(this.getEnd());
	}
	
	/**
	 * Converts a military time string into decimal hours.
	 * 
	 * @param time a string (format ##:##) in military time
	 * @return the time in decimal hours (ex: 12:15 returns 12.25)
	 */
	private Double toDouble(String time) {
		return new Double(time.substring(0,time.indexOf(":"))) + 
			new Double(time.substring(time.indexOf(":")+1))/60;
	}
	
	/**
	 * Converts a military time string into decimal hours.
	 * 
	 * @param time a Double representation of military time
	 * @return the time in string format (ex: 12.25 returns 12:15)
	 */
	public String toString(Double time) {
		return new Integer(time.intValue()).toString() + ":" + this.toStringMinutes(time);
	}
	
	/**
	 * Converts a decimal hours into just its minutes value.
	 * 
	 * @param time a Double representation of military time
	 * @return the time in string format (ex: 12.25 returns 15)
	 */
	private String toStringMinutes(Double time) {
		return new Integer(new Double((time - time.intValue())*60).intValue()).toString();
	}
	
	public boolean isEmpty() {
		if (day.compareTo(new String())==0 && 
			start.compareTo(new String())==0 &&
			end.compareTo(new String())==0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * The day of week
	 */
	private String day;
	/**
	 * The start time of the interval
	 */
	private String start;
	/**
	 * The end time of the interval
	 */
	private String end;
	/**
	 * The id of the Timeframe
	 */
	private Long id;
	/**
	 * The schedule id the Timeframe belongs
	 */
	private Long scheduleid;
}
