
package org.skyllias.alomatia.ui;

import java.util.prefs.Preferences;

import javax.swing.AbstractButton;

import org.skyllias.alomatia.ImageSource;
import org.skyllias.alomatia.i18n.LabelLocalizer;
import org.skyllias.alomatia.ui.RadioSelector.RadioSelectorListener;

/** Creator of radio buttons used to select one of the available {@link ImageSource}s.
 *  It is generic with respect to the component used to implement the radio buttons
 *  (JRadioButton or JRadioButtonMenuItem). */

public class SourceRadioSelector<RADIO extends AbstractButton> implements RadioSelectorListener<ImageSource>
{
  protected static final String PREFKEY_SOURCECOMMAND = "sourceCommandName";

  private final RadioSelector<RADIO, ImageSource> radioSelector;

  private ImageSource previousSource;

  private Preferences preferences = Preferences.userNodeForPackage(getClass());

//==============================================================================

  /** Creates a new instance that will create radio buttons of radioClazz type
   *  and enable or disable the sources as the radio buttons are selected. */

  public SourceRadioSelector(Class<RADIO> radioClazz, LabelLocalizer localizer)
  {
    radioSelector = new RadioSelector<>(radioClazz, localizer, this);
  }

//==============================================================================

  /** Returns a new radio button ready to activate source when selected,
   *  deactivating any other Image source that may have been selected through a
   *  radio button generated by this method. The first radio button is
   *  automatically selected, unless actionCommand is stored in the preferences,
   *  in which case source is taken selected.
   *  actionCommand is used as the key for localization and as the action
   *  command and name for the component. It must be different in all calls. */

  public RADIO createRadioObject(String actionCommand, ImageSource source)
  {
    RADIO radio = radioSelector.createRadioObject(actionCommand, source);

    String previousSelectionCommand = preferences.get(PREFKEY_SOURCECOMMAND, null);
    if (actionCommand.equals(previousSelectionCommand))
    {
      radioSelector.setSelectionByActionCommand(previousSelectionCommand);
    }

    previousSource = radioSelector.getCurrentSelection();
    if (previousSource != null) previousSource.setActive(true);                 // this may be redundant

    return radio;
  }

//------------------------------------------------------------------------------

  @Override
  public void onSelectionChanged(ImageSource selectedSource)
  {
    if (selectedSource != previousSource)
    {
      if (previousSource != null) previousSource.setActive(false);
      selectedSource.setActive(true);
    }
    previousSource = selectedSource;

    preferences.put(PREFKEY_SOURCECOMMAND, radioSelector.getCurrentSelectionAsActionCommand());
  }

//------------------------------------------------------------------------------

  /** Meant only for testing purposes. */

  protected void setPreferences(Preferences preferences)
  {
    this.preferences = preferences;
  }

//------------------------------------------------------------------------------

}
