package org.folio.okapi.util;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.vertx.core.json.Json;
import io.vertx.ext.web.RoutingContext;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import static org.folio.okapi.common.HttpResponse.*;

/**
 * Helper class to mess with logging stuff. Normally we use
 * io.vertx.core.logging.Logger for all our logging. Behind the scenes, this
 * turns out to use log4j. For advanced stuff, we need to access log4j directly.
 * This class tries to hide such things from the rest of the system. There are
 * methods for getting and setting the root logging level, from a Level, a
 * String, or as a web service. Later we may want to add more fancy log level
 * control, for example for different loggers or systems.
 *
 * @author heikki
 */
public class LogHelper {

  private static final Logger l4jlogger = Logger.getLogger(LogHelper.class);

  /**
   * Parameter for the web service. At some point we may want to add more stuff
   * here, to control logging for each module, or something. For now this is
   * good enough.
   */
  public static class LogLevelInfo {

    String level;

    @JsonCreator
    public LogLevelInfo(@JsonProperty String level) {
      this.level = level;
    }

    public LogLevelInfo() {
      this.level = "";
    }

    public String getLevel() {
      return level;
    }

    public void setLevel(String level) {
      this.level = level;
    }

  }

  public String getRootLogLevel() {
    Level lev = l4jlogger.getParent().getEffectiveLevel();
    if (lev == null) {
      return "null";
    } else {
      return lev.toString();
    }
  }

  public void getRootLogLevel(RoutingContext ctx) {
    String lev = getRootLogLevel();
    LogLevelInfo li = new LogLevelInfo(lev);
    String rj = Json.encode(li);
    responseJson(ctx, 200).end(rj);
  }

  public void setRootLogLevel(Level l) {
    // This might stop working in log4j version 2. See
    // http://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2
    l4jlogger.getParent().setLevel(l);
  }

  public void setRootLogLevel(String name) {
    Level l = Level.toLevel(name);
    setRootLogLevel(l);
  }

  public void setRootLogLevel(RoutingContext ctx) {
    final LogLevelInfo inf = Json.decodeValue(ctx.getBodyAsString(),
            LogLevelInfo.class);
    if (inf == null || inf.getLevel() == null || inf.getLevel().isEmpty()) {
      responseError(ctx, 400, "Invalid id");
    } else {
      setRootLogLevel(inf.getLevel());
      responseJson(ctx, 200).end(Json.encode(inf));
    }
  }

}

@@ -135,80 +135,8 @@
 ty;%0A
-import io.vertx.core.json.Json;%0Aimport io.vertx.ext.web.RoutingContext;%0A
 impo
@@ -197,61 +197,8 @@
 ger;
-%0Aimport static org.folio.okapi.common.HttpResponse.*;
 %0A%0A/*
@@ -1373,17 +1373,16 @@
 ;%0A    %7D%0A
-%0A
   %7D%0A%0A  p
@@ -1477,20 +1477,23 @@
 ();%0A    
-if (
+return 
 lev == n
@@ -1499,285 +1499,33 @@
 null
-) %7B%0A      return %22null%22;%0A    %7D else %7B%0A      return lev.toString();%0A    %7D%0A  %7D%0A%0A  public void getRootLogLevel(RoutingContext ctx) %7B%0A    String lev = getRootLogLevel();%0A    LogLevelInfo li = new LogLevelInfo(lev);%0A    String rj = Json.encode(li);%0A    responseJson(ctx, 200).end(rj
+ ? %22null%22 : lev.toString(
 );%0A 
@@ -1876,397 +1876,6 @@
  %7D%0A%0A
-  public void setRootLogLevel(RoutingContext ctx) %7B%0A    final LogLevelInfo inf = Json.decodeValue(ctx.getBodyAsString(),%0A            LogLevelInfo.class);%0A    if (inf == null %7C%7C inf.getLevel() == null %7C%7C inf.getLevel().isEmpty()) %7B%0A      responseError(ctx, 400, %22Invalid id%22);%0A    %7D else %7B%0A      setRootLogLevel(inf.getLevel());%0A      responseJson(ctx, 200).end(Json.encode(inf));%0A    %7D%0A  %7D%0A%0A
 %7D%0A

Remove unused code from LogHelper