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

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

public class ViewMeetingDialog extends MeetingDialog
{
  protected Label status;
  protected Label meetingIdLabel;
  protected String response;
  protected ModViewPanel modViewPanel;
  
  protected StringTokenizer token;

  protected String meetingStatus;
  public String meetingId;
  protected String dateTime;
  protected String duration;
  public String owner;
  protected String meetingPlace;
  protected String description;
  protected String attendees;

  // Constructor
  // ===========
  public ViewMeetingDialog(Frame parent, ClientSocket socket, 
                           EventId eventId_in, String response_in)
  {
    super(parent,socket,eventId_in);

    setTitle("Viewing an Existing Meeting");
    
    response = response_in;
    token = new StringTokenizer(response, ",");

    // Parse response
    parseResponse();

    modViewPanel = new ModViewPanel(this, parent, socket);

    // Panel modifications
    autoManPanel.remove(autoPanel);
    outMostPanel.remove(donePanel);

    // Replace button bar panel
    constraints.gridx=0;
    constraints.gridy=2;
    constraints.gridwidth=2;
    constraints.anchor=constraints.CENTER;
    outMostPanel.add(modViewPanel, constraints);
    constraints.gridwidth=1;       

    // Disable fields
    disableFields();
  }    
 

  // data1Panel layout
  // =================
  protected void layoutData1Panel()
  {
    status = new Label("Status: -- Unknown --");
    meetingIdLabel = new Label("MeetingId: ");
    data1Panel.setLayout(new GridBagLayout());
    constraints.gridx=0;
    constraints.gridy=0;
    constraints.anchor=constraints.WEST;
    data1Panel.add(status, constraints);
    constraints.gridy=1;
    data1Panel.add(meetingIdLabel, constraints);
    constraints.gridy=2;
    constraints.insets= new Insets(7,0,7,0);
    data1Panel.add(autoManPanel, constraints);
    constraints.insets= new Insets(0,0,0,0);
  }


  // Disable fields
  // ==============
  protected void disableFields()
  {
    attendeesPanel.setEditable(false);
    descriptionPanel.setEditable(false);
    manualPanel.setEditable(false);
    autoPanel.setEditable(false);
    autoschedule.setEnabled(false);
  }
  

  // Parse response
  // ==============
  protected void parseResponse()
  {
    meetingStatus = token.nextToken();
    meetingId = token.nextToken();
    dateTime = token.nextToken();
    duration = token.nextToken();
    owner = token.nextToken();
    meetingPlace = token.nextToken();
    description = token.nextToken();

    // Change Status field
    setStatusField();
    
    // Set duration
    setDuration();

    // Add attendees
    selectAttendees();

    // Select location
    selectLocation();

    // Add description
    addDescription();
  }

  // Change Status field
  // ===================
  protected void setStatusField()
  {
    String tmpStatus;
    String tmpMeetingId;
    
    tmpStatus = "Status: --";
    tmpStatus = tmpStatus.concat(meetingStatus);
    tmpStatus = tmpStatus.concat("--");
    status.setText(tmpStatus);
    
    // Set message id
    tmpMeetingId = "MeetingId: ";
    tmpMeetingId = tmpMeetingId.concat(meetingId);
    meetingIdLabel.setText( tmpMeetingId);
  }

