/*
 * ExtensionSelector.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 combo box used to select the extension to be used for dialing
 */

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.*;
import java.net.*;
public class ExtensionSelector extends JComboBox
{
  private static final String computerName = getComputerName ();
  private AddressBookConfiguration configuration;
  private EditPanel panel;
  private ConfigurationRecord widestEntry = null;
  private double widestEntryWidth = 0;
  
  private static String getComputerName ()
  {
    try
    {
      return InetAddress.getLocalHost().getHostName();
    }
    catch (UnknownHostException e)
    {
      return null;
    }
  }
  
  public void initialise (AddressBookConfiguration configuration, EditPanel panel)
  {
    this.configuration = configuration;
    this.panel = panel;
    
    // Get extension last used on this computer (if any)
    String defaultExtension = configuration.getElementValue ("defaultextension", "computer", computerName);
    
    // Get list of extensions from configuration and add to list
    List<ConfigurationRecord> extensions = configuration.getElementRecords ("extension");
    for (ConfigurationRecord extension : extensions)
    {
      add (extension);     
      
      // Select this extension if it was the last one used on this computer
      if (defaultExtension != null && defaultExtension.equals (extension.toString()))
      {
        setSelectedItem (extension);
      }
    }
  }
  
  private void checkWidth (ConfigurationRecord extension)
  {
    setPrototypeDisplayValue (extension);
    double currentWidth = getPreferredSize().getWidth(); 
    if (currentWidth > widestEntryWidth)
    {
      widestEntry = extension;
      widestEntryWidth = currentWidth;
    }
    else
    {
      setPrototypeDisplayValue (widestEntry);
    }
  }
  
  private void adjustWidth ()
  {
    widestEntryWidth = 0;
    List<ConfigurationRecord> extensions = configuration.getElementRecords ("extension");
    for (ConfigurationRecord extension : extensions)
    {
      checkWidth (extension);
    }
  }
  
  public void add (ConfigurationRecord extension)
  {
    // Add entry to list
    addItem (extension);
    
    // Re-adjust width of selector if this is now widest entry 
    checkWidth (extension);
    
    // If this was first extension enable call buttons
    if (getItemCount() == 1)
    {
      panel.enableCallButtons ();
    }
  }
  
  public void remove (ConfigurationRecord extension)
  {
    // Remove any default extension eleents in configuration
    configuration.updateElements ("defaultextension", extension.toString(), null);
    
    // Find matching entry in data model
    int numEntries = getItemCount ();
    for (int i = 0; i < numEntries; ++i)
    {
      ConfigurationRecord item = (ConfigurationRecord) getItemAt (i);
      if (item.equals (extension))
      {
        removeItem (item);
        break;
      }
    }
    
    // Re-adjust width of selector if widest entry was removed
    if (extension.equals (widestEntry))
    {
      adjustWidth ();
    }      
    repaint ();
    
    // If this was only extension disable call buttons
    if (getItemCount() == 0)
    {
      panel.disableCallButtons ();
    }
  }
  
  public void update (ConfigurationRecord extension)
  {
    // Re-adjust width of selector if widest entry was updated
    if (extension.equals (widestEntry))
    {
      adjustWidth ();
    }
    else
    {     
      // Otherwise check if this is now widest entry
      checkWidth (extension);
    }
    
    // Reset current selection if selected item was updated
    if (extension.equals (getSelectedExtension ()))
    {
      setSelectedItem (extension);
      repaint ();
    }
  }
  
  public void saveSelectedExtension ()
  {
    configuration.addOrReplaceElement ("defaultextension", "computer", computerName, getSelectedExtension().toString());
  }
  
  public ConfigurationRecord getSelectedExtension ()
  {
    return (ConfigurationRecord) getSelectedItem();
  }
}
      