
package de.ivu.elect.presentation;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.faces.convert.Converter;

import de.ivu.elect.jee.entity.Entity;
import de.ivu.elect.jee.faces.EntityConverter;
import de.ivu.elect.jee.presentation.dynamicform.AbstractSelectOneMenuProperty;

public abstract class AbstractEntitySelectOneMenuProperty<E extends Entity< ? , ? >> extends AbstractSelectOneMenuProperty<E> {

  private final List<E> values;

  private final boolean hasNoSelectionItem;

  public AbstractEntitySelectOneMenuProperty(List<E> values, boolean hasNoSelectionItem) {
    this(values, (one, another) -> 0, hasNoSelectionItem);
  }

  public AbstractEntitySelectOneMenuProperty(List<E> values, Comparator<E> valuesComparator, boolean hasNoSelectionItem) {
    super();

    this.values = new ArrayList<>(values);
    this.values.sort(valuesComparator);

    this.hasNoSelectionItem = hasNoSelectionItem;
  }

  @Override
  public boolean getDefaultRequired() {
    return false;
  }

  @Override
  public boolean hasNoSelectionItem() {
    return hasNoSelectionItem;
  }

  @Override
  public String getDefaultLabel() {
    return getMessage(getMessageKeyEntityEinzahl());
  }

  protected abstract String getMessageKeyEntityEinzahl();

  @Override
  public List<E> getValues() {
    return values;
  }

  @Override
  public Converter getDefaultConverter() {
    return EntityConverter.getInstance();
  }
}
