package de.ivu.elect.presentation.email;

import java.util.List;

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

import org.apache.deltaspike.core.api.scope.ConversationGroup;
import org.apache.deltaspike.core.api.scope.GroupedConversation;
import org.apache.deltaspike.core.api.scope.GroupedConversationScoped;

import de.ivu.elect.business.email.entity.EmailVorgangType;
import de.ivu.elect.business.person.boundary.PersonendatenListConversationStore;
import de.ivu.elect.business.person.entity.Personendaten;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCaseSecure;
import de.ivu.elect.jee.faces.HttpHeaderContentSecurityPolicy;
import de.ivu.elect.jee.faces.PreConditionCheck;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@GroupedConversationScoped
@ConversationGroup(EmailVorgangMassenEmailCreateView.class)
@Named(EmailVorgangMassenEmailCreateView.COMPONENT_NAME)
@View(viewId = EmailVorgangMassenEmailCreateView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORGANG_CREATE)
@HttpHeaderContentSecurityPolicy(policy="script-src", add="'unsafe-eval'")
@OnlineHilfeId(value = "742", mehrfachVergebenOk = true)
public class EmailVorgangMassenEmailCreateView extends AbstractEmailVorgangCreateView {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "emailVorgangMassenEmailCreateView";

  public static final String VIEW_ID = "/email/email-vorgang-massen-email-create.xhtml";

  @Inject
  private transient GroupedConversation conversation;

  @Inject
  @ConversationGroup(PersonendatenListConversationStore.class)
  private PersonendatenListConversationStore personendatenListConversationStore;

  private boolean showConfirmationDialog = false;

  public EmailVorgangMassenEmailCreateView() {
    super();
  }

  @Override
  protected String getViewId() {
    return VIEW_ID;
  }

  @Nullable
  @Override
  @PreConditionCheck
  public String checkPreCondition() {
    if (!getEntity().hasEmpfaengerPersonendatenMitEmailAdresse()) {
      addWarningMessage("email.vorgang.create.keine.empfaenger.adresse.error.message.mz");
    }
    return null;
  }

  @Override
  @PostConstruct
  public void init() {
    super.init();
    getEntity().setTyp(EmailVorgangType.MASSEN_EMAIL);
    getEntity().setEmpfaengerPersonendaten(personendatenListConversationStore.getEntities());
    ersetzePlatzhalterWennNurEinEmpfaenger();
  }

  @Redirect
  public String initWithPersonendaten(List<Personendaten> personendaten) {
    resetStateAfterAction();
    conversation.close(); // zunächst eine mögliche bestehende Conversation schließen, um immer mit den Default-Werten zu beginnen
    personendatenListConversationStore.setEntities(personendaten);
    return getViewId();
  }

  @Override
  protected void onClose() {
    personendatenListConversationStore.close();
    conversation.close();
  }

  @Override
  protected void resetStateAfterAction() {
    showConfirmationDialog = false;
    super.resetStateAfterAction();
  }

  @Override
  @UseCaseSecure(UseCasePermission.EMAIL_VORGANG_CREATE)
  public String save() {
    showConfirmationDialog = true; // noch nicht speichern, nur Bestätigungsdialog öffnen
    return getViewId();
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.EMAIL_VORGANG_CREATE)
  public String confirmSave() {
    return super.save();
  }

  public boolean isShowConfirmationDialog() {
    return showConfirmationDialog;
  }

  @Override
  public boolean isBtnSaveRendered() {
    return super.isBtnSaveRendered() && getEntity().hasEmpfaengerPersonendatenMitEmailAdresse();
  }

  @Override
  public boolean isBtnResetRendered() {
    return getEntity().hasEmpfaengerPersonendatenMitEmailAdresse();
  }
}
