package com.example.model;

/**
   An Event object represents a length of time on a specific day
   during which the user is busy. An Event has a day of the week,  
   start time, and an end time. 
   @author Ryan Whited, Chris Sobona, Brian Johnson
   @version 2006-05-14
*/

public class Event
{
  /**
     Default constructor - initializes data to 0   
  */
  public Event()
  {
    dayOfWeek = 0;
    startTime = 0;
    endTime = 0;
  }

  /**
     Constructor given a day id, start time, and end time
     @param id day of the week(Sunday = 0, Monday = 1, etc.)
     @param s start time
     @param e end time
  */
  public Event(int day, int s, int e)
  {
    this.dayOfWeek = day;
    this.startTime = s;
    this.endTime = e;
  }

  /** 
     @return the Event's day of the week
  */
  public int getDay()
  {
    return this.dayOfWeek;
  } 

  /**
     @return the Event's start time
  */ 
  public int getStartTime()
  {
    return this.startTime;
  }

  /**
     @return the Event's end time
  */
  public int getEndTime()
  {
    return this.endTime;
  }

  /**
     @param t the Event's start time
  */
  public void setStartTime(int t)
  {
    this.startTime = t;
  }

  /** 
     @param t the Event's end time
  */
  public void setEndTime(int t)
  {
    this.endTime = t;
  }

  /**
     @param id the Event's day of the week
  */
  public void setDay(int id)
  {
    this.dayOfWeek = id;
  }

  /**
     @return a String representing the Event
  */
  public String formatTime()
  {
    String temp = "";
    switch(this.dayOfWeek)
    {
      case 1:
        temp = "Sun ";
        break;
      case 2:
        temp = "Mon ";
        break;
      case 3:
        temp = "Tue ";
        break;
      case 4:
        temp = "Wed ";
        break;
      case 5:
        temp = "Thu ";
        break;
      case 6:
        temp = "Fri ";
        break;
      case 7:
        temp = "Sat ";
        break;
    }

    return temp + Schedule.timeConvert(this.startTime, this.endTime);
  }

  /** Takes a minimum and maximum time (in military time) and returns a String
      in the format 11:00AM-2:30PM, where min=1100 and max=1430
      @param min the minimum time
      @param max the maximum time
      @return formatted String of the form 11:00AM-2:30PM
  */
  private static String timeConvert(int min, int max)
  {
    int h = min / 100;
    int m = min % 100;
    String amPm;
    String result = "";

    if(h >= 0 && h < 12)
      amPm = "AM";
    else amPm = "PM";

    if(h > 12)
      h = h % 12;
    if(h == 0)
      h = 12;

    if(m == 0)
      result += (h + ":00" + amPm + "-");
    else
      result += (h + ":" + m + amPm + "-");

    h = max / 100;
    m = max % 100;

    if((h >= 0 && h < 12) || h==24)
      amPm = "AM";
    else
      amPm = "PM";

    if(h > 12)
      h = h % 12;
    if(h == 0)
      h = 12;

    if(m == 0)
      result += (h + ":00" + amPm);
    else
      result += (h + ":" + m + amPm);

    return result;
  }

  /**
     Takes an Event, e, and returns true if e has the same day of the week,
     start time, and end time as the calling Event. Otherwise, returns false.
     @param e the Event
     @return true if the Events are equal, false otherwise
  */
  public boolean equals(Event e)
  {
    if(this.dayOfWeek == e.dayOfWeek &&
       this.startTime == e.startTime &&
       this.endTime == e.endTime)
         return true;
    else
      return false;
  }

  private int dayOfWeek;  // the Event's day of the week
  private int startTime;  // the Event's start time	
  private int endTime;    // the Event's end time
}
