/** A TimeInterval object represents an interval of time during
    which all members of a group are free.  Each TimeInterval 
    object contains a start time and an end time each stored as
    integers in universal time, eg 2:00PM is stored as 1400.
  @author Anthony Furst
  @version TimeInterval1.1
  @since TimeInterval1.1
*/

package com.example.model;

import java.io.*;
import java.util.*;
import java.lang.*;

public class TimeInterval {

    /** Constructor
	@param sTime The start time for the interval.
	@param eTime The end time for the interval.
    */
    public TimeInterval(int sTime, int eTime) {
    	startTime = sTime;
    	endTime = eTime;
    }

    /** Copy constructor
	@param interval The time interval to be copied
    */
    public TimeInterval(TimeInterval interval) {
	startTime = interval.getStart();
	endTime = interval.getEnd();
    }

    /** Return interval's start time.
	@return The interval's start time.
    */
    public int getStart() {
   	 return startTime;
    }

    /** Return interval's end time.
	@return The interval's end time.
    */
    public int getEnd() {
    	return endTime;
    }

    /** Converts the interval's start time from an integer in universal
	time to a string in standard time.  1745 would be returned as
	"15:45 PM".
	@return A string representation of the start time.
    */
    public String getStartTimeString() { 
	int hour = startTime / 100;
	int minutes = startTime % 100;
	String sHour;
	String sMinutes;
	String partOfDay;

	if (hour < 12)
	    partOfDay = "AM";
	else
	    partOfDay = "PM";

	//Extract hour and convert to standard time.
	if (hour == 0)
	   sHour = String.valueOf(12);
	else if (hour > 12)
	    sHour = String.valueOf(hour - 12);
	else
	    sHour = String.valueOf(hour);

	//Extract minutes and convert to two digit string.
	if (minutes < 10) 
	   sMinutes = "0" + minutes;
	else 
	   sMinutes = String.valueOf(minutes);

	return (sHour + ":" + sMinutes + " " + partOfDay);
    }

    /** Converts the interval's end time from an integer in universal
	time to a string in standard time.  1745 would be returned as
	"15:45 PM".
	@return A string representation of the start time.
    */
    public String getEndTimeString() { 
	int hour = endTime / 100;
	int minutes = endTime % 100;
	String sHour;
	String sMinutes;
	String partOfDay;

	if (hour < 12)
	    partOfDay = "AM";
	else
	    partOfDay = "PM";

	//Extract hour and convert to standard time.
	if (hour == 0)
	   sHour = String.valueOf(12);
	else if (hour > 12)
	    sHour = String.valueOf(hour - 12);
	else
	    sHour = String.valueOf(hour);

	//Extract minutes and convert to two digit string.
	if (minutes < 10) 
	   sMinutes = "0" + minutes;
	else 
	   sMinutes = String.valueOf(minutes);

	return (sHour + ":" + sMinutes + " " + partOfDay);
    }

    /** Set the start time of the interval.
	@param sTime The new start time.
    */
    public void setStart(int sTime) {
    	startTime = sTime;
    }

    /** Set the end time of the interval.
	@param eTime The new end time.
    */
    public void setEnd(int eTime) {
    	endTime = eTime;
    }

    /** Checks if a particular time interval occurs completely before
	this interval.
	@param times The time interval to be checked.
	@return True if times is before this interval, false if it
	        is not.
    */
    public boolean isBefore(TimeInterval times) {
    	if (endTime <= times.getStart())
	    return true;
    	else
	    return false;
    }

    /** Checks if a particular time interval occurs completely after
	this interval.
	@param times The time interval to be checked.
	@return True if times is after this interval, false if it
	        is not.
    */    
    public boolean isAfter(TimeInterval times) {
    	if (startTime >= times.getEnd())
    	    return true;
     	else 
	    return false;
    }

    /** Checks if a particular time interval occurs completely during
	this interval.
	@param times The time interval to be checked.
	@return True if times is during this interval, false if it
	        is not.
    */
    public boolean isDuring(TimeInterval times) {
    	if (startTime >= times.getStart() &&
	    endTime <= times.getEnd())
	    return true;
    	else
	    return false;
    }

    /** Checks if a particular time interval starts before this interval
	but ends during it.
	@param times The time interval to be checked.
	@return True if times starts before this interval and ends during
	      	it, false otherwise.
    */
    public boolean isStartOut(TimeInterval times) {
    	if (startTime < times.getStart() && 
	    (endTime > times.getStart() && 
	     endTime <= times.getEnd()))
 	    return true;
    	else 
 	    return false;
    }

    /** Checks if a particular time interval ends after this interval
	but starts during it.
	@param times The time interval to be checked.
	@return True if times ends after this interval and starts during
	      	it, false otherwise.
    */
    public boolean isEndOut(TimeInterval times) {
    	if (endTime > times.getEnd() && 
	    (startTime >= times.getStart() && 
	     startTime < times.getEnd()))
 	    return true;
    	else 
 	    return false;
    }

    /** Returns the amount of time spanned by the interval.
	@return The amount of time spanned by the interval.
    */
    public int difference() {
	int startHour = startTime / 100;
	int startMin = startTime % 100;
	int endHour = endTime / 100;
	int endMin = endTime % 100;

	endMin += 60 * (endHour - startHour);
    
	return ((endMin - startMin));
    }
    
    private int startTime;
    private int endTime;
}