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

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

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

import de.ivu.elect.business.konfiguration.entity.Konfiguration;
import de.ivu.elect.business.konfiguration.entity.KonfigurationLoader;
import de.ivu.elect.business.konfiguration.entity.MandantKonfigurationWerte;
import de.ivu.elect.business.mandant.boundary.MandantDAO;
import de.ivu.elect.business.mandant.entity.AbstractMandant;
import de.ivu.elect.business.system.boundary.SystemLogWriter;
import de.ivu.elect.business.system.entity.SystemLogQuelle;
import de.ivu.elect.jee.faces.Messages;

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

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

  @Inject
  private transient KonfigurationLoader konfigurationLoader;

  @Inject
  private transient MandantDAO mandantDAO;

  @Inject
  private transient SystemLogWriter systemLogWriter;

  @Inject
  private transient Messages messages;



  public void handleAirGapFailed() {
    LOG.error("No air gap");
    systemLogWriter.writeWarnLogMessageFuerSystemMandant(messages.getMessageForMessageKey("nl.air.gap.check.failed"), SystemLogQuelle.SYSTEM);
    getSystemNachricht().setWert(messages.getMessageForMessageKey("nl.air.gap.check.failed.nachricht"));
  }

  private Konfiguration getSystemNachricht() {
    return konfigurationLoader.getParameter(AbstractMandant.getSystemMandant(mandantDAO), MandantKonfigurationWerte.SYSTEM_MESSAGE);
  }

  public void handleAirGapSuccess() {
    LOG.info("Air gap present");
    getSystemNachricht().setWert(null);
  }

}
