package net.sf.jasperreports.charts.fill;

import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.charts.JRXyzDataset;
import net.sf.jasperreports.charts.JRXyzSeries;
import net.sf.jasperreports.charts.util.DefaultXYZDataset;
import net.sf.jasperreports.engine.JRChartDataset;
import net.sf.jasperreports.engine.JRExpressionCollector;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.design.JRVerifier;
import net.sf.jasperreports.engine.fill.JRCalculator;
import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
import net.sf.jasperreports.engine.fill.JRFillChartDataset;
import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
import net.sf.jasperreports.engine.util.Pair;
import org.jfree.data.general.Dataset;

public class JRFillXyzDataset extends JRFillChartDataset implements JRXyzDataset {
  protected JRFillXyzSeries[] xyzSeries = null;
  
  private DefaultXYZDataset dataset = null;
  
  private Map itemHyperlinks;
  
  public JRFillXyzDataset(JRXyzDataset xyzDataset, JRFillObjectFactory factory) {
    super((JRChartDataset)xyzDataset, factory);
    JRXyzSeries[] srcXyzSeries = xyzDataset.getSeries();
    if (srcXyzSeries != null && srcXyzSeries.length > 0) {
      this.xyzSeries = new JRFillXyzSeries[srcXyzSeries.length];
      for (int i = 0; i < this.xyzSeries.length; i++)
        this.xyzSeries[i] = (JRFillXyzSeries)factory.getXyzSeries(srcXyzSeries[i]); 
    } 
  }
  
  public JRXyzSeries[] getSeries() {
    return (JRXyzSeries[])this.xyzSeries;
  }
  
  protected void customInitialize() {
    this.dataset = new DefaultXYZDataset();
    this.itemHyperlinks = new HashMap();
  }
  
  protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException {
    if (this.xyzSeries != null && this.xyzSeries.length > 0)
      for (int i = 0; i < this.xyzSeries.length; i++)
        this.xyzSeries[i].evaluate(calculator);  
  }
  
  protected void customIncrement() {
    if (this.xyzSeries != null && this.xyzSeries.length > 0)
      for (int i = 0; i < this.xyzSeries.length; i++) {
        JRFillXyzSeries crtXyzSeries = this.xyzSeries[i];
        Comparable seriesName = crtXyzSeries.getSeries();
        if (seriesName == null)
          throw new JRRuntimeException("XYZ series name is null."); 
        this.dataset.addValue(crtXyzSeries.getSeries(), crtXyzSeries.getXValue(), crtXyzSeries.getYValue(), crtXyzSeries.getZValue());
        if (crtXyzSeries.hasItemHyperlinks()) {
          Map seriesLinks = (Map)this.itemHyperlinks.get(crtXyzSeries.getSeries());
          if (seriesLinks == null) {
            seriesLinks = new HashMap();
            this.itemHyperlinks.put(crtXyzSeries.getSeries(), seriesLinks);
          } 
          Pair xyKey = new Pair(crtXyzSeries.getXValue(), crtXyzSeries.getYValue());
          seriesLinks.put(xyKey, crtXyzSeries.getPrintItemHyperlink());
        } 
      }  
  }
  
  public Dataset getCustomDataset() {
    return (Dataset)this.dataset;
  }
  
  public byte getDatasetType() {
    return 4;
  }
  
  public void collectExpressions(JRExpressionCollector collector) {
    collector.collect(this);
  }
  
  public boolean hasItemHyperlinks() {
    boolean foundLinks = false;
    if (this.xyzSeries != null && this.xyzSeries.length > 0)
      for (int i = 0; i < this.xyzSeries.length && !foundLinks; i++) {
        JRFillXyzSeries serie = this.xyzSeries[i];
        foundLinks = serie.hasItemHyperlinks();
      }  
    return foundLinks;
  }
  
  public Map getItemHyperlinks() {
    return this.itemHyperlinks;
  }
  
  public void validate(JRVerifier verifier) {
    verifier.verify(this);
  }
}
