
package de.ivu.elect.presentation.konfiguration.table;

import de.ivu.elect.business.konfiguration.entity.CustomParameter;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.faces.UserLocale;
import de.ivu.elect.jee.presentation.dynamictable.ColumnType;
import de.ivu.elect.presentation.table.DefaultColumnEntry;


import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.StringUtils.abbreviate;
import static org.apache.commons.lang3.StringUtils.length;

public class CustomParameterWertColumn<E extends CustomParameter< ? >> extends DefaultColumnEntry<E> {

  private static final long serialVersionUID = 1L;

  private static final int DISPLAY_VALUE_MAX_LENGTH = 200;

  public CustomParameterWertColumn(Messages messages, UserLocale userLocale) {
    super("CustomParameter-Wert", messages, "custom.parameter.wert", ColumnType.STRING, parameter -> {
      String result = "";

      if (nonNull(parameter)) {
        result = parameter.getColumnDisplayValue(messages, userLocale);
      }

      return abbreviate(result, DISPLAY_VALUE_MAX_LENGTH);
    });
  }

  @Override
  public String getTooltip(E parameter) {
    if (nonNull(parameter) && length(parameter.getWert()) > DISPLAY_VALUE_MAX_LENGTH) {
      return parameter.getWert();
    }

    return super.getTooltip(parameter);
  }
}
