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

import javax.ejb.Stateless;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.CurrentCustomer;
import de.ivu.elect.business.benutzerverwaltung.control.RechteKonfigurationAktualisierenEvent;
import de.ivu.elect.business.konfiguration.control.SystemKonfigurationInit;
import de.ivu.elect.business.konfiguration.entity.ElectKonfigurationWerte;
import de.ivu.elect.business.konfiguration.entity.Konfiguration;
import de.ivu.elect.business.konfiguration.entity.KonfigurationLoader;
import de.ivu.elect.business.mandant.boundary.MandantDAO;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.system.boundary.SystemStatusStore;
import de.ivu.elect.business.system.entity.SystemStatus;
import de.ivu.elect.business.version.boundary.AbstractVersion;


import static de.ivu.elect.business.konfiguration.entity.MandantKonfigurationWerte.SYSTEM_STATUS;

@Stateless
public class SystemStatusInit {

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

  @Inject
  private transient KonfigurationLoader konfigurationLoader;

  @Inject
  private transient SystemStatusStore systemStatusStore;

  @Inject
  private transient MandantDAO mandantDAO;

  @Inject
  private transient RechteKonfigurationAktualisierenEvent rechteKonfigurationAktualisierenEvent;

  @Inject
  private transient CurrentCustomer currentCustomer;

  @Inject
  private transient AbstractVersion abstractVersion;

  public SystemStatusInit() {
    super();
  }

  public void initAlleMandanten() {
    for (Mandant mandant : mandantDAO.findByAll()) {
      init(mandant);
    }
    rechteKonfigurationAktualisierenEvent.sendChangeEvent();
  }

  public void init(Mandant mandant) {
    SystemStatus systemStatus = ermittleSystemStatus(mandant);
    LOG.info("Mandant: " + mandant.getNummer() + " - Systemstatus ermittelt: " + systemStatus);
    systemStatusStore.setSystemStatus(mandant.getPk(), systemStatus);
  }

  private SystemStatus ermittleSystemStatus(Mandant mandant) {
    SystemStatus systemStatus = konfigurationLoader.resolveEnumValue(mandant, SYSTEM_STATUS, SystemStatus.class);

    LOG.info("Systemstatus laut DB: " + systemStatus);

    return systemStatus;
  }

  public void checkCustomer() {
    String savedCustomer = konfigurationLoader.resolveStringValue(ElectKonfigurationWerte.CUSTOMER);
    if (StringUtils.isBlank(savedCustomer)) {
      LOG.info("Erster Start, Customer nicht gesetzt");
    } else {
      if (currentCustomer.getCustomer().name().equalsIgnoreCase(savedCustomer)) {
        LOG.info("Customer OK: " + savedCustomer);
      } else {
        throw new IllegalStateException("Customer falsch: '" + savedCustomer + "', " + currentCustomer.getCustomer());
      }
    }

  }

  public void checkVersion() {
    Konfiguration version = konfigurationLoader.getParameter(ElectKonfigurationWerte.VERSION);
    if (version == null) {
      LOG.info("Erster Start, Version nicht gesetzt");
    } else {
      String actual = SystemKonfigurationInit.getVersionString(abstractVersion);
      if (actual.equalsIgnoreCase(version.getWert())) {
        LOG.info("Version unverändert: " + actual);
      } else {
        LOG.info("Version Update: " + version.getWert() + " => " + actual);
        version.setWert(actual);
      }
    }
  }
}
