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

import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.enterprise.event.TransactionPhase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.konfiguration.entity.ElectKonfigurationWerte;
import de.ivu.elect.business.konfiguration.entity.KonfigurationWertGeaendertEvent;

public class BackgroundServiceRetryKonfigurationChangeListener {

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

  public void listen(@Observes(during = TransactionPhase.AFTER_COMPLETION, notifyObserver = Reception.ALWAYS) KonfigurationWertGeaendertEvent konfigurationWertGeaendertEvent) {
    if (ElectKonfigurationWerte.BACKGROUND_SERVICE_MAX_ERROR_COUNT.equals(konfigurationWertGeaendertEvent.getKey())) {
      try {
        if (konfigurationWertGeaendertEvent.getNeuerWert() != null) {
          long maxErrorCount = Long.parseLong(konfigurationWertGeaendertEvent.getNeuerWert());
          LOG.info("BackgroundServiceRetry: " + maxErrorCount);
          if (maxErrorCount > 0) {
            BackgroundServiceRetry.MAX_ERROR_COUNT.set(maxErrorCount);
          }
        }
      } catch (Exception e) {
        // ignore
      }
    }
  }
}