// Source Code for AdminFrame.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 Frame that displays all the admin utilities     //
// that a system administrator has access to.  Choosing an appropriate    //
// choice bings up a window for that particular utility.                  //
// Inherits from the Frame class and requires the admin password (string) //
// and a socket connected to the server, as parameters                    //  
// Frame can only be closed by pressing the quit button                   //
public class AdminFrame extends Frame implements ActionListener
{
   private GridBagLayout gbLayout;
   private GridBagConstraints gbConstraints; 
   private Button newuser, deluser, addroom, savedb, restoredb;
   private Button shut, quit, changepasswd, ulist, rlist, help;
   private Label dum1, dum2, dum3, dum4;
   private String adminpasswd;
   private ClientSocket clientsocket;
   private MsgBox msgbox;
   private NewUserDialog newuserdialog;
   private DeleteUserDialog deleteuserdialog;
   private ChangePasswdDialog changepasswddialog;
   private AddRoomDialog addroomdialog;
   private RestoreDBDialog restoredbdialog;
   private KillServerDialog killserverdialog;
   private ViewUserDialog viewuserdialog;
   private ViewRoomDialog viewroomdialog;
   private AdminHelpBox helpbox;
   private StringBuffer str;

   // The constructor for the class.  Requires the admin password as a string and  //
   // a socket connected to the server as parameters.                              //
   public AdminFrame(String ap, ClientSocket cs)
   {
     // Call Dialog class constructor //
     super("  Admin Utilities Menu");

     // Set window size and type and copy parameters to local variables //     
     setSize(320,450);
     setResizable(false);  
     adminpasswd = new String (ap);
     clientsocket = cs;

     // Instantiate grid for layout purposes //
     gbLayout = new GridBagLayout();
     gbConstraints = new GridBagConstraints();
     setLayout (gbLayout); 
 
     // Allocate memory for all new items in Frame, and add action listeners //
     dum1 = new Label ("");  //used to provide spacing//
     dum2 = new Label ("");  //used to provide spacing//
     dum3 = new Label ("");  //used to provide spacing//
     dum4 = new Label ("");  //used to provide spacing//
     newuser = new Button ("Add new User");
     newuser.addActionListener (this);
     help = new Button ("Help");
     help.addActionListener (this);
     deluser = new Button ("Delete User");
     deluser.addActionListener (this);
     addroom = new Button ("Add Room");
     addroom.addActionListener (this);
     savedb = new Button ("Save Database");
     savedb.addActionListener (this);
     restoredb = new Button ("Restore Database");
     restoredb.addActionListener (this);
     shut = new Button ("Shutdown Server");
     shut.addActionListener (this);
     quit  = new Button ("Logout and Quit");
     quit.addActionListener (this);
     changepasswd = new Button ("Change User Password");
     changepasswd.addActionListener (this);
     ulist = new Button ("View User List");
     ulist.addActionListener (this);
     rlist = new Button ("View Room List");
     rlist.addActionListener (this);

     // Now place the components in the Frame, based on the grid bag layout //
     gbConstraints.weightx=0;
     gbConstraints.weighty=0;
     gbConstraints.fill = GridBagConstraints.HORIZONTAL;
     addObj (newuser,      0, 0, 2, 1);
     addObj (deluser,      1, 0, 2, 1);
     addObj (changepasswd, 2, 0, 2, 1);
     addObj (dum1,         3, 0, 2, 1);
     addObj (addroom,      4, 0, 2, 1);
     addObj (dum2,         5, 0, 2, 1);
     addObj (savedb,       6, 0, 2, 1);
     addObj (restoredb,    7, 0, 2, 1);
     addObj (dum3,         8, 0, 2, 1);
     addObj (ulist,        9, 0, 2, 1);
     addObj (rlist,       10, 0, 2, 1);
     addObj (dum4,        11, 0, 2, 1);
     gbConstraints.fill = GridBagConstraints.BOTH;
     addObj (help,        12, 0, 1, 2);
     addObj (shut,        12, 1, 1, 1);
     addObj (quit,        13, 1, 1, 1);        
   } 

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

      // If user pressed the add new user button //
      else if ((e.getSource())==newuser)
      {
         newuserdialog = new NewUserDialog (this, clientsocket);      
         newuserdialog.setVisible (true); 
      }

      // If user pressed the delete user button //
      else if ((e.getSource())==deluser)
      {
         deleteuserdialog = new DeleteUserDialog (this, clientsocket);      
         deleteuserdialog.setVisible (true); 
      }

