/*
* Template: src/main/takatuka/templates/EntityPresentationAbstractSearchView.vm
*/
package de.ivu.elect.presentation.kandidatur;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.enterprise.inject.Instance;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.core.api.scope.ConversationGroup;
import org.apache.deltaspike.core.api.scope.ViewAccessScoped;

import de.ivu.elect.business.security.UseCasePermission;

import de.ivu.elect.business.kandidatur.boundary.AbstractKandidaturSearchParameters;
import de.ivu.elect.business.kandidatur.boundary.KandidaturChangeEvent;
import de.ivu.elect.business.kandidatur.boundary.KandidaturConversationStore;
import de.ivu.elect.business.kandidatur.boundary.KandidaturExtendedDAO;
import de.ivu.elect.business.kandidatur.boundary.KandidaturSearch;
import de.ivu.elect.business.kandidatur.boundary.KandidaturSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.kandidatur.entity.Kandidatur;
import de.ivu.elect.business.kandidatur.entity.KandidaturMeta;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;

@ViewAccessScoped
@Named(AbstractKandidaturSearchView.COMPONENT_NAME)
@View(viewId = AbstractKandidaturSearchView.VIEW_ID, useCase = UseCasePermission.KANDIDATUR_SEARCH)
public abstract class AbstractKandidaturSearchView extends AbstractElectSearchView<Kandidatur> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "kandidaturSearchView";
  
  public static final String VIEW_ID = "/kandidatur/kandidatur-search.xhtml";

  @Inject
  private transient RequestHandlerAbstractKandidaturSearchView requestHandler;

  @Inject
  private transient KandidaturExtendedDAO extendedDAO;

  @Inject
  @ConversationGroup(KandidaturConversationStore.class)
  protected transient KandidaturConversationStore conversationStore;

  @Inject
  protected transient KandidaturSearch search;

  @Inject
  @ConversationGroup(AbstractKandidaturSearchParameters.class)
  protected transient KandidaturSearchParameters searchParameters;

  @Inject
  protected transient KandidaturMeta entityMeta;

  public AbstractKandidaturSearchView() {
    super();
  }

  @Override
  protected String getViewId() {
    return VIEW_ID;
  }

  public boolean isEntitySelected() {
    return conversationStore.getEntityPK() != null;
  }

  public Kandidatur getSelectedEntity() {
    if (isEntitySelected()) {
      return extendedDAO.findByPrimaryKey(conversationStore.getEntityPK());
    }

    return null;
  }

  public void setSelectedEntity(Kandidatur entity) {
    if (entity == null) {
      conversationStore.setEntityPK(null);
    } else {
      conversationStore.setEntityPK(entity.getPk());
    }
  }

  @Override
  public KandidaturSearchParameters getSearchParameters() {
    return searchParameters;
  }

  public EagerLoadingCollector<Kandidatur> getEagerLoadingCollector() {
    return EagerLoadingCollector.<Kandidatur> create();
  }

  @Override
  public List<Kandidatur> getSearchResult() {
    requestHandler.nothing(); // ELECT-248

    return super.getSearchResult();
  }

  @Override
  protected List<Kandidatur> performSearch() {
    return search.getSearchResult(getSearchParameters(), getEagerLoadingCollector());
  }

  public void onKandidaturChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) KandidaturChangeEvent event) {
    resetSearchResult();
  }

  // ELECT-2231    ELECT-2250
  /*
   * searchParameters sind von der zur Bearbeitung ausgewählten Wahl abhängig.
   */
  public void onWahlSelectChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WahlSelectChangeEvent event) {
    reset();
  }

  /*
   * searchParameters sind vom zur Bearbeitung ausgewählten Wahltermin abhängig.
   */
  public void onWahlterminSelectChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WahlterminSelectChangeEvent event) {
    reset();
  }

  @RequestScoped
  //  Das Suchergebnis soll nur einmal pro Request geladen werden, daher gibt es dieses "Konstrukt" aus "SearchView" und "RequestHandlerAbstract"
  public static class RequestHandlerAbstractKandidaturSearchView {

    @View
    @Inject
    private transient Instance<AbstractKandidaturSearchView> searchViews;

    @PostConstruct
    public void init() {
      // ELECT-3543 Doppelte Suchen vermeiden - Suchergebnis für Request nicht beim POST-löschen
      if (!FacesContext.getCurrentInstance().isPostback()) {
        for (AbstractKandidaturSearchView searchView : searchViews) {
          searchView.resetSearchResult();
        }
      }
    }

    public void nothing() {
      // Do nothing.
    }
  }
}
