import java.io.*;
import java.awt.*;
import java.awt.event.*;
 
/**
 * CSC 442F:    Software Engineering
 * Software:    Calendar Client
 * Component:   DropReasonDialog class
 * Description: The DropReasonDialog class produces a dialog to enter 
 *              a reason for dropping from a meeting
 * @author David Tam
 * @version 1.0
 */
public class DropReasonDialog extends Dialog implements ActionListener
{ 
  protected ViewMeetingDialog parent;
  protected Frame frame;
  protected ClientSocket socket;

  protected DescriptionPanel descriptionPanel;
  protected Button ok;
  protected Button cancel;
  
  // GridBagLayout constraints
  protected GridBagConstraints constraints;

  // Constructor
  // ===========
  public DropReasonDialog(ViewMeetingDialog parent_in, Frame frame_in,
                          ClientSocket socket_in)
  {
    // Set Dialog attributes
    super(frame_in, "Dropping From Meeting", true);
    setSize(500,200);
    setResizable(true);  

    parent = parent_in;
    frame = frame_in;
    socket = socket_in;
    
    descriptionPanel = new DescriptionPanel();
    descriptionPanel.description.setText("Reason for dropping from meeting:");
    
    ok = new Button("Ok");
    cancel = new Button("Cancel");
    
    // Button listeners
    ok.addActionListener(this);
    cancel.addActionListener(this);
            
    setLayout(new GridBagLayout());

    // Constraints instantiation
    constraints = new GridBagConstraints();
    
    // Perform layout
    dialogLayout();
  }
  
  // Layout components
  // =================
  protected void dialogLayout()
  {
    constraints.anchor=constraints.CENTER;
    constraints.gridy=0;
    constraints.gridx=0;
    constraints.gridwidth=2;
    add(descriptionPanel, constraints); 
    constraints.gridwidth=1;
    constraints.gridy=1;
    constraints.gridx=0;
    constraints.insets = new Insets(5,50,5,25);
    add(ok, constraints);
    constraints.gridx=1;
    constraints.insets = new Insets(5,25,5,50);
    add(cancel, constraints);    
    constraints.insets = new Insets(0,0,0,0);
  }
  
  // Method to handle action events
  // ==============================
  public void actionPerformed(ActionEvent event)
  {
    // Determine who caused the event
    if (event.getSource() == cancel)
      // Return without making changes
      dispose();
    else if (event.getSource() == ok)
    {
      buildResponse();
      parent.dispose();
    }  
  }

  // Build response
  // ==============
  protected void buildResponse()
  {
    ResultBox resultBox;
    StringBuffer result = new StringBuffer("REMOVESELFFROMMEETING,");
    
    result.append(parent.meetingId);
    result.append(",");
    result.append(descriptionPanel.buildResponse());

    // Send to server
    try
    {
      socket.Write(result.toString());
    }
    // Catch errors
    catch (IOException io)
    {
      resultBox = new ResultBox(frame, socket);
      resultBox.getResponse();
    }

    resultBox = new ResultBox(frame, socket);
    if (resultBox.getResponse()==0)
      dispose();
    
  }
  
}
