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

import static org.apache.commons.lang3.StringUtils.isBlank;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

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

public abstract class AbstractChartModelBuilder<C extends ChartModel, B extends AbstractChartModelBuilder<C, ? >> {

  public static final String LEGEND_POSITION_WEST = "w";

  public static final String LEGEND_POSITION_EAST = "e";

  public static final String ROT = "CC3E3E";

  public static final String DUNKEL_ROT = "800000";

  public static final String GELB = "EEE100";

  public static final String GRUEN = "8fcc3e";

  public static final String DUNKEL_GRUEN = "006600";

  public static final String BLAU = "039EE6";

  public static final String DUNKEL_BLAU = "000099";

  public static final String GRAU = "d6cdb4";

  public static final String HELL_GRAU = "fff7dd";

  public static final String DUNKEL_GRAU = "aca48c";

  public static final String PINK = "ffc0cb";

  public static final String ORANGE = "ff9933";

  public static final String DUNKEL_ORANGE = "cc9900";

  public static final String CYAN = "66ffff";

  public static final String WEISS = "ffffff";

  public static final String SCHWARZ = "333333";

  private final List<String> colorList = new ArrayList<>();

  public AbstractChartModelBuilder() {
    super();
  }

  public B add(String label, Number data, @Nullable String color) {
    if (isBlank(color)) {
      colorList.add(GRAU);
    } else {
      colorList.add(color);
    }

    addValue(label, data);

    return (B) this;
  }

  protected abstract void addValue(String label, Number data);

  public B setTitle(String title) {
    getModel().setTitle(title);

    return (B) this;
  }

  public B setLegend(LegendPlacement legendPlacement, String legendPosition) {
    getModel().setLegendPosition(legendPosition);
    getModel().setLegendPlacement(legendPlacement);

    return (B) this;
  }

  protected abstract C getModel();

  public C buildModel() {
    StringBuilder builder = new StringBuilder();

    for (String color : colorList) {
      if (builder.length() > 0) {
        builder.append(',');
      }

      builder.append(color);
    }

    C model = getModel();
    model.setSeriesColors(builder.toString());
    model.setNegativeSeriesColors(builder.toString());

    return model;
  }
}
