/*
 * ProviderConfigurationForm.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 the configuration data for a VoIP provider
 *
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ProviderConfigurationForm extends AddressBookConfigurationForm
{
  
  private JTextField providerName;
  private JTextField postURL;
  private JTextField okResponse;
  private JTextField postUsernameParameter;
  private JTextField postPasswordParameter;
  private JTextField postExtensionNumberParameter;
  private JTextField postNumberToCallParameter;
  private JTextField postExtraParameters;
  private JCheckBox sanitise;
  private ProviderListForm providerList;
  private ConfigurationListEntry providerEntry;
     
  public ProviderConfigurationForm (ConfigurationListEntry providerEntry, AddressBookConfiguration configuration, ProviderListForm providerList) 
  {
    super ("Configure Provider", providerEntry != null ? providerEntry.getRecord() : null, configuration, providerList);
    this.providerEntry = providerEntry;
    this.providerList = providerList;
  }
  
  protected void defineFields ()
  {    
    
    // Add fields for provider name,the URL for the post and the pattern for a success response
    providerName = addNameField ("Provider Name:");
    postURL = addTextField ("Request URL:", "posturl");
    okResponse = addTextField ("OK Response:", "okresponse");
    
    // Add fields for the names of the parameters to the post/get request
    addLabel ("Request Parameters");
    postUsernameParameter = addTextFieldTrailingLabel ("= Username", "postusernameparameter");
    postPasswordParameter = addTextFieldTrailingLabel ("= Password", "postpasswordparameter");
    postExtensionNumberParameter = addTextFieldTrailingLabel ("= Extension Number", "postextensionnumberparameter");
    postNumberToCallParameter = addTextFieldTrailingLabel ("= Number To Call", "postnumbertocallparameter");
    postExtraParameters = addTextField ("Extra Parameters:", "postextraparameters");

    // Add check box to specify if phone numbers need to be 'sanitised' by removing all non-digit characters before 
    // sending to server     
    sanitise = new JCheckBox ("Sanitise Numbers");
    sanitise.setSelected (configurationRecord != null ? configurationRecord.getBooleanValue ("sanitise") : false);
    sanitise.addActionListener (this);
    addField ("", sanitise);
    
    
  }
   
  protected void otherAction (AbstractButton source)
  {
  }
  
  protected boolean validateAndSave ()   
  {
    // Check that all required fields are filled in
    if (!checkFieldEntry (providerName, "Please enter the provider name"))
    {
      return false;
    }
    else if (!checkFieldEntry (postURL, "Please enter the URL for the request"))
    {
      return false;
    }
    else if (!checkFieldEntry (postExtensionNumberParameter, 
             "Please enter the name of the request parameter for specifying the extension number/id"))
    {
      return false;
    }
    else if (!checkFieldEntry (postNumberToCallParameter, 
             "Please enter the name of the request parameter for specifying the number to call"))
    {
      return false;
    }
    
    // Check for duplicate name
    ConfigurationRecord duplicate = configuration.getElementRecord ("provider", providerName.getText());
    if (duplicate != null && !duplicate.equals (configurationRecord))
    {
      JOptionPane.showMessageDialog (this, "There is already an entry for the provider " + providerName.getText(), 
                                     "Duplicate provider name", JOptionPane.ERROR_MESSAGE);
      return false;
    }
    
    // Create a new provider record if not updating an existing provider
    ConfigurationRecord provider;
    if (configurationRecord == null)
    {
      provider = configuration.addElementRecord ("provider", providerName.getText());
    }
    else
    {
      // Update name of existing record
      provider = configurationRecord;
      provider.setName (providerName.getText());
    }
    
    // Update/set the provider with the entered values    
    provider.addOrReplaceElement ("posturl", postURL.getText ());
    provider.addOrReplaceElement ("okresponse", okResponse.getText ());
    provider.addOrReplaceElement ("postusernameparameter", postUsernameParameter.getText ());
    provider.addOrReplaceElement ("postpasswordparameter", postPasswordParameter.getText ());
    provider.addOrReplaceElement ("postextensionnumberparameter", postExtensionNumberParameter.getText ());
    provider.addOrReplaceElement ("postnumbertocallparameter", postNumberToCallParameter.getText ());
    provider.addOrReplaceElement ("postextraparameters", postExtraParameters.getText ());
    provider.addOrReplaceElement ("sanitise", sanitise.isSelected() ? "1" : "0");
    
    // Add new entry to list or update name of existing entry
    if (configurationRecord == null)
    {
      providerList.add (provider);
    }
    else
    {
      providerEntry.update ();
    }
    
    return true;    
        
  }
        
}    