package de.ivu.elect.presentation;

import de.ivu.elect.jee.entity.Entity;

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

public class LambdaEntitySelectOneMenuProperty<E extends Entity<?, ?>> extends AbstractEntitySelectOneMenuProperty<E> {

  private final Consumer<E> setter;

  private final Supplier<E> getter;

  private final Function<E, String> labeler;

  private final String msgKey;

  private final boolean defaultRequired;

  public LambdaEntitySelectOneMenuProperty(List values, Consumer<E> setter, Supplier<E> getter, Function<E, String> labeler, String msgKey, boolean required, boolean hasNoSelectionItem) {
    super(values, hasNoSelectionItem);
    this.setter = setter;
    this.getter = getter;
    this.labeler = labeler;
    this.msgKey = msgKey;
    defaultRequired = required;
  }


  @Override
  protected String getMessageKeyEntityEinzahl() {
    return msgKey;
  }

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

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

  @Override
  public void setValue(E e) {
    setter.accept(e);
  }

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

}
