
package de.ivu.elect.presentation.email;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.NoResultException;

import org.apache.deltaspike.core.api.scope.ViewAccessScoped;

import de.ivu.elect.business.email.entity.EmailVorgang;
import de.ivu.elect.business.konfiguration.entity.KonfigurationExtendedLoader;
import de.ivu.elect.business.konfiguration.entity.MandantKonfigurationWerte;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.wahl.boundary.WahlterminNichtAusgewaehltException;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.jee.faces.FacesContextUtil;
import de.ivu.elect.jee.faces.PreConditionCheck;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.search.AbstractSearchParameters;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.email.table.EmailVorgangColumnBuilder;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;

@OnlineHilfeId("733")
@ViewAccessScoped
@Named(AbstractEmailVorgangSearchView.COMPONENT_NAME)
@View(viewId = AbstractEmailVorgangSearchView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORGANG_SEARCH)
public class EmailVorgangSearchView extends AbstractEmailVorgangSearchView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient KonfigurationExtendedLoader konfigurationExtendedLoader;

  @Inject
  private transient EmailLayoutUiVisibility emailLayoutUiVisibility;

  @Inject
  private transient EmailVorgangColumnBuilder emailVorgangColumnBuilder;

  private Boolean emailLayoutsVisible = null;

  public EmailVorgangSearchView() {
    super();
  }

  @PreConditionCheck
  public String checkPreCondition() {
    if (!isWahlterminSelected() && !isWahlSelected()) {
      throw new WahlterminNichtAusgewaehltException();
    }

    if (!isMailServiceEnabled() && FacesContextUtil.isPartialRequest()) { // NOTE: Test auf PartialRequest verhindert doppelte Warnung
      addWarningMessage("interfaces.email.test.status.deaktiviert");
    }

    return null;
  }

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

  private boolean isMailServiceEnabled() {
    boolean mailServiceEnabled = false;

    try {
      mailServiceEnabled = konfigurationExtendedLoader.resolveBooleanValue(getMandant(), MandantKonfigurationWerte.MAIL_SERVICE_ENABLED);
    } catch (NoResultException ignored) {
    }

    return mailServiceEnabled;
  }

  @Override
  public void presetSearchParameters() {
    // Do nothing...
  }

  @Override
  public void addProperties(FormBuilder formBuilder) {
    checkPreCondition();

    String wildcardTooltip = getMessage(AbstractSearchParameters.WILDCARD_TOOLTIP_MSG_KEY);

    formBuilder.addProperty(getSearchParameters().getStatusProperty()).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getEmpfaengerProperty()).withTooltip(wildcardTooltip).withLabel(getMessage("email.vorgang.search.empfaenger")).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getFamiliennameProperty()).withTooltip(wildcardTooltip).withLabel(getMessage("email.vorgang.search.familienname")).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getVornameProperty()).withTooltip(wildcardTooltip).withLabel(getMessage("email.vorgang.search.vorname")).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getBetreffProperty()).withTooltip(wildcardTooltip).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getTypProperty()).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getEmailVorlageProperty()).withLabel(getMessage("email.vorgang.column.email.vorlage")).withRequired(false);

    if (isEmailLayoutSearchParameterVisible()) {
      formBuilder.addProperty(getSearchParameters().getEmailLayoutProperty()).withLabel(getMessage("email.vorgang.column.email.layout")).withRequired(false);
    }

    formBuilder.addProperty(getSearchParameters().getBenutzerkennungProperty()).withLabel(getMessage("email.vorgang.column.benutzerkennung")).withTooltip(wildcardTooltip).withRequired(false);
  }

  private boolean isEmailLayoutSearchParameterVisible() {
    if (emailLayoutsVisible == null) {
      emailLayoutsVisible = emailLayoutUiVisibility.isEmailLayoutAuswahlVisible();
    }

    return emailLayoutsVisible;
  }

  @Override // ELECTKOM-1649
  public EagerLoadingCollector<EmailVorgang> getEagerLoadingCollector() {
    EagerLoadingCollector<EmailVorgang> eagerLoadingCollector = EagerLoadingCollector.create();
    emailVorgangColumnBuilder.collectEagerLoadings(eagerLoadingCollector);

    return eagerLoadingCollector;
  }
}
