
package de.ivu.elect.business.wahl.boundary;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCaseSecure;
import de.ivu.elect.business.wahl.control.WahlterminByWahldatumComparator;
import de.ivu.elect.business.wahl.entity.Wahltermin;


import static de.ivu.elect.jee.entity.EntityUtil.normalizeDate;

public class WahlterminSearch extends AbstractWahlterminSearch {

  private static final Comparator< ? super Wahltermin> DEFAULT_COMPARATOR = WahlterminByWahldatumComparator.DESCENDING;

  public WahlterminSearch() {
    super();
  }

  @Override
  @UseCaseSecure(UseCasePermission.WAHLTERMIN_SEARCH)
  protected List<Wahltermin> querySearchResult(WahlterminSearchParameters searchParameters) {
    List<Wahltermin> result = dao.findByAll(searchParameters.getMandant());

    // nicht als Pattern verwenden
    if (!searchParameters.getWahlterminStatus().isEmpty()) {
      result = result.stream().filter(w->searchParameters.getWahlterminStatus().contains(w.getStatus())).collect(Collectors.toList());
    }

    result.sort(getSearchResultComparator());

    return result;
  }

  protected Comparator< ? super Wahltermin> getSearchResultComparator() {
    return DEFAULT_COMPARATOR;
  }

  public static Date normalizeDatum(Date date) {
    return normalizeDate(date);
  }
}
