/*
* 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.AbstractWahlartSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlartChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlartConversationStore;
import de.ivu.elect.business.wahl.boundary.WahlartExtendedDAO;
import de.ivu.elect.business.wahl.boundary.WahlartSearch;
import de.ivu.elect.business.wahl.boundary.WahlartSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.wahl.entity.Wahlart;
import de.ivu.elect.business.wahl.entity.WahlartMeta;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;

@ViewAccessScoped
@Named(AbstractWahlartSearchView.COMPONENT_NAME)
@View(viewId = AbstractWahlartSearchView.VIEW_ID, useCase = UseCasePermission.WAHLART_SEARCH)
public abstract class AbstractWahlartSearchView extends AbstractElectSearchView<Wahlart> {

  private static final long serialVersionUID = 1L;

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

  @Inject
  private transient RequestHandlerAbstractWahlartSearchView requestHandler;

  @Inject
  private transient WahlartExtendedDAO extendedDAO;

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

  @Inject
  protected transient WahlartSearch search;

  @Inject
  @ConversationGroup(AbstractWahlartSearchParameters.class)
  protected transient WahlartSearchParameters searchParameters;

  @Inject
  protected transient WahlartMeta entityMeta;

  public AbstractWahlartSearchView() {
    super();
  }

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

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

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

    return null;
  }

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

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

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

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

    return super.getSearchResult();
  }

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

  public void onWahlartChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WahlartChangeEvent 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 RequestHandlerAbstractWahlartSearchView {

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

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

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