package net.sf.jasperreports.charts.util;

import java.util.Map;
import net.sf.jasperreports.engine.JRPrintHyperlink;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.data.time.TimePeriod;
import org.jfree.data.time.TimePeriodValues;
import org.jfree.data.time.TimePeriodValuesCollection;

public class TimePeriodChartHyperlinkProvider implements ChartHyperlinkProvider {
  private static final long serialVersionUID = 10200L;
  
  private Map itemHyperlinks;
  
  public TimePeriodChartHyperlinkProvider(Map itemHyperlinks) {
    this.itemHyperlinks = itemHyperlinks;
  }
  
  public JRPrintHyperlink getEntityHyperlink(ChartEntity entity) {
    JRPrintHyperlink printHyperlink = null;
    if (hasHyperlinks() && entity instanceof XYItemEntity) {
      XYItemEntity itemEntity = (XYItemEntity)entity;
      TimePeriodValuesCollection dataset = (TimePeriodValuesCollection)itemEntity.getDataset();
      TimePeriodValues series = dataset.getSeries(itemEntity.getSeriesIndex());
      Map serieHyperlinks = (Map)this.itemHyperlinks.get(series.getKey());
      if (serieHyperlinks != null) {
        TimePeriod timePeriod = series.getTimePeriod(itemEntity.getItem());
        printHyperlink = (JRPrintHyperlink)serieHyperlinks.get(timePeriod);
      } 
    } 
    return printHyperlink;
  }
  
  public boolean hasHyperlinks() {
    return (this.itemHyperlinks != null && this.itemHyperlinks.size() > 0);
  }
}
