
package de.ivu.elect.presentation.table;

import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.presentation.dynamictable.ColumnType;

import javax.annotation.Nullable;
import java.util.function.Function;

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

public class DefaultColumnEntry<E> extends AbstractColumnEntry<E> {

  private static final long serialVersionUID = 1L;

  public static final String PREFIX_GENERIERTER_COLUMN_KLASSEN = "Abstract";

  private final String id;

  private String headerKey;

  private final ColumnType type;

  private final Function<E, Object> getter;

  private final Object[] headerParams;

  private Function<E, String> tooltip;

  private String style = null;

  protected Function<Object, String> valueLabeler = null;

  @Deprecated // Eindeutige Id vergeben, wichtig für Spaltenreihenfolge ELECT-4631
  public DefaultColumnEntry(Messages messages, String headerKey, ColumnType type, Function<E, Object> getter, Object... headerParams) {
    this(null, messages, headerKey, type, getter, headerParams);
  }

  public DefaultColumnEntry(String id, Messages messages, String headerKey, ColumnType type, Function<E, Object> getter, Object... headerParams) {
    this(id, messages, headerKey, type, getter, e -> "", headerParams);
  }

  public DefaultColumnEntry(String id, Messages messages, String headerKey, ColumnType type, Function<E, Object> getter, Function<E, String> tooltip, Object... headerParams) {
    super(messages);

    this.id = id;

    this.type = type;

    this.headerKey = headerKey;
    this.headerParams = headerParams;

    this.getter = getter;

    this.tooltip = tooltip;
  }

  public String getId() {
    return id;
  }

  @Override
  public String getName() {
    if (isBlank(id)) {
      String name = super.getName();

      if (DefaultColumnEntry.class.getName().equals(name) || name.startsWith(PREFIX_GENERIERTER_COLUMN_KLASSEN)) {
        return name + headerKey + type + getter; // Notlösung ELECT-4631
      }

      return name;
    }

    return id;
  }

  @Override
  public ColumnType getType() {
    return type;
  }

  @Override
  public String getHeader() {
    return getMessage(getHeaderKey(), getHeaderParams());
  }

  public String getHeaderKey() {
    return headerKey;
  }

  public Function<E, Object> getGetter() {
    return getter;
  }

  protected Object[] getHeaderParams() {
    return headerParams;
  }

  @Override
  public Object getValue(E entity) {
    Object value = getter.apply(entity);

    if (valueLabeler == null) {
      if (ColumnType.ENUM.equals(type) && value instanceof Enum /*ELECTMUC-2327*/) {
        return getMessage((Enum) value);
      }

      return value;
    }

    return valueLabeler.apply(value);
  }

  public void setTooltipFct(Function<E, String> tooltip) {
    this.tooltip = tooltip;
  }

  @Override
  public String getTooltip(E entity) {
    return tooltip.apply(entity);
  }

  @Nullable
  @Override
  public String getStyle() {
    return style;
  }

  public void setStyle(String style) {
    this.style = style;
  }

  public void setLineBreak() {
    setStyle("line-break");
  }

  public void setValueLabeler(Function<Object, String> valueLabeler) {
    this.valueLabeler = valueLabeler;
  }

  public void setHeaderKey(String headerKey) {
    this.headerKey = headerKey;
  }
}