      // If user pressed the change password button //
      else if ((e.getSource())==changepasswd)
      {
         changepasswddialog = new ChangePasswdDialog (this, adminpasswd, clientsocket);      
         changepasswddialog.setVisible (true); 
         adminpasswd = new String (changepasswddialog.newPasswd());
      }

      // If user pressed the add room button //
      else if ((e.getSource())==addroom)
      {
         addroomdialog = new AddRoomDialog (this, clientsocket);
         addroomdialog.setVisible (true);
      }

      // If user pressed the save database button //
      else if ((e.getSource())==savedb)
      {
         SaveDB();
      }

      // If user pressed the restore database button //
      else if ((e.getSource())==restoredb)
      {     
         restoredbdialog = new RestoreDBDialog (this, clientsocket);      
	 restoredbdialog.setVisible (true); 
      }

      // If user pressed the shutdown server button //
      else if ((e.getSource())==shut)
      {
         killserverdialog = new KillServerDialog (this, clientsocket);      
	 killserverdialog.setVisible (true); 
         if ((killserverdialog.isPressed())==false)
         {
            //connection has terminated so close down client
            dispose();
         }
      }

      // If user pressed the help button //
      else if ((e.getSource())==help)
      {
         helper();
      }

      // If user pressed the view user list button //     
      else if ((e.getSource())==ulist)
      {
         viewuserdialog = new ViewUserDialog (this, clientsocket);      
	 viewuserdialog.setVisible (true); 
      }

      // If user pressed the view room list button //
      else if ((e.getSource())==rlist)
      {
         viewroomdialog = new ViewRoomDialog (this, clientsocket);      
	 viewroomdialog.setVisible (true); 
      }
   }

   // This function handles logging out the admin and quiting the connection //
   // Closes the socket as well                                              //
   private void quitOut()
   {
      try 
      {
	 // Send request to server //
         clientsocket.Write("logoff,");
         String buf = new String (clientsocket.Read()); 

         // Parse numerical response from server //   
         if (buf.regionMatches(false, 0 , "100", 0,  3))
         {
	   clientsocket.Close();
           msgbox = new MsgBox (this, "  SUCCESS", "Log off Successful!", 250, 150);
           msgbox.setVisible(true);
           dispose();         
         }     
      }
         
      catch (IOException io) // Connection must have been lost //  
      {
         msgbox = new MsgBox (this, "  NETWORK ERROR", "Network Error Detected!", 250, 150);
         msgbox.setVisible(true);
         dispose();
      }  
   }

   // This function simply sends a command to the server to save a current copy of the //
   // server database in memory into a file on the server.                             //
   private void SaveDB()
   {
      try 
      {
	 // Send request to server //
         clientsocket.Write("savedb");
         String buf = new String (clientsocket.Read()); 
       
         // Parse numerical response from server //   
         if (buf.regionMatches(false, 0 , "225", 0,  3))
         {
           msgbox = new MsgBox (this, "  SUCCESS", "Database Saved Successfully!", 250, 150);
           msgbox.setVisible(true);
         }     
 
         else 
         {
           msgbox = new MsgBox (this, "  DATABASE ERROR", "Error Saving Database!", 250, 150);
           msgbox.setVisible(true);
         }     
      }
         
      catch (IOException io)  // Connection must have been lost //
      {
         msgbox = new MsgBox (this, "  NETWORK ERROR", "Network Error Detected!", 250, 150);
         msgbox.setVisible(true);
         dispose();
      }  
   }
 
   // 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 contains all system administrator\n");
      str.append("commands for the desktop server.\n");
      str.append("\n");
      str.append("ADD NEW USER - add new user to server.\n");
      str.append("DELETE USER  - delete a user from the server.\n");
      str.append("CHANGE PASSWORD  - change a user password.\n");
      str.append("ADD ROOM - add a new room in which meetings can be held\n");
      str.append("SAVE DB - used to save the Database in memory to file.\n");
      str.append("RESTORE DB - used to load on old Database from file to memory.\n");
      str.append("VIEW USERS - view a list of current users.\n");
      str.append("VIEW ROOMS - view a list of current rooms as well as their capacity.\n");
      str.append("SHUTDOWN SERVER - this kills the server. All users are kicked out.\n");
      str.append("LOGOUT - this closes the connection and logs you out\n");
      str.append("\n");
      str.append("To select an option, click on the proper button.\n");
      str.append("To quit, YOU MUST click on the Logout button.  Clicking on the X\n");
      str.append("will have no effect in closing the window\n");
 
      // Create and show modal help box //
      helpbox = new AdminHelpBox (this, "  Help Screen", str.toString(), true, 400, 300);
      helpbox.setVisible(true); 
   }
}

