using AyCode.Core.Consts;
using AyCode.Core.Enums;
using AyCode.Utils.Extensions;

namespace AyCode.Core.Loggers;

public interface IAcConsoleLogWriter : IAcTextLogWriterBase
{}

public class AcConsoleLogWriter : AcTextLogWriterBase, IAcConsoleLogWriter
{
    private static readonly object ForWriterLock = new();

    protected AcConsoleLogWriter() : this(null)
    {
        Initialize();
    }

    protected AcConsoleLogWriter(string? categoryName = null) : base(categoryName)
    {
        Initialize();
    }

    public AcConsoleLogWriter(AppType appType, LogLevel logLevel, string? categoryName = null) : base(appType, logLevel, categoryName)
    {
        Initialize();
    }

    private static void Initialize()
    {
        Console.ForegroundColor = ConsoleColor.White;
    }

    protected override void WriteText(string? logText, LogLevel loglevel)
    {
        if (logText.IsNullOrWhiteSpace()) return;

        lock (ForWriterLock)
        {
            switch (loglevel)
            {
                case > LogLevel.Trace and < LogLevel.Suggest:
                    Console.WriteLine(logText);
                    return;
                case <= LogLevel.Trace:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(logText);
                    break;
                case LogLevel.Suggest:
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(logText);
                    break;
                case LogLevel.Warning:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(logText);
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"{AcEnv.NL}{logText}{AcEnv.NL}");
                    break;
            }

            Console.ForegroundColor = ConsoleColor.White;
        }
    }
}