package io.github.ageofwar.telejam.loggers;

import io.github.ageofwar.telejam.Bot;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * Utility class for {@link Logger}.
 */
public final class Loggers {
  
  /**
   * Returns the default logger for the specified bot.
   *
   * @param bot the bot
   * @return the default logger
   */
  public static Logger newLogger(Bot bot) {
    Logger logger = Logger.getLogger(bot.getUsername());
    logger.setUseParentHandlers(false);
    ConsoleHandler handler = new ConsoleHandler();
    handler.setFormatter(new Formatter() {
      @Override
      public String format(LogRecord record) {
        String time = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS).toString();
        String level = record.getLevel().getLocalizedName();
        String text = formatMessage(record);
        String message = "[" + time + "][" + level + "] " + text + System.lineSeparator();
        Throwable t = record.getThrown();
        if (t != null) {
          StringWriter stringWriter = new StringWriter();
          PrintWriter writer = new PrintWriter(stringWriter);
          writer.println();
          t.printStackTrace(writer);
          message += stringWriter.toString();
        }
        return message;
      }
    });
    logger.addHandler(handler);
    return logger;
  }
  
  /**
   * Returns a logger for a bot that does nothing.
   *
   * @param bot the bot
   * @return an empty logger for the bot
   */
  public static Logger emptyLogger(Bot bot) {
    Logger logger = Logger.getLogger(bot.getUsername());
    logger.setUseParentHandlers(false);
    return logger;
  }
  
  /**
   * Logs an error.
   *
   * @param logger the logger
   * @param t      the error
   */
  public static void logError(Logger logger, Throwable t) {
    logger.warning(t.getLocalizedMessage());
    StackTraceElement[] stackTrace = t.getStackTrace();
    if (stackTrace.length > 0) {
      logger.throwing(stackTrace[0].getClassName(), stackTrace[0].getMethodName(), t);
    } else {
      logger.throwing("Unknown", "Unknown", t);
    }
  }
  
  private Loggers() {
    throw new AssertionError();
  }
  
}

@@ -42,48 +42,8 @@
 s;%0A%0A
-import io.github.ageofwar.telejam.Bot;%0A%0A
 impo
@@ -194,127 +194,9 @@
 ing.
-ConsoleHandler;%0Aimport java.util.logging.Formatter;%0Aimport java.util.logging.LogRecord;%0Aimport java.util.logging.Logger
+*
 ;%0A%0A/
@@ -312,60 +312,14 @@
 gger
- for the specified bot.%0A   *%0A   * @param bot the bot
+.%0A   *
 %0A   
@@ -382,31 +382,24 @@
 r newLogger(
-Bot bot
 ) %7B%0A    Logg
@@ -424,32 +424,24 @@
 .get
-Logger(bot.getUsername()
+AnonymousLogger(
 );%0A 
@@ -1391,32 +1391,8 @@
   *%0A
-   * @param bot the bot%0A
    *
@@ -1473,15 +1473,8 @@
 ger(
-Bot bot
 ) %7B%0A
@@ -1507,32 +1507,24 @@
 .get
-Logger(bot.getUsername()
+AnonymousLogger(
 );%0A 

Fix logger logs multiple times if more instances of LongPollingBot with same Bot are created