/*
 * PasswordConfigurationForm.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class implements the form used to set the password on the initial program run
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.security.*;
import java.util.*;
public class PasswordConfigurationForm extends AddressBookConfigurationForm
{
  
  private JPasswordField password;
  private JPasswordField passwordRepeat;
  private AddressBookConfiguration configuration;
  private Boolean passwordRequired;
  
  public PasswordConfigurationForm (AddressBookConfiguration configuration, JFrame mainWindow) 
  {
    super ("Set Password", null, configuration, mainWindow, 16);  
    this.configuration = configuration;
    this.passwordRequired = configuration.getElementValue ("usepassword").equals ("required");
  }
  
  protected void defineFields ()
  {    
 
    // Add fields for entering password and confirmation repeat   
    password = addPasswordField ("Password:", "");
    passwordRepeat = addPasswordField ("Repeat Password:", "");
       
  }
  
 
  protected void otherAction (AbstractButton source)
  {
  }
  
  protected boolean validateAndSave ()   
  {
    // Check if user has entered a password
    if (password.getPassword().length == 0)
    {
      // If not check if password is required to be set
      if (passwordRequired)
      {
        JOptionPane.showMessageDialog (this, "You must set a password");
        password.requestFocus ();
        return false;
      }
      else
      {
      
        // Otherwise if optional use default password
        configuration.passwordSet ("");
        return true;
      }
    }
    
    // Check that repeat matches original
    if (!Arrays.equals (password.getPassword(), passwordRepeat.getPassword()))
    {
      JOptionPane.showMessageDialog (this, "The passwords you have entered do not match");
      password.setText ("");
      passwordRepeat.setText ("");
      password.requestFocus ();
      return false;
    }
    
    // Store MD5 hash of password in configuration
    try
    {
      MessageDigest algorithm = MessageDigest.getInstance("MD5");
      algorithm.reset ();
      algorithm.update ((new String (password.getPassword ())).getBytes());
      configuration.addOrReplaceElement ("addressbookpassword", 
                                         (new BytesToHexBuffer (algorithm.digest())).toString());
    }
    catch (NoSuchAlgorithmException e)
    {}
                                       
    // Set up encryption key in configuration
    configuration.passwordSet (new String (password.getPassword ()));
    
    // Prevent program exiting when window is closed
    passwordRequired = false;

    return true;        
  }
  
  protected Boolean cancel ()
  {
    // If not check if password is required to be set
    if (passwordRequired)
    {
      JOptionPane.showMessageDialog (this, "You must set a password");
      password.requestFocus ();
      return false;
    }
    else
    {
    
      // Otherwise if optional use default password
      configuration.passwordSet ("");
      return true;
    }  
  }
  
  protected Boolean closeAction ()
  {
    return cancel ();
  }
           
}    