  // Method to set duration
  // ======================
  protected void setDuration()
  {
    GregorianCalendar calendar;
    int durationHour;
    int durationMin;
    int startHour = Integer.parseInt(manualPanel.startPanel.hour.getText());
    int startMin = Integer.parseInt(manualPanel.startPanel.min.getText());
    String startAmpm = manualPanel.startPanel.ampm.getSelectedItem();
    int ampm;
    String hour_result;
    String min_result;

/*
    manualPanel.startPanel.ampm.removeListener(manualPanel.startPanel.ampm);
    manualPanel.startPanel.hour.removeListener(manualPanel.startPanel.hour);
    manualPanel.startPanel.min.removeListener(manualPanel.startPanel.min);
    manualPanel.endPanel.ampm.removeListener(manualPanel.endPanel.ampm);
    manualPanel.endPanel.hour.removeListener(manualPanel.endPanel.hour);
    manualPanel.endPanel.min.removeListener(manualPanel.endPanel.min);
    manualPanel.durationPanel.hour.removeListener(manualPanel.durationPanel.hour);
    manualPanel.durationPanel.min.removeListener(manualPanel.durationPanel.min);
*/

    // Determing AM/PM flag
    if (startAmpm.equals("AM"))
      ampm=0;
    else
      ampm=1;

    // Set duration
    durationMin = Integer.parseInt(duration);
    durationHour = (int) Math.floor(durationMin / 60.0);
    durationMin = durationMin - (60*durationHour);
        
    // Enter time into calendar object 
    calendar = new GregorianCalendar(1998, 11, 29, startHour+(12*ampm),
                                     startMin);
    // Calculate end time
    calendar.add(calendar.MINUTE, durationMin);
    calendar.add(calendar.HOUR_OF_DAY, durationHour);
    
//    manualPanel.durationPanel.hour.setText(String.valueOf(durationHour));
//    manualPanel.durationPanel.min.setText(String.valueOf(durationMin));

    System.out.println(durationHour);
    System.out.println(durationMin);
    // Format hour nicely
    if (calendar.get(calendar.HOUR_OF_DAY) < 10)
    {
      hour_result = "0";
      hour_result =
      hour_result.concat(String.valueOf(calendar.get(calendar.HOUR_OF_DAY)));
    }
    // Else if need to convert 24hr to ampm
    else if (calendar.get(calendar.HOUR_OF_DAY) > 12)
    {
      hour_result = String.valueOf(calendar.get(calendar.HOUR_OF_DAY) - 12);
      manualPanel.endPanel.ampm.select("PM");
    }
    else
    { 
      hour_result = String.valueOf(calendar.get(calendar.HOUR_OF_DAY));
      manualPanel.endPanel.ampm.select("AM");
    }
    
    // Format minutes nicely
    if (calendar.get(calendar.MINUTE) < 10)
    {
      min_result = "0";
      min_result =
      min_result.concat(String.valueOf(calendar.get(calendar.MINUTE)));
    }
    else
      min_result = String.valueOf(calendar.get(calendar.MINUTE));
 
    // Write results in end time TextField
    manualPanel.endPanel.hour.setText(String.valueOf(hour_result));
    manualPanel.endPanel.min.setText(String.valueOf(min_result));
    
  }  
    
  // Method to select attendees
  // ==========================
  protected void selectAttendees()
  {
    int count;
    String attendee;

    // Remove all items from list
    attendeesPanel.attendeesList.removeAll();

    count = token.countTokens();
    
    // Check if it is a personal meeting
    if (count <= 3)
      status.setText("Status: --Confirmed--");
    // Else there is more one personal attending
    else
    {
      // Add attendees to the attendees list
      for (int i = 0; i < count - 2 ; i++)
      {
        attendee = token.nextToken();

        // Don't include shadow users
        if (!attendee.endsWith("_personal"))
          attendeesPanel.attendeesList.addItem(attendee);
        // Mark owner of meeting
        else if (attendee.equals(owner))
          attendeesPanel.attendeesList.addItem(attendee.concat("<--owner"));
      }
    }
  }
  
  // Method to select location
  // =========================
  protected void selectLocation()
  {
    // Remove all items from list
    locationPanel.locationChoice.removeAll();
    
    // Add one item only
    locationPanel.locationChoice.addItem(meetingPlace);
  }
    
    
  // Method to set description
  // =========================
  protected void addDescription()
  {
    StringBuffer properDescription = new StringBuffer();
    StringTokenizer token = new StringTokenizer(description, "^^");
    int count = token.countTokens();

    // Append all but the last token    
    for (int i = 0 ; i < count - 1 ; i++)    
    {
      properDescription.append(token.nextToken());
      properDescription.append(",");
    }

    // Append last token
    properDescription.append(token.nextToken());
    
    // Add description to GUI
    descriptionPanel.descriptionTxt.setText(properDescription.toString());
  }
  

  // 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 view an existing meeting\n");
    helpbuf.append("or a personal event.\n\n");
    helpbuf.append("To modify the meeting, click on the 'modify'\n");
    helpbuf.append("button.\n\n");
    helpbuf.append("Note that you must be the owner of the meeting\n");
    helpbuf.append("in order to modify it.\n\n");
    helpbuf.append("To remove yourself from the meeting, click on the\n");
    helpbuf.append("'drop' button.");
    helpbuf.append("To cancel the meeting, click on the \n");
    helpbuf.append("'delete' button. Note that you must be the owner\n");
    helpbuf.append("of the meeting in order to cancel it.");

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

}
