// CSC 442F - Software Engineering
// Software:    Calendar Client
// Component:   TimeDuration class
// Description: Calculates time difference

import java.util.*;

// Class to calculate duration time
// ================================
class TimeDuration
{
  protected TemporalPanel start;
  protected TemporalPanel end;
  protected DurationPanel duration;
  
  // Constructor
  // ===========
  public TimeDuration()
  {
  }
  
  
  public void initialize(TemporalPanel start_in, TemporalPanel end_in,
                         DurationPanel duration_in)
  {
    start = start_in;
    end = end_in;
    duration = duration_in;
  }
  
  
  public void calcDuration()
  {
    GregorianCalendar calendar;
    int ampm= 0;
    String startHour = start.hour.getText();
    String startMin = start.min.getText();
    String endHour = end.hour.getText();
    String endMin = end.min.getText();
    int startAmpm = start.ampm.getSelectedIndex();
    int endAmpm = end.ampm.getSelectedIndex();
    String hour_result;
    String min_result;

    // If time fields are filled, calculate duration time
    if (startHour.length()!=0 && startMin.length()!=0 &&
        endHour.length()!=0 && endMin.length()!=0)
    {
      // Adjust for AM/PM discrepancies
      if (endAmpm == 1 && Integer.parseInt(endHour)!=12)
        ampm = 12;
      if (endAmpm == 0 && Integer.parseInt(endHour)==12)
        ampm = -12;

      // Enter time into calendar object 
      calendar = new GregorianCalendar(1998, 11, 24, Integer.parseInt(endHour)
                                       + ampm, Integer.parseInt(endMin));

      // Adjust for AM/PM discrepancies
       ampm = 0;
      if (startAmpm == 1 && Integer.parseInt(startHour)!=12)
        ampm = 12;
      if (startAmpm == 0 && Integer.parseInt(startHour)==12)
        ampm = -12;

      // Calculate duration
      calendar.add(calendar.HOUR_OF_DAY,
                   -1*(Integer.parseInt(startHour) + ampm));
      calendar.add(calendar.MINUTE,
                   -1*(Integer.parseInt(startMin)));

      // Disable TextListener for duration fields before modifying
      duration.hour.removeTextListener(duration);
      duration.min.removeTextListener(duration);
  
      // Format hour nicely
      if (calendar.get(calendar.HOUR_OF_DAY) < 10)
      {
        hour_result = "0";
        hour_result =
        hour_result.concat(String.valueOf(calendar.get(calendar.HOUR_OF_DAY)));
      }
      else
        hour_result = String.valueOf(calendar.get(calendar.HOUR_OF_DAY));
      
      // Format minutes nicely
      if (calendar.get(calendar.MINUTE) < 10)
      {
        min_result = "0";
        min_result =
        min_result.concat(String.valueOf(calendar.get(calendar.MINUTE)));
      }
      else
        min_result = String.valueOf(calendar.get(calendar.MINUTE));
           
      // Write results in duration TextField
      duration.hour.setText(hour_result);
      duration.min.setText(min_result);

      // Restor TextListener
      duration.hour.addTextListener(duration);
      duration.min.addTextListener(duration);
    }
    // Else if not all values entered, fill duration time with zeros
    else
    {
      duration.hour.setText("00");
      duration.min.setText("00");
    }
  }    
  
}
