// CSC 442F - Software Engineering
// Software:    Calendar Client
// Component:   MeetingDialog class
// Description: GUI to create, modify, delete meeting/event

import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class MeetingDialog extends Dialog implements ItemListener
{
  protected Frame frame;
  protected ClientSocket clientsocket;
  public EventId eventId;
  protected HelpBox helpDialog;

  // OutMost Panel
  protected Panel outMostPanel;
  
  // Outer Panels
  protected Panel data1Panel;
  protected Panel data2Panel;
  protected DescriptionPanel descriptionPanel;
  protected DonePanel donePanel;

  // Panels within data1Panel
  protected Panel autoManPanel;
  
  // Panels within autoManPanel
  protected ManualPanel manualPanel;
  protected AutoPanel autoPanel;

  // Panels within data2Panel
  protected AttendeesPanel attendeesPanel;
  protected LocationPanel locationPanel;
  
  // Check boxes
  protected Checkbox autoschedule;
  
  // CardLayout manager
  CardLayout cardlayout;
  
  // GridBagLayout constraints
  protected GridBagConstraints constraints;


  // Constructor
  // ===========
  public MeetingDialog(Frame parent, ClientSocket socket, EventId eventId_in)
  {
    // Set Dialog attributes
    super(parent, "Create a New Meeting", true);
    setResizable(true);  
    
    clientsocket = socket;
    frame = parent;
    eventId = eventId_in;
  
    // OutMost panel instantiation
    outMostPanel = new Panel();
    
    // Outer panel instantiation
    descriptionPanel = new DescriptionPanel();
    donePanel = new DonePanel(this);

    // DataPanel instantiation
    data1Panel = new Panel();
    data2Panel = new Panel();

    // Data1Panel instantiation
    autoManPanel = new Panel();
    
    // autoManPanel instantiation
    manualPanel   = new ManualPanel(frame, eventId);
    autoPanel     = new AutoPanel(frame, eventId);

    // Data2Panel instantiation
    attendeesPanel = new AttendeesPanel(socket,eventId.username);
    attendeesPanel.fetchData();
    locationPanel  = new LocationPanel(socket);
    locationPanel.fetchData();
    
    // CardLayout manager instantiation
    cardlayout = new CardLayout();
    
    // Constraints instantiation
    constraints = new GridBagConstraints();

    // Checkbox instantiation
    autoschedule = new Checkbox ("Auto-schedule", false);

    // Item listeners
    autoschedule.addItemListener(this);
    
    // data1Panel layout
    layoutData1Panel();

    // data2Panel layout
    layoutData2Panel();

    // autoManPanel layout
    autoManPanel.setLayout(cardlayout);
    autoManPanel.add(manualPanel, "manualPanel");
    autoManPanel.add(autoPanel, "autoPanel");

    // Outer panel layout
    outerPanelLayout();

    // OutMost panel layout
    setLayout (new GridBagLayout());
    constraints.anchor=constraints.CENTER;
    constraints.insets = new Insets(10,10,10,10);
    add(outMostPanel, constraints);
    constraints.insets = new Insets(0,0,0,0);
    constraints.anchor=constraints.WEST;
  }

  // data1Panel layout
  // =================
  protected void layoutData1Panel()
  {
    data1Panel.setLayout(new GridBagLayout());
    constraints.gridx=0;
    constraints.gridy=0;
    constraints.anchor=constraints.WEST;
    constraints.insets= new Insets(0,0,5,0);
    data1Panel.add(autoschedule, constraints);
    constraints.gridy=1;
    data1Panel.add(autoManPanel, constraints);
    constraints.insets= new Insets(0,0,0,0);
  }
  

  // data2Panel layout
  // =================   
  protected void layoutData2Panel()
  {
      data2Panel.setLayout(new GridBagLayout());
      constraints.gridx=0;
      constraints.gridy=0;
      data2Panel.add(attendeesPanel, constraints);
      constraints.gridy=1;
      constraints.insets=new Insets(10,0,0,0);
      data2Panel.add(locationPanel, constraints);
      constraints.insets=new Insets(0,0,0,0);
  }


  // Outer panel layout
  // ==================  
  protected void outerPanelLayout()
  {
    outMostPanel.setLayout(new GridBagLayout());
    constraints.gridx=0;
    constraints.gridy=0;
    constraints.insets = new Insets(0, 0, 10, 10);
    outMostPanel.add(data1Panel, constraints);
    constraints.gridx=1;
    constraints.insets = new Insets(0, 10, 0, 0);
    outMostPanel.add(data2Panel, constraints);
    constraints.insets = new Insets(0, 0, 0, 0);
    constraints.gridy=1;
    constraints.gridx=0;
    constraints.gridwidth=2;
    constraints.anchor=constraints.WEST;
    outMostPanel.add(descriptionPanel, constraints);
    constraints.gridy=2;
    outMostPanel.add(donePanel, constraints);
    constraints.gridwidth=1;
  }
  

  // Method to handle item select events
  // ===================================
  public void itemStateChanged(ItemEvent item)
  {
    // Flip between auto and manual schedule panels
    if (autoschedule.getState()==true)
      cardlayout.show(autoManPanel, "autoPanel");
    else if (autoschedule.getState()==false)
      cardlayout.show(autoManPanel, "manualPanel");
  }    

  // Grabs values from all components -- server dependent function
  // =============================================================
  protected int grabValues()
  {
    // Local variables
    StringBuffer output = new StringBuffer("newmeeting,");
    ResultBox resultbox;
    MsgBox msgbox;
    String testResponse;
      
    // Concatenate <date-time1>,<date-time2>,<duration>,
    // Determine whether should get autoschedule
    if (autoschedule.getState()==true)
      testResponse=autoPanel.buildResponse();
    // Else, should get manualschedule
    else
      testResponse=manualPanel.buildResponse();
      
    // Error check response first  
    if (testResponse.equals(""))
      return -1;      
    else 
      output.append(testResponse);
     
    // Concatenate <meetingPlace>,
    testResponse = locationPanel.buildResponse();

    // Error-check response first  
    if (testResponse.equals(""))
      return -1;      
    else 
      output.append(testResponse);

    // Concatenate <meetingReason>,
    output.append(descriptionPanel.buildResponse());
    output.append(",");
    
    // Concantenate <numberOfUsers>, <userName1>, .. , <userNameN>
    output.append(attendeesPanel.buildResponse());
       
    // Attempt newmeeting request
    try
    {
      clientsocket.Write(output.toString());
    }
    // Catch errors
    catch (IOException io)
    {
      resultbox = new ResultBox(frame, clientsocket);
      return -1;
    }

    // Determine server response and send a user-friendly message
    resultbox = new ResultBox(frame, clientsocket);
    if (resultbox.getResponse()==0)
      dispose();

    return 0;
  }


  // Method to display help information
  // ==================================
  protected void help()
  {
    StringBuffer helpbuf = new StringBuffer("");
    
    // Build help content    
    helpbuf.append("Help:\n");
    helpbuf.append("====\n");
    helpbuf.append("This window is used to create a new meeting\n");
    helpbuf.append("or a personal event.\n\n");
    helpbuf.append("Simply enter the year,month, day, start time,\n");
    helpbuf.append("and end time in the appropriate fields.\n\n");
    helpbuf.append("Attendees and meeting location may be optionally\n");
    helpbuf.append("chosen. Multi-select users by clicking once on\n");
    helpbuf.append("their usernames. The description field\n");
    helpbuf.append("is also optional.\n\n");
    helpbuf.append("To select auto-scheduling mode, click on\n");
    helpbuf.append("the Auto-schedule button located near the\n");
    helpbuf.append("top-left corner of the window. In this mode\n");
    helpbuf.append("you may enter your earliest avaiable time,\n");
    helpbuf.append("latest available time, and meeting duration.\n");
    helpbuf.append("The server will then figure out an available\n");
    helpbuf.append("time slot and schedule your meeting for you.\n");
    
    // Add to content to help window
    helpDialog = new HelpBox(frame, "Help", helpbuf.toString(), false, 30, 50);
  }

}
