
package de.ivu.elect.presentation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import de.ivu.elect.jee.entity.Entity;
import de.ivu.elect.jee.presentation.dynamicform.PropertyType;

public class LambdaEntitySelectManyProperty<E extends Entity< ? , ? >> extends AbstractEntitySelectManyProperty<E> {

  private final Consumer<Collection<E>> setter;

  private final Supplier<Collection<E>> getter;

  private final Function<E, String> itemLabelFct;

  private final String labelMsgKey;

  private final boolean required;

  public LambdaEntitySelectManyProperty(List<E> values, PropertyType propertyType, Consumer<Collection<E>> setter, Supplier<Collection<E>> getter, Function<E, String> itemLabelFct, String labelMsgKey,
      boolean required) {
    super(values, propertyType);

    this.setter = setter;
    this.getter = getter;

    this.labelMsgKey = labelMsgKey;

    this.itemLabelFct = itemLabelFct;

    this.required = required;
  }

  @Override
  protected String getMessageKeyEntityMehrzahl() {
    return labelMsgKey;
  }

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

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

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

  @Override
  public String getItemLabel(E e) {
    if (e == null) {
      return "";
    }

    return itemLabelFct.apply(e);
  }
}
