import java.awt.*;
import java.awt.event.*;
 
/**
 * CSC 442F:    Software Engineering
 * Software:    Calendar Client
 * Component:   AutoPanel class
 * Description: The AutoPanel class produces a panel for auto 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 AutoPanel extends Panel
{ 
  protected Frame frame;
  protected EventId eventId;
  protected MsgBox msgbox;
  
  // Panels
  protected YYmmddPanel yymmdd1Panel;
  protected YYmmddPanel yymmdd2Panel;
  protected TemporalPanel start1Panel;
  protected TemporalPanel start2Panel;
  protected DurationPanel durationPanel;

  // Labels
  protected Label earliestLabel;
  protected Label latestLabel;
  protected Label start1Label;
  protected Label start2Label;
  protected Label durationLabel;

  // GridBagLayout constraints
  protected GridBagConstraints constraints;


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

    yymmdd1Panel  = new YYmmddPanel(frame,eventId);
    yymmdd2Panel  = new YYmmddPanel(frame);
    durationPanel = new DurationPanel(frame);
    start1Panel   = new TemporalPanel(frame, eventId, null);
    start2Panel   = new TemporalPanel(frame, null);

    // Label initialization
    earliestLabel = new Label("---EARLIEST AVAILABLE DATE---");
    latestLabel = new Label("---LATEST AVAILABLE DATE---");
    start1Label = new Label("Start Time");
    start2Label = new Label("Start Time");
    durationLabel = new Label("Duration");

    // Constraints instantiation
    constraints = new GridBagConstraints();   

    // Panel layout
    setLayout(new GridBagLayout());
    constraints.anchor=constraints.WEST;
    constraints.gridy=0;
    constraints.gridx=0;
    add(durationLabel, constraints);
    constraints.gridx=1;
    add(durationPanel, constraints);
    constraints.gridy=1;
    constraints.gridx=0;
    constraints.insets= new Insets(10,0,0,0);
    constraints.gridwidth=5;
    add(earliestLabel, constraints);
    constraints.insets= new Insets(0,0,0,0);
    constraints.gridy=2;
    add(yymmdd1Panel, constraints);
    constraints.gridwidth=1;
    constraints.gridy=3;
    add(start1Label, constraints);
    constraints.gridx=1;    
    add(start1Panel, constraints);
    constraints.gridy=4;
    constraints.gridx=0;
    constraints.insets= new Insets(10,0,0,0);
    constraints.gridwidth=5;
    add(latestLabel, constraints);
    constraints.gridy=6;
    constraints.insets= new Insets(0,0,0,0);
    add(yymmdd2Panel, constraints);
    constraints.gridwidth=1;
    constraints.gridy=7;
    add(start2Label, constraints);
    constraints.gridx=1;
    add(start2Panel, constraints);
  }
  
  
 /**
  * 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 (yymmdd1Panel.buildResponse()!="" && start1Panel.buildResponse()!="" &&
        yymmdd2Panel.buildResponse()!="" && start2Panel.buildResponse()!="" &&
        durationPanel.buildResponse()!="")
    {
       result = result.append(yymmdd1Panel.buildResponse());
       result = result.append(start1Panel.buildResponse());
       result = result.append(yymmdd2Panel.buildResponse());
       result = result.append(start2Panel.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)
  {
    yymmdd1Panel.setEditable(value);
    start1Panel.setEditable(value);
    yymmdd2Panel.setEditable(value);
    start2Panel.setEditable(value);
    durationPanel.setEditable(value);
  }
  
}
  
