
package de.ivu.elect.presentation.charts;

import java.io.Serializable;

import javax.inject.Named;

import org.primefaces.model.chart.PieChartModel;

@Named
public class PieChartStyle implements Serializable {

  private static final long serialVersionUID = 1L;

  private static final int CANVAS_MIN_HEIGHT_IN_PX = 200;

  private static final int CHART_LEGEND_ENTRY_HEIGHT_IN_PX = 30;

  public PieChartStyle() {
    super();
  }

  public String getOuterPanelStyle(PieChartModel pieChartModel) {
    int width = pieChartModel.getDiameter() * 2;

    return "white-space:nowrap; width:" + width + "px; height:" + calculateChartLegendHeightInPx(pieChartModel) + "px;";
  }

  public String getCanvasStyle(PieChartModel pieChartModel) {
    return "height:" + calculateChartLegendHeightInPx(pieChartModel) + "px;";
  }

  private int calculateChartLegendHeightInPx(PieChartModel pieChartModel) {
    int legendEntriesCount = pieChartModel.getData().size();
    int legendHeight = CHART_LEGEND_ENTRY_HEIGHT_IN_PX * legendEntriesCount;

    return Math.max(CANVAS_MIN_HEIGHT_IN_PX, legendHeight);
  }
}
