/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

package bank.css;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 * A bean to handle the css themes (page and chart) in the bank chart sample.
 */
public class CSSBean implements Serializable {

  private static final long serialVersionUID = -6865467180683251900L;
  private String theme;
  private List<SelectItem> selectItems = null;

  Map<String, CSSTheme> themeMap = new HashMap<String, CSSTheme>();

  /**
   * Creates a CSS Bean. Registers predefined themes and set the default one.
   */
  public CSSBean() {
    addTheme(BlueTheme.KEY, new BlueTheme());
    addTheme(GreenTheme.KEY, new GreenTheme());

    // select the default theme
    setTheme(BlueTheme.KEY);
  }

  /**
   * Registers a theme in this bean.
   * 
   * @param key
   *          The key of the theme.
   * @param theme
   *          The theme to register.
   */
  public void addTheme(String key, CSSTheme theme) {
    themeMap.put(key, theme);
  }

  /**
   * Returns the current theme of this bean.
   * 
   * @return The current theme of this bean.
   */
  public CSSTheme getTheme() {
    return (CSSTheme) themeMap.get(theme);
  }

  /**
   * Returns the current theme of this bean.
   * 
   * @return The current theme of this bean.
   */
  public String getThemeKey() {
    CSSTheme theme = getTheme();
    return theme != null ? theme.getKey() : null;
  }

  /**
   * Works is done on ChangeListener but this setter needs to exist so that the
   * faces resolver doesn't think that thmeKey is read-only
   * 
   * @see #themeChangeListener(ValueChangeEvent)
   * @param key
   */
  public void setThemeKey(String key) {
    // work is done on themeChangeListener
  }

  /**
   * Sets the current theme of this bean.
   * 
   * @param theme
   *          The theme to set.
   */
  public void setTheme(String theme) {
    this.theme = theme;
  }

  /**
   * Get the select items that represents the color schemes managed by this
   * bean.
   * 
   * @return The select items.
   */
  public List<SelectItem> getSelectItems() {
    if (selectItems == null)
      buildSelectItems();
    return selectItems;
  }

  private void buildSelectItems() {
    selectItems = new ArrayList<SelectItem>();
    Iterator<String> iter = themeMap.keySet().iterator();
    SuppressWarnings("unchecked")
    Map<String, String> bundle = (Map<String, String>) FacesContext.getCurrentInstance().getExternalContext()
        .getRequestMap().get("bundle");
    while (iter.hasNext()) {
      String key = iter.next();
      SelectItem item = new SelectItem(key, bundle.get(key));
      selectItems.add(item);
    }
  }

  /**
   * A value change listener that applies the new theme selected on the client.
   * 
   * @param event
   *          The value change event.
   */
  public void themeChangeListener(ValueChangeEvent event) {
    String s = (String) event.getNewValue();
    if (s != null)
      setTheme(s);
  }

}