
package de.ivu.elect.presentation.email;

import de.ivu.elect.business.email.boundary.EmailVorgangEdit;
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.jee.background.NotifyOnSuccess;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.View;

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

@Named(AbstractEmailEditView.COMPONENT_NAME)
@View(viewId = AbstractEmailEditNoScopeView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORGANG_EDIT)
public class EmailEditView extends AbstractEmailEditView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient EmailVorgangEdit emailVorgangEdit;

  public EmailEditView() {
    super();
  }

  @Override
  protected boolean isGebietBearbeitenRechtPruefen() {
    return false;
  }

  @Override
  public void addProperties(FormBuilder formBuilder, Email email) {
    // add properties here
  }

  @NotifyOnSuccess(EmailSendenEvent.class)
  @Redirect
  @UseCaseSecure(UseCasePermission.EMAIL_VORGANG_CREATE)
  public String sendeEmailErneut() {
    Email email = getEntity();
    email.setStatus(EmailStatusType.ZU_VERSENDEN);
    email.setFehlermeldung(null);
    edit.save(email);

    EmailVorgang emailVorgang = email.getEmailVorgang();
    emailVorgang.setStatus(EmailVorgangStatusType.WIRD_VERSENDET);
    emailVorgangEdit.save(emailVorgang);

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

    resetStateAfterAction();
    return getParentViewId(); // definierter Parent, benutze deleteAndGoBack um zum vorherigen View zurückzukehren
  }

  @Override
  @Redirect
  @UseCaseSecure(UseCasePermission.EMAIL_VORGANG_EDIT)
  public String save() {
    return super.save();
  }

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.EMAIL_VORGANG_EDIT);
  }

  @Override
  @Redirect
  @UseCaseSecure(UseCasePermission.EMAIL_VORGANG_DELETE)
  public String delete() {
    return super.delete();
  }

  @Override
  @Redirect
  @UseCaseSecure(UseCasePermission.EMAIL_VORGANG_DELETE)
  public String deleteAndGoBack() {
    return super.deleteAndGoBack();
  }

  @Override
  public boolean isBtnDeleteRendered() {
    return hasPermission(UseCasePermission.EMAIL_VORGANG_DELETE);
  }
}
