package de.ivu.elect.presentation.email;

import de.ivu.elect.business.email.entity.EmailVorlage;
import de.ivu.elect.business.wahl.boundary.WahlterminNichtAusgewaehltException;
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;

import javax.inject.Inject;
import javax.inject.Named;

@Named(AbstractEmailVorlageSearchView.COMPONENT_NAME)
@OnlineHilfeId("736")
public class EmailVorlageSearchView extends AbstractEmailVorlageSearchView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient EmailLayoutUiVisibility emailLayoutUiVisibility;

  private Boolean emailLayoutsVisible = null;

  public EmailVorlageSearchView() {
    super();
  }

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

  @Override
  protected AbstractGebietSecurityFilter<EmailVorlage> 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().getBezeichnungProperty()).withTooltip(wildcardTooltip).withRequired(false);
    if (isEmailLayoutSearchParameterVisible()) {
      formBuilder.addProperty(getSearchParameters().getEmailLayoutProperty()).withRequired(false);
    }
  }

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