package de.ivu.elect.presentation.email;

import de.ivu.elect.business.email.boundary.*;
import de.ivu.elect.business.email.control.EmailSendenEvent;
import de.ivu.elect.business.email.entity.Email;
import de.ivu.elect.business.email.entity.EmailStatusType;
import de.ivu.elect.business.email.entity.EmailVorgang;
import de.ivu.elect.business.email.entity.EmailVorgangStatusType;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCaseSecure;
import de.ivu.elect.business.wahl.boundary.WahlterminNichtAusgewaehltException;
import de.ivu.elect.jee.background.NotifyOnSuccess;
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.jee.presentation.search.AbstractSearchParameters;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;
import org.apache.deltaspike.core.api.scope.ConversationGroup;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

@Named(AbstractEmailSearchView.COMPONENT_NAME)
@View(viewId = AbstractEmailSearchView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORGANG_SEARCH)
@OnlineHilfeId(value = "743", mehrfachVergebenOk = true)
public class EmailSearchView extends AbstractEmailSearchView {

  private static final long serialVersionUID = 1L;

  @Inject
  @ConversationGroup(EmailVorgangConversationStore.class)
  private transient EmailVorgangConversationStore emailVorgangConversationStore;

  @Inject
  private transient EmailVorgangConversationSelect emailVorgangConversationSelect;

  @Inject
  private transient EmailEdit emailEdit;

  @Inject
  private transient EmailVorgangEdit emailVorgangEdit;

  @Inject
  private transient EmailExtendedDAO emailExtendedDAO;

  public EmailSearchView() {
    super();
  }

  @PreConditionCheck
  public String checkPreCondition() {
    if (!emailVorgangConversationSelect.isSelected()) {
      throw new EmailVorgangNichtAusgewaehltException();
    }
    return null;
  }

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

  @Redirect
  public String initWithEmailVorgang(EmailVorgang emailVorgang) {
    emailVorgangConversationSelect.setEntity(emailVorgang);
    return getViewId();
  }

  @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()).withLabel(getMessage("email.column.empfaenger"))
        .withTooltip(wildcardTooltip).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getFamiliennameProperty()).withTooltip(wildcardTooltip).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getVornameProperty()).withTooltip(wildcardTooltip).withRequired(false);
    formBuilder.addProperty(getSearchParameters().getFehlermeldungProperty()).withTooltip(wildcardTooltip).withRequired(false);
  }

  @Override
  @Nullable
  @Redirect
  public String goBack() {
    emailVorgangConversationStore.close();
    return super.goBack();
  }

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

  public String getTitle() {
    String betreff = "";
    if (emailVorgangConversationSelect.isSelected()) {
      betreff = getEmailVorgangSelected().getBetreff();
    }
    return getMessage("email.search.page.title", betreff);
  }

  protected EmailVorgang getEmailVorgangSelected() {
    return emailVorgangConversationSelect.getEntity();
  }

  public boolean isBtnSendeEmailsErneutRendered() {
    return true;
  }

  public boolean isBtnSendeEmailsErneutDisabled() {
    return getSearchResult() == null || getSearchResult().isEmpty();
  }

  @NotifyOnSuccess(EmailSendenEvent.class)
  @Redirect
  @UseCaseSecure(UseCasePermission.EMAIL_VORGANG_CREATE)
  public String sendeEmailsErneut(List<Email> emails) {
    if (emails == null) {
      return getViewId();
    }

    emailEdit.sendeEmailsErneut(emails);

    EmailVorgang emailVorgang = getEmailVorgangSelected();
    if (emailVorgang != null) {
      emailVorgang.setStatus(EmailVorgangStatusType.WIRD_VERSENDET);
      emailVorgangEdit.save(emailVorgang);
    }

    addInfoMessage("email.edit.action.sende.emails.erneut.success.message", getMessage("EmailStatusType.ZU_VERSENDEN"));

    return getViewId();
  }
}
