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

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.wahl.boundary.AbstractWahlSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlConversationStore;
import de.ivu.elect.business.wahl.boundary.WahlExtendedDAO;
import de.ivu.elect.business.wahl.boundary.WahlSearch;
import de.ivu.elect.business.wahl.boundary.WahlSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.business.wahl.entity.WahlMeta;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;

@ViewAccessScoped
@Named(AbstractWahlSearchView.COMPONENT_NAME)
@View(viewId = AbstractWahlSearchView.VIEW_ID, useCase = UseCasePermission.WAHL_SEARCH)
public abstract class AbstractWahlSearchView extends AbstractElectSearchView<Wahl> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wahlSearchView";
  
  public static final String VIEW_ID = "/wahl/wahl-search.xhtml";

  @Inject
  private transient RequestHandlerAbstractWahlSearchView requestHandler;

  @Inject
  private transient WahlExtendedDAO extendedDAO;

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

  @Inject
  protected transient WahlSearch search;

  @Inject
  @ConversationGroup(AbstractWahlSearchParameters.class)
  protected transient WahlSearchParameters searchParameters;

  @Inject
  protected transient WahlMeta entityMeta;

  public AbstractWahlSearchView() {
    super();
  }

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

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

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

    return null;
  }

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

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

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

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

    return super.getSearchResult();
  }

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

  public void onWahlChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WahlChangeEvent 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
  // elect: Das Suchergebnis soll nur einmal pro Request geladen werden, daher gibt es dieses "Konstrukt" aus "SearchView" und "RequestHandlerAbstract"
  public static class RequestHandlerAbstractWahlSearchView {

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

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

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