namespace NubLang.Diagnostics;

public static class ConsoleColors
{
    public const string Reset = "\e[0m";
    public const string Bold = "\e[1m";
    public const string Faint = "\e[2m";
    public const string Italic = "\e[3m";
    public const string Underline = "\e[4m";
    public const string SlowBlink = "\e[5m";
    public const string RapidBlink = "\e[6m";
    public const string SwapBgAndFg = "\e[7m";
    public const string Conceal = "\e[8m";
    public const string CrossedOut = "\e[9m";

    public const string DefaultFont = "\e[10m";
    public const string AltFont1 = "\e[11m";
    public const string AltFont2 = "\e[12m";
    public const string AltFont3 = "\e[13m";
    public const string AltFont4 = "\e[14m";
    public const string AltFont5 = "\e[15m";
    public const string AltFont6 = "\e[16m";
    public const string AltFont7 = "\e[17m";
    public const string AltFont8 = "\e[18m";
    public const string AltFont9 = "\e[19m";

    public const string Black = "\e[30m";
    public const string Red = "\e[31m";
    public const string Green = "\e[32m";
    public const string Yellow = "\e[33m";
    public const string Blue = "\e[34m";
    public const string Magenta = "\e[35m";
    public const string Cyan = "\e[36m";
    public const string White = "\e[37m";

    public const string BrightBlack = "\e[90m";
    public const string BrightRed = "\e[91m";
    public const string BrightGreen = "\e[92m";
    public const string BrightYellow = "\e[93m";
    public const string BrightBlue = "\e[94m";
    public const string BrightMagenta = "\e[95m";
    public const string BrightCyan = "\e[96m";
    public const string BrightWhite = "\e[97m";

    private static bool IsColorSupported()
    {
        var term = Environment.GetEnvironmentVariable("TERM");
        var colorTerm = Environment.GetEnvironmentVariable("COLORTERM");
        return !string.IsNullOrEmpty(term) || !string.IsNullOrEmpty(colorTerm) || !Console.IsOutputRedirected;
    }

    public static string Colorize(string text, string color)
    {
        return IsColorSupported() ? $"{color}{text}{Reset}" : text;
    }
}