import java.awt.*;
import java.awt.event.*;
 
/**
 * CSC 442F:    Software Engineering
 * Software:    Calendar Client
 * Component:   TemporalPanel class
 * Description: The TemproalPanel class produces an temporal panel. This panel
 *              accepts date input from the user. It also enforces type
 *              restrictions.
 *
 * @author David Tam
 * @version 1.0
 */
public class TemporalPanel extends DurationPanel 
                           implements TextListener, ItemListener
{ 
  public Choice ampm;
  protected TimeDuration timeDuration;

 /**
  * Constructor 1 -- Create panel with specific values
  * =============
  *
  * @param frame_in       Used to create error message dialogs.
  * @param timeDuration   Used to calculate time differences.
  * @param eventId_in     Used to set specific values for temporal fields.
  */
  public TemporalPanel (Frame frame_in, EventId eventId_in,
                        TimeDuration timeDuration_in)
  {
    super(frame_in, eventId_in);
    timeDuration = timeDuration_in;
  }

 /**
  * Constructor 2 -- Create panel with empty values
  * =============
  *
  * @param frame_in          Used to create error message dialogs.
  * @param timeDuration      Used to calculate time differences.
  */
  public TemporalPanel (Frame frame_in, TimeDuration timeDuration_in)
  {
    super(frame_in);
    timeDuration = timeDuration_in;
  }


 /**
  * Method to perform basic initialization
  * ======================================
  *
  */
  protected void basicInit(Frame frame_in)
  {

    super.basicInit(frame_in);

    // Choice instantiation
    ampm  = new Choice();

    // Adding choice items
    ampm.addItem("AM");
    ampm.addItem("PM");

    // Item listeners
    ampm.addItemListener(this);
  }
  
  
 /**
  * Function that enters specific values into fields
  * ================================================
  * Since no error checking is performed in the time values
  * errors could potentially appear 
  */
  protected void fillSpecific()
  {
    // Fill hour field
    if (eventId.hour.length()!=0)
    {
      // Check to see if need to perform AM/PM conversion
      // If PM
      if (Integer.parseInt(eventId.hour) > 12)
      {
        hour.setText(String.valueOf(Integer.parseInt(eventId.hour) - 12));
        ampm.select("PM");
      }
      // Else it is AM
      else
      {
        hour.setText(eventId.hour);
        ampm.select("AM");
      }
    }
    
    // Fill minute field
    if (eventId.minute.length()!=0)
      min.setText(eventId.minute);
  }


 /**
  * Function that does layout of panel
  * ==================================
  */
  protected void arrange()
  {
    super.arrange();

    setLayout(new GridBagLayout());
    constraints.gridy=0;
    constraints.gridx=3;
    add(ampm, constraints);
  }


 /**
  * Method to toggle input fields between editable and noneditable
  * ==============================================================
  *
  * @param value   A boolean value that set field to editable or noneditable
  */
  public void setEditable(boolean value)
  {
    String item;
    
    super.setEditable(value);
    item = ampm.getSelectedItem();
    ampm.removeAll();
    ampm.add(item);
  }
  
  
 /**
  * Method to handle TextField events
  * =================================
  */
  public void textValueChanged(TextEvent event)
  {
    restrict=true; 
    super.textValueChanged(event);

    // Perform calculation only if user requested at instantiation time
    if (timeDuration!=null)
      timeDuration.calcDuration();    
  }

  // Method to handle item select events
  // ===================================
  public void itemStateChanged(ItemEvent item)
  {
    // Perform calculation only if user requested at instantiation time
    if (timeDuration!=null)
      timeDuration.calcDuration();
  }
  
  
 /**
  * Method to send duration back to calling function
  * ================================================
  * 
  * @return   Returns a String in the form of the protocol.
  *           It converts time to 24hr.
  */
  public String buildResponse()
  {
    // Local variables
    MsgBox msgbox;
    String result = "";
    int hour_out;
    
    // If hour field is blank
    if (hour.getText().length()==0)
    {
      msgbox = new MsgBox(frame, "Error -- Invalid hour",
     "Please enter a value in the hour field(s).",450,150);
      msgbox.setVisible(true);
      result = "";
    }
    // If minute field is blank
    else if (min.getText().length()==0)
    {
      msgbox = new MsgBox(frame, "Error -- Invalid minute",
     "Please enter a value in the minute field(s).",450,150);
      msgbox.setVisible(true);
      result = "";
    }
    // Else fields are valid so build appropriate response
    else
    {
      hour_out = Integer.parseInt(hour.getText());

      // Convert AM/PM to military time
      if (ampm.getSelectedIndex()==1)
        result = result.concat(String.valueOf(hour_out + 12));
      else if (ampm.getSelectedIndex()==0 && hour_out==12)
        result = result.concat("0");
      else
        result = result.concat(String.valueOf(hour_out));
        
      result = result.concat("/");
      result = result.concat(min.getText());
      result = result.concat(",");
    }
        
    return result;
  }

}
