// Source Code for NewUserDialog.class //
// Client Code for Desktop Calendar    //

// Code based on using Server #18     //
// Written by Khalid Mufti - Group 31 //

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

// This class handles the dialog which enables an admin to add a new    //
// user for the server.                                                 //
// Inherits from the Dialog class (modal) and requires the parent frame //
// and a socket connected to the server, as parameters                  //  
public class NewUserDialog extends Dialog implements ActionListener
{
   private GridBagLayout gbLayout;
   private GridBagConstraints gbConstraints; 
   private TextField uname, passwd, fname, lname, office, email, pnum;
   private TextField department, extra;
   private Button cancel, ok, help;
   private Label uname_l, passwd_l, fname_l, lname_l, office_l;
   private Label email_l, pnum_l, department_l, extra_l;
   private Label dum1, dum2;
   private ClientSocket clientsocket;
   private Frame frame;
   private MsgBox msgbox;
   private AdminHelpBox helpbox;
   private StringBuffer str;

   // The constructor for the class.  Requires a parent frame and socket connected //
   // to the server as parameters.                                                 //
   public NewUserDialog(Frame parent, ClientSocket cs)
   {
     // Call Dialog class constructor //
     super (parent, "  Please enter new user Information", true);
 
     // Set window size and type and copy parameters to local variables //
     setSize(400,500);
     setResizable(false);  
     clientsocket=cs;
     frame=parent;

     // Instantiate grid for layout purposes //
     gbLayout = new GridBagLayout();
     gbConstraints = new GridBagConstraints();
     setLayout (gbLayout); 
     
     // Allocate memory for all new items in Dialog, and add action listeners //
     dum1 = new Label (""); //used to provide spacing//
     dum2 = new Label (""); //used to provide spacing//
     uname = new TextField (20);
     uname.addActionListener (this);
     passwd = new TextField (20);
     passwd.addActionListener (this);
     fname = new TextField (20);
     fname.addActionListener (this);
     lname = new TextField (20);
     lname.addActionListener (this);
     office = new TextField (20);
     office.addActionListener (this);
     email = new TextField (20);
     email.addActionListener (this);
     pnum = new TextField (20);
     pnum.addActionListener (this);
     department = new TextField (20);
     department.addActionListener (this);
     extra = new TextField (20);
     extra.addActionListener (this);
     cancel = new Button ("Cancel");
     cancel.addActionListener (this);
     ok = new Button ("OK");
     ok.addActionListener (this);
     help = new Button ("Help");
     help.addActionListener (this);
     uname_l = new Label("Login Name:");
     passwd_l = new Label ("Login Password:");
     fname_l = new Label ("First Name:");
     lname_l = new Label ("Last Name:");
     office_l = new Label ("Office:");
     email_l = new Label ("Email:");
     pnum_l = new Label ("Phone #:");
     department_l = new Label ("Department:");
     extra_l = new Label ("Extra:");
    
     // Now place the components in the Dialog, based on the grid bag layout //
     gbConstraints.fill = GridBagConstraints.HORIZONTAL;
     addObj (uname,        0, 1, 2, 1);
     addObj (passwd,       1, 1, 2, 1);
     addObj (dum1,         2, 1, 2, 1);        
     addObj (fname,        3, 1, 2, 1);        
     addObj (lname,        4, 1, 2, 1);
     addObj (office,       5, 1, 2, 1);
     addObj (email,        6, 1, 2, 1);        
     addObj (pnum,         7, 1, 2, 1);
     addObj (department,   8, 1, 2, 1);
     addObj (extra,        9, 1, 2, 1);        
     addObj (uname_l,      0, 0, 1, 1);
     addObj (passwd_l,     1, 0, 1, 1);
     addObj (fname_l,      3, 0, 1, 1);        
     addObj (lname_l,      4, 0, 1, 1);
     addObj (office_l,     5, 0, 1, 1);
     addObj (email_l,      6, 0, 1, 1);        
     addObj (pnum_l,       7, 0, 1, 1);
     addObj (department_l, 8, 0, 1, 1);
     addObj (extra_l,      9, 0, 1, 1);        
     addObj (dum2,        10, 0, 2, 1);        

     gbConstraints.fill = GridBagConstraints.BOTH;
     addObj (help,        11, 0, 1, 2);
     addObj (ok,          11, 1, 2, 1);
     addObj (cancel,      12, 1, 2, 1);        

     // Add a listener for closing the window prematurely and set the textfields //
     // to an empty value                                   		         //
     addWindowListener (new CloseWindow());
     uname.setText("");
     passwd.setText("password");
     fname.setText("-----");
     lname.setText("-----");
     office.setText("-----");
     email.setText("-----");
     pnum.setText("-----");
     department.setText("-----");
     extra.setText("-----");    
   } 

