
package de.ivu.elect.presentation.konfiguration.form;

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

import de.ivu.elect.business.konfiguration.entity.CustomParameter;
import de.ivu.elect.business.konfiguration.entity.CustomParameterTemplate;
import de.ivu.elect.jee.presentation.dynamicform.AbstractSelectOneMenuProperty;

public abstract class AbstractCustomParameterSelectOneMenuProperty<T> extends AbstractSelectOneMenuProperty<T> {

  private final CustomParameter< ? > parameter;

  private final CustomParameterTemplate template;

  private final List<T> values;

  public AbstractCustomParameterSelectOneMenuProperty(CustomParameter parameter, Function<CustomParameterTemplate, Supplier<List<T>>> wertValuesFct) {
    super();

    this.parameter = parameter;
    this.template = requireCustomParameterTemplate(parameter);
    this.values = wertValuesFct.apply(this.template).get();
  }

  private CustomParameterTemplate requireCustomParameterTemplate(CustomParameter parameter) {
    Optional< ? > templateOptional = parameter.getTemplate();

    if (!templateOptional.isPresent()) {
      throw new IllegalStateException("Kein Template gefunden: " + parameter);
    }

    return (CustomParameterTemplate) templateOptional.get();
  }

  protected CustomParameter< ? > getParameter() {
    return parameter;
  }

  protected CustomParameterTemplate getTemplate() {
    return template;
  }

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

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

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

  @Override
  public List<T> getValues() {
    return values;
  }
}
