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

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.email.boundary.AbstractEmailVorgangSearchParameters;
import de.ivu.elect.business.email.boundary.EmailVorgangChangeEvent;
import de.ivu.elect.business.email.boundary.EmailVorgangConversationStore;
import de.ivu.elect.business.email.boundary.EmailVorgangExtendedDAO;
import de.ivu.elect.business.email.boundary.EmailVorgangSearch;
import de.ivu.elect.business.email.boundary.EmailVorgangSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.email.entity.EmailVorgang;
import de.ivu.elect.business.email.entity.EmailVorgangMeta;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;

@ViewAccessScoped
@Named(AbstractEmailVorgangSearchView.COMPONENT_NAME)
@View(viewId = AbstractEmailVorgangSearchView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORGANG_SEARCH)
public abstract class AbstractEmailVorgangSearchView extends AbstractElectSearchView<EmailVorgang> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "emailVorgangSearchView";
  
  public static final String VIEW_ID = "/email/email-vorgang-search.xhtml";

  @Inject
  private transient RequestHandlerAbstractEmailVorgangSearchView requestHandler;

  @Inject
  private transient EmailVorgangExtendedDAO extendedDAO;

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

  @Inject
  protected transient EmailVorgangSearch search;

  @Inject
  @ConversationGroup(AbstractEmailVorgangSearchParameters.class)
  protected transient EmailVorgangSearchParameters searchParameters;

  @Inject
  protected transient EmailVorgangMeta entityMeta;

  public AbstractEmailVorgangSearchView() {
    super();
  }

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

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

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

    return null;
  }

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

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

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

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

    return super.getSearchResult();
  }

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

  public void onEmailVorgangChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) EmailVorgangChangeEvent 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 RequestHandlerAbstractEmailVorgangSearchView {

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

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

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