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

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

public class LambdaEnumProperty<E extends Enum<E>> extends AbstractEnumProperty<E> {

  private final String defaultLabel;

  private final E defaultValue;

  private final boolean defaultRequired;

  private final Consumer<E> setter;

  private final Supplier<E> getter;

  private final Class<E> enumClass;

  private final List<E> values;

  private final boolean hasNoSelectionItem;

  public LambdaEnumProperty(String defaultLabel, boolean defaultRequired, Consumer<E> setter, Supplier<E> getter, Class<E> enumClass, List<E> values) {
    this(defaultLabel, defaultRequired, setter, getter, enumClass, values, false);
  }

  public LambdaEnumProperty(String defaultLabel, boolean defaultRequired, Consumer<E> setter, Supplier<E> getter, Class<E> enumClass, List<E> values, boolean hasNoSelectionItem) {
    this(defaultLabel, null, defaultRequired, setter, getter, enumClass, values, hasNoSelectionItem);
  }

  public LambdaEnumProperty(String defaultLabel, E defaultValue, boolean defaultRequired, Consumer<E> setter, Supplier<E> getter, Class<E> enumClass, List<E> values) {
    this(defaultLabel, defaultValue, defaultRequired, setter, getter, enumClass, values, false);
  }

  public LambdaEnumProperty(String defaultLabel, E defaultValue, boolean defaultRequired, Consumer<E> setter, Supplier<E> getter, Class<E> enumClass, List<E> values, boolean hasNoSelectionItem) {
    super();

    this.defaultLabel = defaultLabel;
    this.defaultValue = defaultValue;
    this.defaultRequired = defaultRequired;

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

    this.enumClass = enumClass;

    this.values = values;

    this.hasNoSelectionItem = hasNoSelectionItem;
  }

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

  @Override
  public E getDefaultValue() {
    return defaultValue;
  }

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

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

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

  @Override
  protected Class<E> getEnumClass() {
    return enumClass;
  }

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

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

}