   // This function handles any action events from the user //
   public void actionPerformed (ActionEvent e)
   {   
      // If user pressed the cancel button //
      if ((e.getSource())==cancel)
         dispose();

      // If user pressed the ok button //
      else if ((e.getSource())==ok)
      {
         try 
         { 
           // Send request to server //
           clientsocket.Write("adduser," + uname.getText() + "," + passwd.getText() + "," + fname.getText() + ","                                 + lname.getText() + "," + office.getText() + "," + email.getText() + "," +                                          pnum.getText() + "," + department.getText() + "," + extra.getText());
           String buf = new String (clientsocket.Read()); 

           // Parse numerical response from server // 
           if (buf.regionMatches(false, 0 , "200", 0,  3))
           {
             //create backup user to handle personal events (since server not handle directly)//
             clientsocket.Write("adduser," + uname.getText() + "_personal" + "," + "-" + "," + "-" + "," 
                                + "-" + "," + "-" + "," + "-" + "," + "-" + "," + "-" + "," + "-");
             String garbage = new String (clientsocket.Read()); 
             msgbox = new MsgBox (frame, "  SUCCESS", "User Added Successfully!", 250, 150);
             msgbox.setVisible(true);
             dispose();
           }     

           else if (buf.regionMatches(false, 0 , "800", 0,  3))
           {
             msgbox = new MsgBox (frame, "  ADD ERROR", "Username already exists!", 250, 150);
             msgbox.setVisible(true);
           }     

           else 
           {
             msgbox = new MsgBox (frame, "  ADD ERROR", "Invalid Entry Detected!", 250, 150);
             msgbox.setVisible(true);
           }     
         }
         
         catch (IOException io)  // Connection must have been lost //
         {
           msgbox = new MsgBox (frame, "  NETWORK ERROR", "Network Error Detected!", 250, 150);
           msgbox.setVisible(true);
           dispose();
         }  
      }   
 
      // If user pressed the help button //
      else if ((e.getSource())==help)
      {
         helper();
      }    
   }

   // This function simply adds an object (registers it) to the Dialog //
   // Further, it sets various parts of the grid bag layout            // 
   private void addObj (Component c, int row, int column, int width, int height)
   {
      gbConstraints.gridx=column;
      gbConstraints.gridy=row;    
      gbConstraints.gridwidth=width;
      gbConstraints.gridheight=height;
      gbLayout.setConstraints (c, gbConstraints);
      add (c);
   } 

   // This function simply pops up a help dialog box that displays help on using //
   // this dialog screen.                                                        //
   private void helper()
   {
      // Set what the help box should say //
      str = new StringBuffer(); 
      str.append("This screen is used to add a new user for the\n");
      str.append("desktop server.\n");
      str.append("\n");
      str.append("For the login id field, enter a unique username\n");
      str.append("for the new user.  (the server will check if it\n");
      str.append("is unique).\n");
      str.append("\n");
      str.append("For the password, enter a default password.  The\n");
      str.append("user should change this once s/he logins in.\n"); 
      str.append("\n");
      str.append("The other fields are info about the user.  They\n");
      str.append("can be filled in by the user later, also.\n");
      str.append("\n");
      str.append("When done, cick on the OK button; otherwise,\n");
      str.append("click Cancel to return to the Admin menu.\n");
 
      // Create and show modal help box //
      helpbox = new AdminHelpBox (frame, "  Help Screen", str.toString(), true, 300, 300);
      helpbox.setVisible(true); 
   }
}