package de.ivu.elect.presentation.mandant;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import javax.el.MethodExpression;
import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.mandant.boundary.MandantChangeEvent;
import de.ivu.elect.business.mandant.boundary.MandantReset;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.mandant.entity.MandantPK;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCaseSecure;
import de.ivu.elect.business.system.boundary.SystemLogChangeEvent;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.mandant.form.MandantViewFormBuilder;

@Named(AbstractMandantEditView.COMPONENT_NAME)
@OnlineHilfeId(value = "779", mehrfachVergebenOk = true)
public class MandantEditView extends AbstractMandantEditView {

  private static final long serialVersionUID = 1L;

  @Inject
  private MandantViewFormBuilder viewFormBuilder;

  @Inject
  private transient MandantChangeEvent changeEvent;


  @Inject
  private transient MandantReset mandantReset;

  public MandantEditView() {
    super();
  }

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

  @Override
  public void addProperties(FormBuilder formBuilder, Mandant mandant) {
    viewFormBuilder.addProperties(formBuilder, mandant);
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.MANDANT_RESET)
  public String resetMandant() {

    Mandant entity = getEntity();
    if (entity != null) {
      mandantReset.resetMandant(entity.getPk());
      addInfoMessage("entity.edit.action.reset.success.message", entityMeta.getEinzahlMessage());

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

  @Redirect
  @UseCaseSecure(UseCasePermission.MANDANT_RESET)
  public String resetSearched(Collection<Mandant> mandanten) {
    mandantReset.resetMandant(mandanten.stream().filter(m->m.getArt().isResetMoeglich()).map(Mandant::getPk).collect(Collectors.toList()));
    addInfoMessage("entity.edit.action.reset.success.message", entityMeta.getMehrzahlMessage());
    changeEvent.sendChangeEvent();
    resetStateAfterAction();
    return getParentViewId(); // definierter Parent, benutze deleteAndGoBack um zum vorherigen View zurückzukehren
  }
}
