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

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

import javax.faces.convert.Converter;

public class LambdaStringSelectManyProperty extends AbstractSelectManyProperty<String> {

  private final String defaultLabel;

  private final boolean defaultRequired;

  private final Consumer<Collection<String>> setter;

  private final Supplier<Collection<String>> getter;

  private final List<String> values;

  private final boolean showSeparateItems;

  public LambdaStringSelectManyProperty(PropertyType propertyType, String defaultLabel, boolean defaultRequired, Consumer<Collection<String>> setter, Supplier<Collection<String>> getter, List<String> values) {
    this(propertyType, defaultLabel, defaultRequired, setter,  getter,  values, true);
  }

  public LambdaStringSelectManyProperty(PropertyType propertyType, String defaultLabel, boolean defaultRequired, Consumer<Collection<String>> setter, Supplier<Collection<String>> getter, List<String> values, boolean showSeparateItems) {
    super(propertyType);
    this.defaultLabel = defaultLabel;
    this.defaultRequired = defaultRequired;
    this.setter = setter;
    this.getter = getter;
    this.values = values;
    this.showSeparateItems = showSeparateItems;
  }

  @Override
  protected Collection<String> getValueAsCollection() {
    return getter.get();
  }

  @Override
  protected void setValueAsCollection(Collection<String> collection) {
    setter.accept(collection);
  }

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

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

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

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

  @Override
  public String getItemLabel(String e) {
    return e;
  }

  @Override
  public boolean isShowSeparateItems() {
    return showSeparateItems;
  }
}
