package de.ivu.elect.jee.presentation.dynamicform;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.faces.convert.Converter;

public class LambdaStringSelectOneProperty extends AbstractSelectOneMenuProperty<String> {

  private final String defaultLabel;

  private final boolean defaultRequired;

  private final Consumer<String> setter;

  private final Supplier<String> getter;

  private final List<String> values;

  private final boolean hasNoSelectionItem;

  private Function<String, String> labelFct = Function.identity();

  public LambdaStringSelectOneProperty(String defaultLabel, boolean defaultRequired, Consumer<String> setter, Supplier<String> getter, List<String> values, boolean hasNoSelectionItem) {
    this.defaultLabel = defaultLabel;
    this.defaultRequired = defaultRequired;
    this.setter = setter;
    this.getter = getter;
    this.values = values;
    this.hasNoSelectionItem = hasNoSelectionItem;
  }

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

  @Override
  public String getDefaultLabel() {
    return defaultLabel;
  }

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

  @Override
  public Converter getDefaultConverter() {
    return null;
  }

  @Override
  public String getValue() {
    return getter.get();
  }

  @Override
  public void setValue(String value) {
    setter.accept(value);
  }

  @Override
  public List<String> getValues() {
    return values.stream().filter(getValueFilter()).collect(Collectors.toList());
  }

  @Override
  public String getItemLabel(String item) {
    return labelFct.apply(item);
  }

  public void setLabelFct(Function<String, String> labelFct) {
    this.labelFct = labelFct;
  }
}
