package de.dhbwstuttgart.server;

import de.dhbwstuttgart.util.Logger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.java_websocket.WebSocket;

public class ServerTaskLogger extends Logger {

  private final WebSocket webSocket;
  private final SocketServer socketServer;
  private final LogLevel customLogLevel;

  public ServerTaskLogger(WebSocket webSocket, SocketServer socketServer, LogLevel customLogLevel) {
    this.webSocket = webSocket;
    this.socketServer = socketServer;
    this.customLogLevel = customLogLevel;
  }

  @Override
  public boolean isLogLevelActive(LogLevel logLevel) {
    return logLevel.isHigherOrEqualTo(customLogLevel);
  }

  @Override
  protected void print(String s, LogLevel logLevel) {
    String coloredPrefix = this.getPrefix(logLevel);
    if (logLevel.isHigherOrEqualTo(LogLevel.ERROR)) {
      socketServer.sendError(webSocket, coloredPrefix + s, false);
    }
    else {
      socketServer.sendMessage(webSocket, coloredPrefix + s);
    }
  }

  @Override
  protected void write(String s) {
    // under no circumstances write anything to a file
  }

}
