package ervu_dashboard.component.chart.label;

import java.util.Collections;
import property.grid.Formatter;
import rpc.LoadGraphSource;

import ru.cg.webbpm.modules.database.api.bean.TableRow;
import ru.cg.webbpm.modules.database.api.dao.LoadDao;
import ru.cg.webbpm.modules.database.api.dao.option.LoadOptions;
import ru.cg.webbpm.modules.database.bean.AggregationFunction;
import ru.cg.webbpm.modules.database.bean.annotation.LocalGraphSource;
import ru.cg.webbpm.modules.database.bean.entity_graph.AggregateFuncField;
import ru.cg.webbpm.modules.database.bean.entity_graph.EntityColumn;
import ru.cg.webbpm.modules.standard_annotations.validation.NotNull;

/**
 * @author Vitaly Chekushkin
 */
public class DefaultRoundLabelConfiguration extends AbstractRoundLabelConfiguration
    implements LoadGraphSource {

  @NotNull
  public LoadDao loadDao;

  @NotNull
  @LocalGraphSource(sourceFieldName = "loadDao")
  public EntityColumn valueColumn;

  @NotNull
  public AggregationFunction aggregationFunction;

  public Formatter<Object, Object> valueFormatter;

  @Override
  protected String getLabel(LoadOptions loadOptions) {
    AggregateFuncField funcField = new AggregateFuncField(valueColumn, aggregationFunction);
    TableRow tableRow = loadDao.loadAggregations(Collections.singleton(funcField), loadOptions);
    Object value = tableRow.get(valueColumn);
    value = valueFormatter != null ? valueFormatter.format(value) : value;
    return value != null ? value.toString() : null;
  }
}
