using System.IO.Compression;
using EonaCat.Logger.EonaCatCoreLogger;
using EonaCat.Logger.Extensions;
using EonaCat.Logger.Managers;

namespace EonaCat.Logger.Test.Web;

public static class Logger
{
    private static LogManager LogManager;
    public static ELogType MinLogType { get; set; }
    public static bool UseLocalTime { get; set; }
    public static string LogFolder => Path.Combine(FileLoggerOptions.DefaultPath, "logs");
    public static string CurrentLogFile => LogManager.CurrentLogFile;
    public static bool IsDisabled { get; set; }

    public static void DeleteCurrentLogFile()
    {
        if (IsDisabled)
            return;

        LogManager.DeleteCurrentLogFile();
    }

    private static string ConvertToAbsolutePath(string path)
    {
        return Path.IsPathRooted(path) ? path : Path.Combine(LogFolder, path);
    }

    public static async Task DownloadLogAsync(HttpContext context, string logName, long limit)
    {
        var logFileName = logName + ".log";

        await using var fS = new FileStream(Path.Combine(ConvertToAbsolutePath(LogFolder), logFileName), FileMode.Open,
            FileAccess.Read, FileShare.ReadWrite, 64 * 1024, true);
        var response = context.Response;

        response.ContentType = "text/plain";
        response.Headers.ContentDisposition = "attachment;filename=" + logFileName;

        if (limit > fS.Length || limit < 1)
            limit = fS.Length;

        var oFS = new OffsetStream(fS, 0, limit);
        var request = context.Request;
        Stream stream;

        string acceptEncoding = request.Headers["Accept-Encoding"];
        if (string.IsNullOrEmpty(acceptEncoding))
        {
            stream = response.Body;
        }
        else
        {
            var acceptEncodingParts = acceptEncoding.Split(new[] { ',' },
                StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

            if (acceptEncodingParts.Contains("br"))
            {
                response.Headers.ContentEncoding = "br";
                stream = new BrotliStream(response.Body, CompressionMode.Compress);
            }
            else if (acceptEncodingParts.Contains("gzip"))
            {
                response.Headers.ContentEncoding = "gzip";
                stream = new GZipStream(response.Body, CompressionMode.Compress);
            }
            else if (acceptEncodingParts.Contains("deflate"))
            {
                response.Headers.ContentEncoding = "deflate";
                stream = new DeflateStream(response.Body, CompressionMode.Compress);
            }
            else
            {
                stream = response.Body;
            }
        }

        await using (stream)
        {
            await oFS.CopyToAsync(stream).ConfigureAwait(false);

            if (fS.Length > limit)
                await stream.WriteAsync("\r\n####__EONACATLOGGER_TRUNCATED___####"u8.ToArray()).ConfigureAwait(false);
        }
    }

    public static void Log(string message, ELogType logType = ELogType.INFO, bool writeToConsole = true)
    {
        if (IsDisabled)
            return;

        LogManager.Write(message, logType, writeToConsole);
    }

    public static void Log(Exception exception, string message = "", bool writeToConsole = true)
    {
        if (IsDisabled)
            return;

        LogManager.Write(exception, message, writeToConsole: writeToConsole);
    }

    public static void Configure()
    {
        var loggerSettings = new LoggerSettings
        {
            Id = "EonaCatTestLogger",
            MinLogType = ELogType.INFO,
            UseLocalTime = UseLocalTime,
            FileLoggerOptions =
            {
                LogDirectory = LogFolder,
                FileSizeLimit = 20_000_000, // 20 MB,
                UseLocalTime = UseLocalTime
            }
        };
        LogManager = new LogManager(loggerSettings);
    }
}