package de.ivu.elect.presentation.system;

import de.ivu.elect.business.konfiguration.entity.ElectKonfigurationWerte;
import de.ivu.elect.business.konfiguration.entity.KonfigurationNewTransaktionLoader;
import de.ivu.elect.business.system.boundary.SystemLogWriter;
import de.ivu.elect.business.system.entity.HttpSessionTracking;
import de.ivu.elect.business.system.entity.SystemLogQuelle;
import de.ivu.elect.jee.presentation.menu.SessionIdConfigurationStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.SessionCookieConfig;
import javax.servlet.annotation.WebListener;

/*http://www.logicbig.com/tutorials/java-ee-tutorial/java-servlet/session-tracking-mode/*/
@WebListener
public class ElectContextListener implements ServletContextListener {

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

  @Inject
  private transient SystemLogWriter systemLogWriter;

  @Inject
  private transient KonfigurationNewTransaktionLoader konfigurationLoader;

  @Override
  public void contextInitialized(ServletContextEvent event) {
    setzeCookieConfig(event);
    setzeSessionTracking(event);
  }

  private void setzeCookieConfig(ServletContextEvent event) {
    LOG.info("Setze Cookie Config");

    try {
      boolean httpOnly = konfigurationLoader.resolveBooleanValue(ElectKonfigurationWerte.HTTP_SESSION_COOKIE_CONFIG_HTTP_ONLY);
      boolean secure = konfigurationLoader.resolveBooleanValue(ElectKonfigurationWerte.HTTP_SESSION_COOKIE_CONFIG_SECURE);
      LOG.info("Setze SessionCookieConfig: httpOnly=" + httpOnly + ", secure=" + secure);
      SessionCookieConfig sessionCookieConfig = event.getServletContext().getSessionCookieConfig();
      sessionCookieConfig.setHttpOnly(httpOnly);
      sessionCookieConfig.setSecure(secure);
      SessionIdConfigurationStore.SECURE.set(secure);
    } catch (Exception e) {
      systemLogWriter.writeErrorLogMessageFuerSystemMandant("Fehler beim Setzen der Cooki Config", e, SystemLogQuelle.SYSTEM);
    }
    LOG.info("Setze Cookie Config - done");

  }

  private void setzeSessionTracking(ServletContextEvent event) {
    LOG.info("Setze session tracking");
    try {
      HttpSessionTracking httpSessionTracking = konfigurationLoader
          .resolveEnumValue(ElectKonfigurationWerte.HTTP_SESSION_TRACKING_MODE, HttpSessionTracking.class);
      LOG.info("Setze session tracking: " + httpSessionTracking);
      if (httpSessionTracking.isStorageAnpassen()) {
        LOG.info("Setze session tracking:  tracking modes: " + httpSessionTracking.getSessionTrackingModes());
        event.getServletContext()
            .setSessionTrackingModes(httpSessionTracking.getSessionTrackingModes());
      }
    } catch (Exception e) {
      systemLogWriter.writeErrorLogMessageFuerSystemMandant("Fehler beim Setzen des session tracking", e, SystemLogQuelle.SYSTEM);
    }
    LOG.info("Setze session tracking - done");
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
  }
}