package org.hirw.game.util;

import java.util.EnumMap;
import java.util.Map;

public final class Log {
  private interface Colours {
    final String BLACK = "\u001B[30m";
    final String BLACK_BG = "\u001B[40m";
    final String RED = "\u001B[31m";
    final String RED_BG = "\u001B[41m";
    final String GREEN = "\u001B[32m";
    final String GREEN_BG = "\u001B[42m";
    final String YELLOW = "\u001B[33m";
    final String YELLOW_BG = "\u001B[43m";
    final String BLUE = "\u001B[34m";
    final String BLUE_BG = "\u001B[44m";
    final String PURPLE = "\u001B[35m";
    final String PURPLE_BG = "\u001B[45m";
    final String CYAN = "\u001B[36m";
    final String CYAN_BG = "\u001B[46m";
    final String WHITE = "\u001B[37m";
    final String WHITE_BG = "\u001B[47m";
    final String ANSI_RESET = "\u001B[0m";
  }

  private static final EnumMap<LogType, String> COLOUR_FROM_LOG_TYPE =
      new EnumMap<>(
          Map.of(
              LogType.SUCCESS, Colours.GREEN,
              LogType.ERROR, Colours.RED,
              LogType.WARNING, Colours.PURPLE,
              LogType.DEBUG, Colours.WHITE));

  public static void error(String errorStage, String errorDescription) {
    System.out.println(formatString(LogType.ERROR, errorStage, errorDescription));
  }

  public static void success(String successStage, String successDescription) {
    System.out.println(formatString(LogType.SUCCESS, successStage, successDescription));
  }

  public static void warning(String warningStage, String warningDescription) {
    System.out.println(formatString(LogType.WARNING, warningStage, warningDescription));
  }

  public static void debug(String debugMessage) {
    System.out.println(formatString(LogType.DEBUG, "DEBUG", debugMessage));
  }

  private static String formatString(LogType logType, String stage, String description) {
    String formattedType = String.format("[%s]", colouriseString(logType, logType.toString()));
    String formattedStage = colouriseString(Colours.YELLOW, String.format("<%s>", stage));
    return String.format("%s %s %s", formattedType, formattedStage, description);
  }

  private static String colouriseString(LogType logType, String string) {
    return COLOUR_FROM_LOG_TYPE.get(logType) + string + Colours.ANSI_RESET;
  }

  private static String colouriseString(String colour, String string) {
    return colour + string + Colours.ANSI_RESET;
  }

  private static String colouriseString(String colour, String otherColour, String string) {
    return colour + otherColour + string + Colours.ANSI_RESET;
  }
}
