
package de.ivu.elect.presentation.internet;

import java.util.List;

import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
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.ViewAccessScoped;

import de.ivu.elect.business.internet.boundary.InternetWidgetConversationSelect;
import de.ivu.elect.business.internet.boundary.InternetWidgetFileSearch;
import de.ivu.elect.business.internet.entity.InternetWidget;
import de.ivu.elect.business.internet.entity.InternetWidgetFileEntity;
import de.ivu.elect.business.search.control.AbstractElectSearchParameters;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.faces.PreConditionCheck;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;

@ViewAccessScoped
@Named(InternetWidgetFileSearchView.COMPONENT_NAME)
@View(viewId = InternetWidgetFileSearchView.VIEW_ID, useCase = UseCasePermission.INTERNET_WIDGET_SEARCH)
@OnlineHilfeId(value = "750", mehrfachVergebenOk = true)
public class InternetWidgetFileSearchView extends AbstractElectSearchView<InternetWidgetFileEntity> {

  public static final String COMPONENT_NAME = "internetWidgetFileSearchView";

  public static final String VIEW_ID = "/internet/internet-widget-file-search.xhtml";

  public static final String PARENT_VIEW_ID = InternetWidgetSearchView.VIEW_ID;

  private static final long serialVersionUID = 1L;

  @Inject
  private transient InternetWidgetConversationSelect internetWidgetConversationSelect;

  @Inject
  private transient InternetWidgetFileSearch internetWidgetFileSearch;

  @Nullable
  @PreConditionCheck
  public String checkCondition() {
    if (internetWidgetConversationSelect.isSelected()) {
      return null;
    }

    return PARENT_VIEW_ID;
  }

  @Override
  protected List<InternetWidgetFileEntity> performSearch() {
    return internetWidgetFileSearch.getSearchResult(getInternetWidget());
  }

  private InternetWidget getInternetWidget() {
    return internetWidgetConversationSelect.getEntity();
  }

  @Override
  protected AbstractGebietSecurityFilter<InternetWidgetFileEntity> getGebietSecurityFilter() {
    return KEIN_GEBIET_SECURITY_FILTER;
  }

  @Override
  public void addProperties(FormBuilder formBuilder) {

  }

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

  @Override
  public AbstractElectSearchParameters getSearchParameters() {
    return null; // keine
  }

  @Override
  public void presetSearchParameters() {

  }

  public String getTitelInternetWidget() {
    InternetWidget internetWidget = getInternetWidget();
    if (internetWidget == null) {
      return null;
    }
    return getMessage(internetWidget.getWidgetTyp());
  }

  @Override
  protected void initSearchParametersIfNotInitialized() {
    // nichts zu tun
  }

  @Override
  public boolean isBtnGoBackRendered() {
    return true;
  }

  @Nullable
  @Override
  @Redirect
  public String goBack() {
    return PARENT_VIEW_ID;
  }

  @RequestScoped
  //  Das Suchergebnis soll nur einmal pro Request geladen werden, daher gibt es dieses "Konstrukt" aus "SearchView" und "RequestHandlerAbstract"
  public static class RequestHandlerInternetWidgetFileSearchView {

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

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

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