/*
 * 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 faces.dhtml;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import ilog.views.chart.IlvChart;
import ilog.views.chart.data.IlvDataSetPoint;
import ilog.views.chart.data.IlvDataSource;
import ilog.views.chart.data.IlvDefaultDataSet;
import ilog.views.chart.data.IlvDefaultDataSource;
import ilog.views.chart.faces.dhtml.component.IlvChartDHTMLView;
import ilog.views.chart.faces.servlet.IlvFacesChart;
import ilog.views.faces.dhtml.event.FacesViewActionEvent;
import ilog.views.faces.dhtml.interactor.IlvObjectSelectInteractor;
import ilog.views.util.servlet.IlvMenuFactory;

/**
 * The application bean.
 */
public class ChartBean {

  /**
   * The Chart graphic component.
   */
  private IlvChart chart;

  /**
   * The Chart View JSF component.
   */
  private IlvChartDHTMLView chartView;

  /**
   * The contextual menu menuFactory.
   */
  private IlvMenuFactory menuFactory = new MenuFactory();

  /**
   * The image map generator
   */
  private ImapGenerator imapGenerator = new ImapGenerator();

  /**
   * The chart header text property
   */
  private Object header;

  /**
   * A object used to display a value.
   */
  private Object value;

  /**
   * Returns the chart instance.
   * 
   * @return The chart graphic component.
   */
  public IlvChart getChart() {
    if (chart == null) {
      chart = new IlvFacesChart();
      fillChart(chart);
    }
    return chart;
  }

  /**
   * Fill the chart with data.
   * 
   * @param chart
   *          The chart to populate.
   */
  static void fillChart(IlvChart chart) {
    try {
      IlvDataSource source = new IlvDefaultDataSource();
      double x[] = { 1, 3, 2, 4, 6, 5, 10, 2, 3, 0 };
      IlvDefaultDataSet dds = new IlvDefaultDataSet("Sample", x);
      source.setDataSet(0, dds);
      chart.setDataSource(source);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Return the JSF chart view component.
   * 
   * @return The Chart View JSF component.
   */
  public IlvChartDHTMLView getChartView() {
    if (chartView == null) {
      chartView = new IlvChartDHTMLView();
      IlvChart chart = getChart();
      chartView.setChart(chart);
    }
    return chartView;
  }

  /**
   * Sets the JSF chart view component.
   * 
   * @param chart
   *          The JSF Chart view component to set.
   */
  public void setChartView(IlvChartDHTMLView chart) {
    chartView = chart;
  }

  /**
   * Sets the Chart graphic component.
   * 
   * @param chart
   *          The chart graphic component.
   */
  public void setChart(IlvChart chart) {
    this.chart = chart;
  }

  /**
   * <p>
   * This method is a value change listener called by a chart select interactor.
   * </p>
   * <p>
   * The listener will set the pseudo class "selected" on the display point
   * picked with the interactor. The CSS has a rule to customize the point with
   * this pseudo class.
   * </p>
   * 
   * @param evt
   *          The value change event.
   */
  public void pointSelected(ValueChangeEvent evt) {

    IlvDataSetPoint point = (IlvDataSetPoint) evt.getNewValue();

    if (point != null) {

      // The source of the event is the interactor
      IlvObjectSelectInteractor interactor = (IlvObjectSelectInteractor) evt.getSource();

      // Retrieve the JSF view connected to the interactor
      IlvChartDHTMLView jsfView = (IlvChartDHTMLView) interactor.getView();

      // Retrieve the IlvChart wrapped by the JSF component.
      IlvChart chart = jsfView.getChart();

      // Set a pseudo class on the display point.
      // A CSS rule like point:selected { ... }
      // will customize the graphic representation of the point.
      chart.setPseudoClasses(point.getDataSet(), point.getIndex(), new String[] { "selected" });
      value = "Point values: x= " + point.getXData() + " y=" + point.getYData();
    }
  }

  /**
   * <p>
   * This method is a view event listener called by a chart pop-up menu.
   * </p>
   * <p>
   * The listener will set the pseudo class "selected" on the display point
   * picked when the pop-up was triggered. The CSS has a rule to customize the
   * point with this pseudo class.
   * </p>
   * 
   * @param event
   *          The view action event.
   */
  public void pointSelected(FacesViewActionEvent event) {
    IlvDataSetPoint point = (IlvDataSetPoint) event.getObject();
    if (point != null) {
      IlvChart chart = (IlvChart) event.getGraphicComponent();
      chart.setPseudoClasses(point.getDataSet(), point.getIndex(), new String[] { "selected" });
    }
  }

  /**
   * Sets the graphic component current visible window to the client side
   * representation of the chart view.
   * 
   * @param evt
   *          The action event.
   */
  public void reset(ActionEvent evt) {
    chartView.setDefaultChartVisibleWindow();
  }

  public void setHeaderLabel(ValueChangeEvent evt) {
    chart.setHeaderText((String) evt.getNewValue());
  }

  public Object getHeader() {
    return header;
  }

  public void setHeader(Object object) {
    header = object;
  }

  public void setFooterLabel(ValueChangeEvent evt) {
    chart.setFooterText((String) evt.getNewValue());
  }

  public IlvMenuFactory getMenuFactory() {
    return menuFactory;
  }

  public ImapGenerator getImapGenerator() {
    return imapGenerator;
  }

  public Object getValue() {
    return value;
  }

  public void setValue(Object value) {
    this.value = value;
  }
}