
package de.ivu.elect.jee.presentation.charts;

import javax.annotation.Nonnull;

import org.primefaces.model.chart.LegendPlacement;
import org.primefaces.model.chart.PieChartModel;

public class PieChartModelBuilder extends AbstractChartModelBuilder<PieChartModel, PieChartModelBuilder> {

  public static final int PIE_CHART_DIAMETER_IN_PX = 100;

  private final PieChartModel model = new PieChartModel();

  public PieChartModelBuilder() {
    super();

    model.setSliceMargin(0); // größere Zahlen verursachen in PF 5.1 Fehler

    model.setDataFormat("value");
    model.setLegendPosition(PieChartModelBuilder.LEGEND_POSITION_WEST);
    model.setDiameter(PIE_CHART_DIAMETER_IN_PX);
    model.setShadow(true);
    model.setShowDataLabels(true);
  }

  @Override
  protected void addValue(String label, Number data) {
    model.set(label, data);
  }

  @Override
  protected PieChartModel getModel() {
    return model;
  }

  public PieChartModelBuilder setShowDataLabels(boolean showDataLabels) {
    model.setShowDataLabels(showDataLabels);

    return this;
  }

  public PieChartModelBuilder setDiameter(int diameter) {
    model.setDiameter(diameter);

    return this;
  }

  @Nonnull
  public static PieChartModelBuilder getDefaultInstance() {
    PieChartModelBuilder pieChartModelBuilder = new PieChartModelBuilder();
    pieChartModelBuilder.setDiameter(130);
    pieChartModelBuilder.setShowDataLabels(false);
    pieChartModelBuilder.setLegend(LegendPlacement.OUTSIDE, PieChartModelBuilder.LEGEND_POSITION_EAST);

    return pieChartModelBuilder;
  }
}
