
package de.ivu.elect.presentation.wahl;

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

import javax.inject.Named;

import de.ivu.elect.business.wahl.boundary.WahlterminSearchParameters;
import de.ivu.elect.business.wahl.entity.WahlConstants;
import de.ivu.elect.business.wahl.entity.Wahltermin;
import de.ivu.elect.business.wahl.entity.WahlterminStatus;
import de.ivu.elect.jee.presentation.dynamicform.AbstractSelectManyProperty;
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;

@OnlineHilfeId("440")
@Named(AbstractWahlterminSearchView.COMPONENT_NAME)
public class WahlterminSearchView extends AbstractWahlterminSearchView {

  private static final long serialVersionUID = 1L;

  public WahlterminSearchView() {
    super();
  }

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

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

  @Override
  public int getDefaultPageSize() {
    return getPageSizeShowAll();
  }

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

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

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