
package de.ivu.elect.presentation.email;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;

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

import de.ivu.elect.business.email.boundary.EmailEdit;
import de.ivu.elect.business.email.boundary.EmailVorgangConversationSelect;
import de.ivu.elect.business.email.boundary.EmailVorgangConversationStore;
import de.ivu.elect.business.email.boundary.EmailVorgangEdit;
import de.ivu.elect.business.email.boundary.EmailVorgangNichtAusgewaehltException;
import de.ivu.elect.business.email.control.EmailSendenEvent;
import de.ivu.elect.business.email.entity.Email;
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.jee.background.NotifyOnSuccess;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
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.email.table.EmailColumnBuilder;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;

@OnlineHilfeId(value = "743", mehrfachVergebenOk = true)
@ViewAccessScoped
@Named(AbstractEmailSearchView.COMPONENT_NAME)
@View(viewId = AbstractEmailSearchView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORGANG_SEARCH) // UseCasePermission#EMAIL_VORGANG_SEARCH statt UseCasePermission#EMAIL_SEARCH!!!
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 EmailColumnBuilder emailColumnBuilder;

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

  @Redirect
  @UseCaseSecure(UseCasePermission.EMAIL_VORGANG_CREATE)
  @NotifyOnSuccess(EmailSendenEvent.class)
  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();
  }

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

    return eagerLoadingCollector;
  }
}
