/*
 * 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 imgmap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ilog.views.chart.IlvChart;
import ilog.views.chart.IlvLegend;
import ilog.views.chart.IlvLegendItem;
import ilog.views.chart.data.IlvDataSet;
import ilog.views.chart.faces.dhtml.IlvFacesChartImageMapGenerator;
import ilog.views.chart.servlet.IlvDefaultIMapDefinition;
import ilog.views.chart.servlet.IlvIMapAttributes;
import ilog.views.chart.servlet.IlvIMapDefinition;
import ilog.views.util.IlvResourceUtil;

/**
 * The image map areas generator.
 */
public class ImageMapAreaGenerator implements IlvFacesChartImageMapGenerator {

  /**
   * Creates the image maps definition of the slices of the Pie chart.
   * 
   * @param chart
   *          The Pie chart.
   */
  Override
  public IlvIMapDefinition createImageMapDefinition(IlvChart chart) {
    IlvDefaultIMapDefinition mapdef = null;
    IlvDataSet dataSet = chart.getDataSource().getDataSet(0);
    // Get the href property read by our HREFPropertyReader class.
    SuppressWarnings("unchecked")
    List<String> hrefs = (List<String>) dataSet.getProperty(HREFPropertyReader.HREFS_TAG);
    if (hrefs != null) {
      String[] hrefsValues = new String[hrefs.size()];
      String[] titles = new String[hrefs.size()];
      for (int i = 0; i < hrefsValues.length; ++i) {
        String srequest = i + "";
        titles[i] = dataSet.getDataLabel(i);
        hrefsValues[i] = srequest;
      }
      // Create a new IlvDefaultIMapAttributes instance initialized with
      // the contents of the hrefs and title arrays.
      IlvIMapAttributes[] attrs = new IlvIMapAttributes[hrefsValues.length];
      Map<IlvLegendItem, IlvIMapAttributes> legAttrs = new HashMap<IlvLegendItem, IlvIMapAttributes>(
          hrefsValues.length);
      IlvLegend legend = chart.getLegend();
      for (int i = 0; i < hrefsValues.length; ++i) {
        attrs[i] = new SliceIMapAttributes(hrefsValues[i], titles[i], (String) hrefs.get(i));
        legAttrs.put((IlvLegendItem) legend.getComponent(i), attrs[i]);
      }
      // Create the IlvIMapDefinition instance.
      try {
        mapdef = new IlvDefaultIMapDefinition(new IlvDataSet[] { dataSet }, new IlvIMapAttributes[][] { attrs });
        mapdef.setLegendItemAttributes(legAttrs);
      } catch (IllegalArgumentException e) {
        String msg = IlvResourceUtil.getServerLocaleString(ImageMapAreaGenerator.class, "notCreateMapDefinition");
        System.err.println(msg + e.getMessage());
        e.printStackTrace();
      }
    }
    return mapdef;

  }

}