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

import static org.apache.commons.lang3.StringUtils.abbreviate;
import static org.apache.commons.lang3.StringUtils.length;

import java.util.Locale;

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

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

  private static final long serialVersionUID = 1L;

  private static final int DISPLAY_VALUE_MAX_LENGTH = 200;

  private final Locale locale;

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

      if (parameter != null) {
        result = parameter.getDisplayValueFormatted(messages, locale);
      }

      return abbreviate(result, DISPLAY_VALUE_MAX_LENGTH);
    });

    this.locale = locale;
  }

  @Override
  public String getTooltip(E parameter) {
    if (parameter != null) {
      String value = parameter.getDisplayValueFormatted(getMessages(), locale);

      if (length(value) > DISPLAY_VALUE_MAX_LENGTH) {
        return value;
      }
    }

    return super.getTooltip(parameter);
  }
}
