package de.ivu.elect.business.mandant.control;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;

import de.ivu.elect.business.benutzerverwaltung.control.RechteKonfigurationAktualisierenEvent;
import de.ivu.elect.business.mandant.boundary.MandantCreateEvent;
import de.ivu.elect.business.mandant.boundary.MandantPostCreateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.gebietsbaum.boundary.GebietsartCreator;
import de.ivu.elect.business.konfiguration.control.KonfigurationCreator;
import de.ivu.elect.business.mandant.boundary.MandantDAO;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.security.boundary.BenutzerCreator;
import de.ivu.elect.business.security.boundary.RolleCreator;
import de.ivu.elect.business.system.control.SystemStatusCheckPostStartupAsync;
import de.ivu.elect.business.system.control.SystemStatusInit;
import de.ivu.elect.business.wahl.boundary.WahlartCreator;

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class MandantPostCreateActions {

  private static final Logger LOG = LoggerFactory.getLogger(MandantPostCreateActions.class);

  @Inject
  protected transient MandantDAO mandantDAO;

  @Inject
  private transient KonfigurationCreator konfigurationCreator;

  @Inject
  private transient RolleCreator rolleCreator;

  @Inject
  private transient GebietsartCreator gebietsartCreator;

  @Inject
  private transient WahlartCreator wahlartCreator;

  @Inject
  private transient BenutzerCreator benutzerCreator;

  @Inject
  private transient SystemStatusInit systemStatusInit;

  @Inject
  transient Event<MandantPostCreateEvent> postCreateEventEvent;

  @Inject
  private transient RechteKonfigurationAktualisierenEvent rechteKonfigurationAktualisierenEvent;

  public void initMandant(Mandant mandantNotAttached) {
    LOG.info("Initialisiere Mandant: " +mandantNotAttached);
    Mandant mandant = mandantDAO.attach(mandantNotAttached);
    init(mandant);
    postInit(mandant);
    LOG.info("Initialisiere Mandant: " +mandantNotAttached +" - done");
  }

  protected void init(Mandant mandant) {
    konfigurationCreator.initKonfiguration(mandant);
    mandantDAO.flush();
    rolleCreator.initRollen(mandant);
    mandantDAO.flush();
    rolleCreator.initProduktivTestRechte(mandant);
    mandantDAO.flush();
    rolleCreator.aktualisiereAdminRolle(mandant);
    mandantDAO.flush();
    benutzerCreator.initBenutzer(mandant);
    mandantDAO.flush();
    gebietsartCreator.initGebietsarten(mandant);
    mandantDAO.flush();
    wahlartCreator.initWahlarten(mandant);
    mandantDAO.flush();
    systemStatusInit.init(mandant);
    mandantDAO.flush();
    // Konfigurierte Rechte laden
    rechteKonfigurationAktualisierenEvent.sendChangeEvent();
  }

  protected void postInit(Mandant mandant) {
    postCreateEventEvent.fire(new MandantPostCreateEvent(mandant));
  }

}
