// CSC 442F - Software Engineering
// Software:    Calendar Client
// Component:   ResultBox class
// Description: Returns server reponse to user

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

public class ResultBox
{
  Frame frame;
  ClientSocket clientsocket;
  MsgBox msgbox;
  HelpBox biggerbox;
  String title;
  String buf;
  String buf2;
  StringTokenizer token;

  // Constructor
  // ===========   
  public ResultBox(Frame frame_in, ClientSocket clientsocket_in)
  {
    frame = frame_in;
    clientsocket = clientsocket_in;
  }


  public int getResponse()
  {
    try 
    {
      buf = new String (clientsocket.Read()); 
      token = new StringTokenizer(buf, ",");
      buf2 = token.nextToken();
      if (Integer.parseInt(buf2) == 131)
      {
        title = "Success";
        biggerbox = new HelpBox(frame, title, parse(), true, 17, 25);
      }
      else if (Integer.parseInt(buf2) == 700 || Integer.parseInt(buf2) == 701 ||
               Integer.parseInt(buf2) == 702 || Integer.parseInt(buf2) == 703 ||
               Integer.parseInt(buf2) == 709)
      {
        title = "Error -- Client Problem";
        buf = "There is a problem with the client. Server did not like your ";
        buf = buf.concat("request.");
        msgbox = new MsgBox(frame, title, buf, 700, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 135)
      {
        title = "Success";
        buf = "The meeting has been cancelled because there is ";
        buf = buf.concat("only 1 user left to attend meeting.");
        msgbox = new MsgBox(frame, title, buf, 750, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 140)
      {
        title = "Success";
        buf = "You have been successfully removed from the meeting.";
        msgbox = new MsgBox(frame, title, buf, 750, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 714)
      {
        title = "Error -- Administrator at Work";
        buf = "The system is currently in use by the administrator.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 715)
      {
        title = "Error -- User Not Logged In";
        buf = "You are not logged in. Please log in first.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 720)
      {
        title = "Error -- Invalid Date";
        buf = "The date you entered does not exist. Please try again.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 720)
      {
        title = "Error -- Invalid Date";
        buf = "The date you entered does not exist. Please try again.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 721)
      {
        title = "Error -- Incorrect Date";
        buf = "The date you entered is not applicable. Please try again.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 722)
      {
        title = "Error -- Invalid Time";
        buf = "The time you entered does not exist. Please try again.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 723)
      {
        title = "Error -- Incorrect Time";
        buf = "The time you entered is not applicable. Please try again.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 730)
      {
        title = "Error -- Meeting Conflict";
        buf = "The meeting is not possible at the specified time. ";
        buf = buf.concat("Please try again.");
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 732)
      {
        title = "Error -- User Not Part of Meeting";
        buf = "You are not part of the meeting so you may not drop from it.";
        msgbox = new MsgBox(frame, title, buf, 650, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 737)
      {
        title = "Error -- Invalid Room";
        buf = "The room you chose is invalid. Please try again.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 738)
      {
        title = "Error -- Room not available";
        buf = "The room you chose is not available. Please try again.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 739)
      {
        title = "Error -- Insufficient Room Capacity";
        buf = "The room you chose does not have enough capacity. ";
        buf = buf.concat("Please try again.");
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 740)
      {
        title = "Error -- Appointment ID Does Not Exist";
        buf = "Sorry. The appointment ID that was referenced by your client";
        buf = buf.concat(" does not exist.");
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 741)
      {
        title = "Error -- User does not exist";
        buf = "The attendee(s) you have select does not exist. ";
        buf = buf.concat("Please try again.");
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else if (Integer.parseInt(buf2) == 911)
      {
        title = "Error -- Fatal Error";
        buf = "Warning! A fatal error occurred on the server.";
        msgbox = new MsgBox(frame, title, buf, 450, 150);
        msgbox.show();
      }
      else
      {
        title = "Error -- Unknown";
        buf = "The server returned an unknown error. Please try again.";
        msgbox = new MsgBox(frame, title, buf, 550, 150);
        msgbox.show();
      }

    }
      
    catch (IOException io)
    {
      title = "Error";
      msgbox = new MsgBox(frame, title, "There was a I/O error.", 450, 150);
      msgbox.show();
      return -1;
    }
    
    // Decide on return value
    if (title.equals("Success"))
      return 0;
    else
      return -1;
  }


  // Method to parse successful request
  // ==================================
  private String parse()
  {
    // Local variables
    StringTokenizer token2;
    StringBuffer result = new StringBuffer("");

    // Build response    
    result.append("Meeting scheduled\n");
    result.append("=================\n");
    result.append(token.nextToken());
    result.append(".\nMeeting ID is: ");
    result.append(token.nextToken());

    // Parse eventid
    result.append("\n\nStart Time\n");
    result.append("----------\n");
    token2 = new StringTokenizer(token.nextToken(), "/");
    result.append("Year: ");
    result.append(token2.nextToken());
    result.append("\nMonth: ");
    result.append(token2.nextToken());
    result.append("\nDay: ");
    result.append(token2.nextToken());
    result.append("\nHour: ");
    result.append(token2.nextToken());
    result.append("\nMinute: ");
    result.append(token2.nextToken());

    
    result.append("\nRoom: ");
    result.append(token.nextToken());
    
    return result.toString();
  }

} 
