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

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.wahlvorschlag.boundary.AbstractListeSearchParameters;
import de.ivu.elect.business.wahlvorschlag.boundary.ListeChangeEvent;
import de.ivu.elect.business.wahlvorschlag.boundary.ListeConversationStore;
import de.ivu.elect.business.wahlvorschlag.boundary.ListeExtendedDAO;
import de.ivu.elect.business.wahlvorschlag.boundary.ListeSearch;
import de.ivu.elect.business.wahlvorschlag.boundary.ListeSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.wahlvorschlag.entity.Liste;
import de.ivu.elect.business.wahlvorschlag.entity.ListeMeta;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;

@ViewAccessScoped
@Named(AbstractListeSearchView.COMPONENT_NAME)
@View(viewId = AbstractListeSearchView.VIEW_ID, useCase = UseCasePermission.LISTE_SEARCH)
public abstract class AbstractListeSearchView extends AbstractElectSearchView<Liste> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "listeSearchView";
  
  public static final String VIEW_ID = "/wahlvorschlag/liste-search.xhtml";

  @Inject
  private transient RequestHandlerAbstractListeSearchView requestHandler;

  @Inject
  private transient ListeExtendedDAO extendedDAO;

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

  @Inject
  protected transient ListeSearch search;

  @Inject
  @ConversationGroup(AbstractListeSearchParameters.class)
  protected transient ListeSearchParameters searchParameters;

  @Inject
  protected transient ListeMeta entityMeta;

  public AbstractListeSearchView() {
    super();
  }

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

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

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

    return null;
  }

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

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

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

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

    return super.getSearchResult();
  }

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

  public void onListeChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) ListeChangeEvent 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 RequestHandlerAbstractListeSearchView {

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

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

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