// CSC 442F - Software Engineering
// Software:    Calendar Client
// Component:   MeetingIntro class
// Description: Decides whether to create, or view entry

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

public class MeetingIntro
{
  protected Frame frame;
  protected ClientSocket clientsocket;
  protected EventId eventId;
  
  protected MeetingDialog createmeeting;
  protected ViewMeetingDialog viewmeeting;
  protected MsgBox msgbox;
  
  // Constructor
  // ===========
  public MeetingIntro(Frame frame_in, ClientSocket socket, EventId eventId_in)
  {
    clientsocket = socket;
    frame = frame_in;
    eventId = eventId_in;
    
    String request;
    String response;
    StringTokenizer token;
    String errorcode;
    String status;
       
    request = "GETCALENDAR,D,";
    request = request.concat(eventId.concat());

    // Attempt IO operation
    // --------------------
    try
    {
      clientsocket.Write(request);
      response = clientsocket.Read();
      token = new StringTokenizer(response, ",");

      // Parse response
      errorcode = token.nextToken();

      // If there is an error
      if (errorcode.equals("720"))
      {
        msgbox = new MsgBox(frame, "Error -- Server Trouble",
                                    "Sorry. Can't get status for event slot.",350,150);
        msgbox.setVisible(true);
        
        // Default to an editable dialog box
        createmeeting = new MeetingDialog(frame, clientsocket, eventId);
        createmeeting.pack();
        createmeeting.setVisible(true);

      }
      // Else there is no error
      else
      {
        status = token.nextToken();

        // If nothing scheduled, open create new meeting dialog
        if (status.equals("END"))
        {
          createmeeting = new MeetingDialog(frame, clientsocket, eventId);
          createmeeting.pack();
          createmeeting.setVisible(true);
        }
        // Else if something scheduled, open view meeting dialog instead
        else
        {
          // Input values into dialog box
          viewmeeting = new ViewMeetingDialog(frame, clientsocket, eventId,
                              response.substring(4,response.length()));
          viewmeeting.pack();
          viewmeeting.setVisible(true);
        }
      }
    }

    // Catch IO problems  
    // -----------------
    catch (IOException io)
    { 
        msgbox = new MsgBox(frame, "Error -- I/O trouble",
       "Sorry. There was an I/O problem.",350,150);
        msgbox.setVisible(true);
    }
  }
}
    
