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

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import de.ivu.elect.business.CurrentCustomer;
import de.ivu.elect.business.konfiguration.boundary.CustomParameterSearchParameters;
import de.ivu.elect.business.konfiguration.entity.CustomParameterKategorie;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.LambdaBooleanProperty;
import de.ivu.elect.jee.presentation.dynamicform.LambdaEnumProperty;
import de.ivu.elect.presentation.ViewFormBuilder;


import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;

public class CustomParameterSearchFormBuilder extends ViewFormBuilder {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient CurrentCustomer currentCustomer;

  public CustomParameterSearchFormBuilder() {
    super();
  }

  public void addProperties(FormBuilder formBuilder, CustomParameterSearchParameters searchParameters) {
    Set<CustomParameterKategorie> templatesKategorien = new HashSet<>();

    searchParameters.getSupportedTemplates().stream().filter(template -> template.hasCustomerOrParent(currentCustomer.getCustomer()))
        .forEach(template -> templatesKategorien.add(template.getKategorie()));

    List<CustomParameterKategorie> kategorien = stream(CustomParameterKategorie.values()) //
        .filter(templatesKategorien::contains) //
      .sorted(Comparator.comparing(Enum::name)) //
        .collect(toList());

    formBuilder.addProperty(
        new LambdaEnumProperty<>(getMessage("custom.parameter.kategorie"), false, searchParameters::setKategorie, searchParameters::getKategorie, CustomParameterKategorie.class, kategorien, true));

    formBuilder.addProperty(
      new LambdaBooleanProperty(getMessage("custom.parameter.nur.ungueltige"), false, searchParameters::setNurUngueltige, searchParameters::isNurUngueltige));

  }
}
