package ru.micord.ervu.account_applications.component.property.grid;

import java.util.Collection;
import java.util.Collections;

import component.field.dataconvert.DataConverter;
import component.field.dataconvert.DataConverterProvider;
import component.field.persist.filter.AbstractFilterComponent;
import component.grid.model.PinnedType;
import property.grid.Column;
import property.grid.Formatter;
import utils.GridUtils;

import ru.cg.webbpm.modules.database.bean.AggregationFunction;
import ru.cg.webbpm.modules.database.bean.entity_graph.EntityColumn;
import ru.cg.webbpm.modules.standard_annotations.editor.Visible;
import ru.cg.webbpm.modules.webkit.annotations.Exclude;
import ru.cg.webbpm.modules.webkit.annotations.Model;

/**
 * @author Adel Kalimullin
 */
@Model
public class StaticGridColumn extends AbstractFilterComponent<Object, Object>
    implements Column {

  public StaticColumn field;
  public String displayName;
  public String headerTooltip;
  @Visible(predicate = "displayPopup != true")
  public String columnTooltip;
  public Integer width;
  public boolean widthFixed;
  public boolean autoHeight;
  public boolean hidden;
  public boolean sortable;
  public boolean disableHiding;
  public PinnedType pinned;
  public boolean filter;
  @Exclude
  public Formatter<?, ?> formatter;
  public AggregationFunction aggregationFunction;
  @Exclude
  public Formatter<?, ?> aggregationFormatter;
  @Exclude
  public Formatter<?, ?> exportFileFormatter;
  public boolean displayPopup;
  public boolean suppressHeaderMenu;

  public EntityColumn getField() {
    return GridUtils.toEntityColumn(field.column);
  }

  @Override
  public String getDisplayName() {
    return displayName;
  }

  public Collection<EntityColumn> getEntityColumns() {
    return Collections.singleton(getField());
  }

  public AggregationFunction getAggregationFunction() {
    return this.aggregationFunction;
  }

  public Formatter<?, ?> getFormatter() {
    return this.formatter;
  }

  public Formatter<?, ?> getAggregationFormatter() {
    return this.aggregationFormatter;
  }

  public Formatter<?, ?> getExportFormatter() {
    return exportFileFormatter;
  }

  public Object convertData(Object rawValue) {
    if (this.dataConverter != null) {
      return this.dataConverter.convertValueForSave(rawValue);
    }
    else {
      DataConverter converter = DataConverterProvider.getDataConverter(rawValue.getClass());
      return converter.convertValueForSave(rawValue);
    }
  }
}