// CSC 442F - Software Engineering
// Software:    Calendar Client
// Component:   ModMeetingDialog class
// Description: GUI to modify meeting/event

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

public class ModMeetingDialog extends ViewMeetingDialog
{
  int length;
  Choice choice;
  String buf;
  
  // Constructor
  // ===========
  public ModMeetingDialog  (Frame parent, ClientSocket socket, 
                           EventId eventId_in, String response_in)
  {
    super(parent, socket, eventId_in, response_in);
    setTitle("Modifying an Existing Meeting");

    choice = locationPanel.locationChoice;
    length = response.length();

  
    // Replace button bar
    outMostPanel.remove(modViewPanel);
    constraints.gridwidth=2;
    outMostPanel.add(donePanel,constraints);
    constraints.gridwidth=1;
    
    // Refresh location list
    choice.removeAll();
    choice.addItem("none");
    choice.addItem("any");
    locationPanel.fetchData();
    
    StringTokenizer token = new StringTokenizer(meetingPlace, ",");
    
    System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
    System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
    System.out.println(choice.getItemCount());

    // Determine which one was chosen before
    for (int i=0 ; i < token.countTokens() ; i++)
    {
        String rbuf = new String (token.nextToken()); 
    
        if (rbuf.regionMatches(false, 0 , choice.getItem(i), 0, 
        choice.getItem(i).length()))
        {
          choice.select(i);
          break;
        }
    }
 
    pack();
    setVisible(true);
  }
  
  
  // 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);
    constraints.gridy=1;
    data1Panel.add(autoManPanel, constraints);
    constraints.insets= new Insets(0,0,0,0);
  }
  
    // Disable fields
  // ==============
  protected void disableFields()
  {
    attendeesPanel.setEditable(false);
  }
  
    // Change Status field
  // ===================
  protected void setStatusField()
  {
  }
  
  // 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 modify an existing meeting\n");
    helpbuf.append("or a personal event.\n\n");
    helpbuf.append("Simply modify the year,month, day, start time,\n");
    helpbuf.append("and end time in the appropriate fields.\n\n");
    helpbuf.append("Meeting location and description fields\n");
    helpbuf.append("may also be modified.\n\n");
    helpbuf.append("However, attendees can not be modified\n");
    helpbuf.append("due to inherent server restrictions.\n\n");

    // Add to content to help window
    helpDialog = new HelpBox(frame, "Help", helpbuf.toString(), false, 30, 50);
  }

  // Grabs values from all components -- server dependent function
  // =============================================================
  protected int grabValues()
  { 
    // Local variables
    StringBuffer header = new StringBuffer("CHANGEMEETING,");
    StringBuffer garbage = new StringBuffer();
    StringTokenizer token;
    String testResponse;
    String newValue;
    String oldValue;
 
    // Build header
    header.append(meetingId);
    header.append(",");
    
    // Build string <date-time1>,<date-time2>,<duration>,
    testResponse=manualPanel.buildResponse();
     
    token =  new StringTokenizer(testResponse,",");

    // Grab <date-time1>
    newValue = token.nextToken();

    // Error check response first  
    if (newValue.equals(""))
      return -1;
    // Else if values old & new values differ, send update to server
    else if (!newValue.equals(dateTime))
    {
      garbage.append(header.toString());
      garbage.append("STARTDATE=");
      garbage.append(newValue);

      try
      {
        clientsocket.Write(garbage.toString());
      }
      catch (IOException io)
      {
      }
        checkResponse();
    }
    
    // Grab <duration>
    token.nextToken();
    newValue = token.nextToken();
    
    // Error check response first  
    if (newValue.equals(""))
      return -1;
    // Else if values old & new values differ, send update to server
    else if (!newValue.equals(duration))
    {
      garbage.append(header.toString());
      garbage.append("DURATION=");
      garbage.append(newValue);
      
      try
      {
        clientsocket.Write(garbage.toString());
      }
      catch (IOException io)
      {
      }
      checkResponse();
    }

    // Grab <ROOM>
    newValue = locationPanel.buildResponse();
    
    // Error check response first  
    if (newValue.equals(""))
      return -1;
    // Else if values old & new values differ, send update to server
    else if (!newValue.equals(meetingPlace))
    {
      garbage.append(header.toString());
      garbage.append("ROOM=");
      garbage.append(newValue);
      
      try
      {
        clientsocket.Write(garbage.toString());
      }
      catch (IOException io)
      {
      }
      
      checkResponse();
    }
    
    // Grab <DESCRIPTION>
    newValue = descriptionPanel.buildResponse();
    
    // Error check response first  
    if (newValue.equals(""))
      return -1;
    // Else if values old & new values differ, send update to server
    else if (!newValue.equals(description))
    {
      garbage.append(header.toString());
      garbage.append("REASON=");
      garbage.append(newValue);
      
      try
      {
        clientsocket.Write(garbage.toString());
      }
      catch (IOException io)
      {
      }

      checkResponse();
    }
    
    return 0;
  }
  
  // Determines server response and sends a user-friendly message
  // ============================================================
  protected void checkResponse()
  {
    ResultBox resultbox = new ResultBox(frame, clientsocket);
    if (resultbox.getResponse()==0);
      dispose();
  }

}
