/*
* 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.AbstractEmailVorlageSearchParameters;
import de.ivu.elect.business.email.boundary.EmailVorlageChangeEvent;
import de.ivu.elect.business.email.boundary.EmailVorlageConversationStore;
import de.ivu.elect.business.email.boundary.EmailVorlageExtendedDAO;
import de.ivu.elect.business.email.boundary.EmailVorlageSearch;
import de.ivu.elect.business.email.boundary.EmailVorlageSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.email.entity.EmailVorlage;
import de.ivu.elect.business.email.entity.EmailVorlageMeta;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;

@ViewAccessScoped
@Named(AbstractEmailVorlageSearchView.COMPONENT_NAME)
@View(viewId = AbstractEmailVorlageSearchView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORLAGE_SEARCH)
public abstract class AbstractEmailVorlageSearchView extends AbstractElectSearchView<EmailVorlage> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "emailVorlageSearchView";
  
  public static final String VIEW_ID = "/email/email-vorlage-search.xhtml";

  @Inject
  private transient RequestHandlerAbstractEmailVorlageSearchView requestHandler;

  @Inject
  private transient EmailVorlageExtendedDAO extendedDAO;

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

  @Inject
  protected transient EmailVorlageSearch search;

  @Inject
  @ConversationGroup(AbstractEmailVorlageSearchParameters.class)
  protected transient EmailVorlageSearchParameters searchParameters;

  @Inject
  protected transient EmailVorlageMeta entityMeta;

  public AbstractEmailVorlageSearchView() {
    super();
  }

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

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

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

    return null;
  }

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

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

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

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

    return super.getSearchResult();
  }

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

  public void onEmailVorlageChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) EmailVorlageChangeEvent 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 RequestHandlerAbstractEmailVorlageSearchView {

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

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

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