// Source Code for ChangePasswdDialog.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 change an     //
// existing user's password.					         //
// Inherits from the Dialog class (modal) and requires the parent frame, //
// a socket connected to the server, and an admin password as parameters //
public class ChangePasswdDialog extends Dialog implements ActionListener
{
   private GridBagLayout gbLayout;
   private GridBagConstraints gbConstraints; 
   private TextField uname, passwd;
   private Button cancel, ok, help;
   private Label uname_l, passwd_l;
   private Label dum1;
   private String password;
   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 and the admin password as a string as parameters.              //
   public ChangePasswdDialog(Frame parent, String p, ClientSocket cs)
   {
     // Call Dialog class constructor //
     super (parent, "  Please enter username and new Password", true);

     // Set window size and type and copy parameters to local variables //
     setSize(350,300);
     setResizable(false);  
     password = new String (p);
     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//
     uname = new TextField (20);
     uname.addActionListener (this);
     passwd = new TextField (20);
     passwd.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 ("New Login Password:");
    
     // 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 (uname_l,      0, 0, 1, 1);
     addObj (passwd_l,     1, 0, 1, 1);
     addObj (dum1,         2, 0, 2, 1);        
     gbConstraints.fill = GridBagConstraints.BOTH;
     addObj (help,         3, 0, 1, 2);
     addObj (ok,           3, 1, 2, 1);
     addObj (cancel,       4, 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("");
   } 

   // 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("changepassword," + uname.getText() + "," + password + "," + passwd.getText());
           String buf = new String (clientsocket.Read()); 
        
           // Parse numerical response from server //     
           if (buf.regionMatches(false, 0 , "120", 0,  3))
           {
             msgbox = new MsgBox (frame, "  SUCCESS", "Password Changed Successfully!", 250, 150);
             msgbox.setVisible(true);
             password = new String (passwd.getText());
             dispose();
           }     

           else if (buf.regionMatches(false, 0 , "712", 0,  3))
           {
             msgbox = new MsgBox (frame, "  USER ERROR", "Username does not Exist!", 250, 150);
             msgbox.setVisible(true);
           }     

           else 
           {
             msgbox = new MsgBox (frame, "  INPUT 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 returns the new password //
   public String newPasswd ()
   {
      return password; 
   }  

   // 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 = new StringBuffer(); 
      str.append("This screen is used to change a password for\n");
      str.append("an exisiting user.  This can be used if the\n");
      str.append("user forget his/her password.\n");
      str.append("\n");
      str.append("For the user id field, enter the username\n");
      str.append("whose password will be changed.\n");
      str.append("\n");
      str.append("For the password field, enter the new password\n");
      str.append("for the user.  The user can change this new.\n");
      str.append("password once he/she logs in.\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); 
   }
}