/*
 * ExtensionConfigurationForm.java
 *
 * Copyright 2013 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 enter configuration data for an extension
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class ExtensionConfigurationForm extends AddressBookConfigurationForm
{
  
  private JTextField extensionName;
  private JTextField extensionNumber;
  private JComboBox extensionProvider;
  private JTextField extensionUsername;
  private JPasswordField extensionPassword;
  private ExtensionListForm extensionList;
  private ConfigurationListEntry extensionEntry;
  private ExtensionSelector extensionSelector;
     
  public ExtensionConfigurationForm (ConfigurationListEntry extensionEntry, AddressBookConfiguration configuration, 
                                     ExtensionListForm extensionList, ExtensionSelector extensionSelector) 
  {
    super ("Configure Extension", extensionEntry != null ? extensionEntry.getRecord() : null, 
           configuration, extensionList, 24, 12);
    this.extensionEntry = extensionEntry;
    this.extensionList = extensionList;
    this.extensionSelector = extensionSelector;
  }
  
  protected void defineFields ()
  {    
    
    // Add fields for extension name and number
    extensionName = addNameField ("Extension Name:");
    extensionNumber = addTextField ("Extension Number:", "extensionnumber");
    
    // Add selector for provider and select parent provider for extension
    java.util.List<ConfigurationRecord> providers = configuration.getElementRecords ("provider");
    extensionProvider = new JComboBox (new Vector<ConfigurationRecord> (providers));
    if (configurationRecord != null)
    {
      for (ConfigurationRecord provider : providers)
      {
        if (provider.isParentRecord (configurationRecord))
        {
          extensionProvider.setSelectedItem (provider);
        }
      }
    }
    addField ("Provider:", extensionProvider);
    
    // Add fields for the username & password
    extensionUsername = addTextField ("Username:", "username");
    extensionPassword = addPasswordField ("Password:", "password");
    
  }
   
  protected void otherAction (AbstractButton source)
  {
  }
  
  protected boolean validateAndSave ()   
  {
    // Check that all required fields are filled in
    ConfigurationRecord provider = (ConfigurationRecord) extensionProvider.getSelectedItem ();
    if (!checkFieldEntry (extensionName, "Please enter the extension name"))
    {
      return false;
    }
    else if (!checkFieldEntry (extensionNumber, "Please enter extension number/id"))
    {
      return false;
    }
    else if (provider.getElementValue ("postusernameparameter") != null && 
                 !checkFieldEntry (extensionUsername, 
                                   "Please enter the username for the extension"))
    {
      return false;
    }
    else if (provider.getElementValue ("postpasswordparameter") != null && 
                 !checkFieldEntry (extensionPassword, 
                 "Please enter the password for the extension"))
    {
      return false;
    }
    
    // Check for duplicate name
    ConfigurationRecord duplicate = configuration.getElementRecord ("extension", extensionName.getText());
    if (duplicate != null && !duplicate.equals (configurationRecord))
    {
      JOptionPane.showMessageDialog (this, "There is already an entry for the extension " + extensionName.getText(), 
                                     "Duplicate extension name", JOptionPane.ERROR_MESSAGE);
      return false;
    }
    
    // Create a new extension record (under selected provider) if not updating an existing extension
    ConfigurationRecord extension;
    if (configurationRecord == null)
    {
      extension = provider.addElementRecord ("extension", extensionName.getText());
      
      // Add extension to selector
      extensionSelector.add (extension);      
    }
    else
    {
      // Otherwise update existing record
      extension = configurationRecord;
      
      // Check if name has changed
      if (!extension.toString().equals (extensionName.getText()))
      {
        // Update any default extension configuration elements
        configuration.updateElements ("defaultextension", extension.toString(), extensionName.getText());
        
        // Update extension name in configuration record and selector
        extension.setName (extensionName.getText());
        extensionSelector.update (extension);      
      }
      
      // Check if provider has been changed
      if (!provider.isParentRecord (extension))
      {
        // If so move extension to selected provider
        extension.changeParent (provider);
      }
    }
    
    // Update/set the extension with the entered values    
    extension.addOrReplaceElement ("extensionnumber", extensionNumber.getText ());
    extension.addOrReplaceElement ("username", extensionUsername.getText ());
    if (provider.getElementValue ("postpasswordparameter") != null)
    {
      extension.addOrReplaceEncryptedElement ("password", new String (extensionPassword.getPassword ()));
    }

    // Add new entry to list or update name of existing entry
    if (configurationRecord == null)
    {
      extensionList.add (extension);
    }
    else
    {
      extensionEntry.update ();
    }
    
    return true;    
        
  }
        
}    