package de.ivu.elect.presentation.email;

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

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.wahl.boundary.WahlterminNichtAusgewaehltException;
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.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;

@Named(AbstractEmailVorgangSearchView.COMPONENT_NAME)
@OnlineHilfeId("733")
public class EmailVorgangSearchView extends AbstractEmailVorgangSearchView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient EmailLayoutUiVisibility emailLayoutUiVisibility;

  @Inject
  private transient KonfigurationExtendedLoader konfigurationExtendedLoader;

  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;
  }

  private boolean isMailServiceEnabled() {
    boolean mailServiceEnabled = false;
    try {
      mailServiceEnabled = konfigurationExtendedLoader.resolveBooleanValue(getMandant(), MandantKonfigurationWerte.MAIL_SERVICE_ENABLED);
    } catch (NoResultException e) {
      // do nothing
    }
    return mailServiceEnabled;
  }

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

  @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;
  }
}
