
package de.ivu.elect.presentation.wahl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.wahl.boundary.WahlSearchParameters;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.business.wahl.entity.WahlConstants;
import de.ivu.elect.business.wahl.entity.WahlStatus;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.LambdaEnumSelectManyProperty;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;
import de.ivu.elect.presentation.wahl.table.WahlColumnBuilder;

@OnlineHilfeId("403")
@Named(AbstractWahlSearchView.COMPONENT_NAME)
public class WahlSearchView extends AbstractWahlSearchView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient WahlColumnBuilder wahlColumnBuilder;

  public WahlSearchView() {
    super();
  }

  @Override
  protected AbstractGebietSecurityFilter<Wahl> getGebietSecurityFilter() {
    return KEIN_GEBIET_SECURITY_FILTER;
  }

  @Override
  public void presetSearchParameters() {
    // Do nothing.
  }

  @Override
  protected List<Wahl> performSearch() {
    return super.performSearch().stream().filter(w -> w.isRechtZumEinsehenVorhanden(getPrincipal())).collect(Collectors.toList());
  }

  @Override
  public WahlSearchParameters getSearchParameters() {
    WahlSearchParameters searchParameters = super.getSearchParameters();
    searchParameters.setMandant(getMandant());
    return searchParameters;
  }

  @Override
  public void addProperties(FormBuilder formBuilder) {
    formBuilder.addProperty(new LambdaEnumSelectManyProperty<>(LambdaEnumSelectManyProperty.CHECKBOX_MENU, getMessage(WahlConstants.MESSAGE_KEY_STATUS_EINZAHL),
      false,
      getSearchParameters()::setWahlStatus, getSearchParameters()::getWahlStatus, WahlStatus.class, Arrays.asList(WahlStatus.values()), true));
  }

  @Override
  public EagerLoadingCollector<Wahl> getEagerLoadingCollector() {
    EagerLoadingCollector<Wahl> eagerLoadingCollector = super.getEagerLoadingCollector();
    wahlColumnBuilder.collectEagerLoadings(eagerLoadingCollector);

    return eagerLoadingCollector;
  }

}
