import java.awt.*;
import java.awt.event.*;
 
/**
 * CSC 442F:    Software Engineering
 * Software:    Calendar Client
 * Component:   ManualPanel class
 * Description: The ManualPanel class produces a panel for manual scheduling of
 *              an appointment. This panel accepts date input from the user. It
 *              also enforces type restrictions.
 *
 * @author David Tam
 * @version 1.0
 */
public class ManualPanel extends Panel
{ 
  protected Frame frame;
  protected EventId eventId;
  protected MsgBox msgbox;
  protected YYmmddPanel yymmddPanel;
  protected TemporalPanel startPanel;
  public TemporalPanel endPanel;
  public DurationPanel durationPanel;
  protected TimeDuration timeDuration;

  protected Label startLabel;
  protected Label endLabel;
  protected Label durationLabel;
  
  // GridBagLayout constraints
  protected GridBagConstraints constraints;


 /** 
  * Constructor
  * ===========
  *
  * @param eventId_in   Used to set default date and time values
  */
  public ManualPanel(Frame frame_in, EventId eventId_in)
  {
    frame = frame_in;
    eventId = eventId_in;

    yymmddPanel = new YYmmddPanel(frame,eventId);
    timeDuration = new TimeDuration();
    startPanel = new TemporalPanel(frame, eventId, timeDuration);
    endPanel = new TemporalPanel(frame, timeDuration);
    durationPanel = new DurationPanel(frame, eventId);
    timeDuration.initialize(startPanel, endPanel, durationPanel);

    // Label initialization
    startLabel = new Label("Start Time");
    endLabel = new Label("End Time");
    durationLabel = new Label("Duration");

    // Constraints instantiation
    constraints = new GridBagConstraints();   

    // Layout
    setLayout(new GridBagLayout());
    constraints.anchor=constraints.WEST;
    constraints.gridx=0;
    constraints.gridy=0;
    constraints.gridwidth=5;
    add(yymmddPanel, constraints);
    constraints.gridwidth=1;
    constraints.gridy=1;
    add(startLabel, constraints);
    constraints.gridx=1;
    add(startPanel, constraints);
    constraints.gridy=2;
    constraints.gridx=0;
    add(endLabel, constraints);
    constraints.gridx=1;
    add(endPanel, constraints);
    constraints.gridy=3;
    constraints.gridx=0;
    add(durationLabel, constraints);
    constraints.gridx=1;
    add(durationPanel, constraints);

    // Turn off duration editting
    durationPanel.hour.setEditable(false);
    durationPanel.min.setEditable(false);
  }


 /**
  * Method to send build response
  * =============================
  * 
  * @return Returns a String in the form of the protocol
  * Returns null if error and brings up a message box.
  */
  public String buildResponse()
  {
    StringBuffer result = new StringBuffer();
    
    // Concatenate <date-time1>,<date-time2>,<duration>,
    // If there were no errors, return a filled response
    if (yymmddPanel.buildResponse()!="" && startPanel.buildResponse()!="" &&
          durationPanel.buildResponse()!="")
    {
       result = result.append(yymmddPanel.buildResponse());
       result = result.append(startPanel.buildResponse());
       result = result.append(yymmddPanel.buildResponse());
       result = result.append(startPanel.buildResponse());
       result = result.append(durationPanel.buildResponse());
    }
    
    return result.toString();
  }
  
 /**
  * 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)
  {
    yymmddPanel.setEditable(value);
    startPanel.setEditable(value);
    endPanel.setEditable(value);
  }

}
  
