using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;

namespace Shared;

public class Log
{

#pragma warning disable CA2254
#pragma warning disable IDE0060

    private readonly ILogger _Logger;

    public Log(ILogger logger) => _Logger = logger;

    public void Black(object message) => _Logger.LogInformation(message.ToString());
    public void Blue(object message) => _Logger.LogInformation(message.ToString());
    public void Gray(object message) => _Logger.LogInformation(message.ToString());
    public void Green(object message) => _Logger.LogInformation(message.ToString());
    public void Magenta(string message, Exception exception) => _Logger.LogWarning(message, exception);
    public void Red(string message, Exception exception) => _Logger.LogError(message, exception);
    public void White(object message) => _Logger.LogInformation(message.ToString());
    public void Yellow(string message, Exception exception) => _Logger.LogWarning(message, exception);

    //

    public void DoLog(LogLevel logLevel, EventId eventId, Exception exception, string message, params object[] args) => _Logger.Log(logLevel, eventId, exception, message, args);
    public void DoLog(LogLevel logLevel, EventId eventId, string message, params object[] args) => _Logger.Log(logLevel, eventId, message, args);
    public void DoLog(LogLevel logLevel, Exception exception, string message, params object[] args) => _Logger.Log(logLevel, exception, message, args);
    public void DoLog(LogLevel logLevel, string message, params object[] args) => _Logger.Log(logLevel, message, args);
    public void LogCritical(EventId eventId, Exception exception, string message, params object[] args) => _Logger.LogCritical(eventId, exception, message, args);
    public void LogCritical(EventId eventId, string message, params object[] args) => _Logger.LogCritical(eventId, message, args);
    public void LogCritical(Exception exception, string message, params object[] args) => _Logger.LogCritical(exception, message, args);
    public void LogCritical(string message, params object[] args) => _Logger.LogCritical(message, args);
    public void LogDebug(EventId eventId, Exception exception, string message, params object[] args) => _Logger.LogDebug(eventId, exception, message, args);
    public void LogDebug(EventId eventId, string message, params object[] args) => _Logger.LogDebug(eventId, message, args);
    public void LogDebug(Exception exception, string message, params object[] args) => _Logger.LogDebug(exception, message, args);
    public void LogDebug(string message, params object[] args) => _Logger.LogDebug(message, args);
    public void LogError(EventId eventId, Exception exception, string message, params object[] args) => _Logger.LogError(eventId, exception, message, args);
    public void LogError(EventId eventId, string message, params object[] args) => _Logger.LogError(eventId, message, args);
    public void LogError(Exception exception, string message, params object[] args) => _Logger.LogError(message, args);
    public void LogError(string message, params object[] args) => _Logger.LogError(message, args);
    public void LogInformation(EventId eventId, Exception exception, string message, params object[] args) => _Logger.LogInformation(eventId, exception, message, args);
    public void LogInformation(EventId eventId, string message, params object[] args) => _Logger.LogInformation(eventId, message, args);
    public void LogInformation(Exception exception, string message, params object[] args) => _Logger.LogInformation(exception, message, args);
    public void LogInformation(string message, params object[] args) => _Logger.LogInformation(message, args);
    public void LogTrace(EventId eventId, Exception exception, string message, params object[] args) => _Logger.LogTrace(eventId, exception, message, args);
    public void LogTrace(EventId eventId, string message, params object[] args) => _Logger.LogTrace(eventId, message, args);
    public void LogTrace(Exception exception, string message, params object[] args) => _Logger.LogTrace(exception, message, args);
    public void LogTrace(string message, params object[] args) => _Logger.LogTrace(message, args);
    public void LogWarning(EventId eventId, Exception exception, string message, params object[] args) => _Logger.LogWarning(eventId, exception, message, args);
    public void LogWarning(EventId eventId, string message, params object[] args) => _Logger.LogWarning(eventId, message, args);
    public void LogWarning(Exception exception, string message, params object[] args) => _Logger.LogWarning(exception, message, args);
    public void LogWarning(string message, params object[] args) => _Logger.LogWarning(message, args);

    //

    public void Debug() => _Logger.LogDebug(string.Empty);
    public void Debug(object message) => _Logger.LogDebug(message.ToString());
    public void Debug(string message, Exception exception) => _Logger.LogDebug(exception, message);
    public void DebugFormat(IFormatProvider provider, string format, params object[] args) => _Logger.LogDebug(string.Format(provider, format, args));
    public void DebugFormat(string format, object arg0, object arg1, object arg2) => _Logger.LogDebug(string.Format(format, arg0, arg1, arg2));
    public void DebugFormat(string format, object arg0, object arg1) => _Logger.LogDebug(string.Format(format, arg0, arg1));
    public void DebugFormat(string format, object arg0) => _Logger.LogDebug(string.Format(format, arg0));
    public void DebugFormat(string format, params object[] args) => _Logger.LogDebug(string.Format(format, args));
    public void Error(object message) => _Logger.LogError(message.ToString());
    public void Error(string message, Exception exception) => _Logger.LogError(exception, message.ToString());
    public void ErrorFormat(IFormatProvider provider, string format, params object[] args) => _Logger.LogError(string.Format(provider, format, args));
    public void ErrorFormat(string format, object arg0, object arg1, object arg2) => _Logger.LogError(string.Format(format, arg0, arg1, arg2));
    public void ErrorFormat(string format, object arg0, object arg1) => _Logger.LogError(string.Format(format, arg0, arg1));
    public void ErrorFormat(string format, object arg0) => _Logger.LogError(string.Format(format, arg0));
    public void ErrorFormat(string format, params object[] args) => _Logger.LogError(string.Format(format, args));
    public void Fatal(object message) => _Logger.LogCritical(message.ToString());
    public void Fatal(string message, Exception exception) => _Logger.LogCritical(exception, message.ToString());
    public void FatalFormat(IFormatProvider provider, string format, params object[] args) => _Logger.LogCritical(string.Format(provider, format, args));
    public void FatalFormat(string format, object arg0, object arg1, object arg2) => _Logger.LogCritical(string.Format(format, arg0, arg1, arg2));
    public void FatalFormat(string format, object arg0, object arg1) => _Logger.LogCritical(string.Format(format, arg0, arg1));
    public void FatalFormat(string format, object arg0) => _Logger.LogCritical(string.Format(format, arg0));
    public void FatalFormat(string format, params object[] args) => _Logger.LogCritical(string.Format(format, args));
    public void Info(object message) => _Logger.LogInformation(message.ToString());
    public void Info(string message, Exception exception) => _Logger.LogInformation(exception, message.ToString());
    public void InfoFormat(IFormatProvider provider, string format, params object[] args) => _Logger.LogInformation(string.Format(provider, format, args));
    public void InfoFormat(string format, object arg0, object arg1, object arg2) => _Logger.LogInformation(string.Format(format, arg0, arg1, arg2));
    public void InfoFormat(string format, object arg0, object arg1) => _Logger.LogInformation(string.Format(format, arg0, arg1));
    public void InfoFormat(string format, object arg0) => _Logger.LogInformation(string.Format(format, arg0));
    public void InfoFormat(string format, params object[] args) => _Logger.LogInformation(string.Format(format, args));
    public void Warn(object message) => _Logger.LogWarning(message.ToString());
    public void Warn(string message, Exception exception) => _Logger.LogWarning(exception, message.ToString());
    public void WarnFormat(IFormatProvider provider, string format, params object[] args) => _Logger.LogWarning(string.Format(provider, format, args));
    public void WarnFormat(string format, object arg0, object arg1, object arg2) => _Logger.LogWarning(string.Format(format, arg0, arg1, arg2));
    public void WarnFormat(string format, object arg0, object arg1) => _Logger.LogWarning(string.Format(format, arg0, arg1));
    public void WarnFormat(string format, object arg0) => _Logger.LogWarning(string.Format(format, arg0));
    public void WarnFormat(string format, params object[] args) => _Logger.LogWarning(string.Format(format, args));

    public static string GetWorkingDirectory(string executingAssemblyName, string subDirectoryName)
    {
        string result = string.Empty;
        string traceFile;
        List<string> directories = new();
        Environment.SpecialFolder[] specialFolders = new Environment.SpecialFolder[]
        {
                    Environment.SpecialFolder.LocalApplicationData,
                    Environment.SpecialFolder.ApplicationData,
                    Environment.SpecialFolder.History,
                    Environment.SpecialFolder.CommonApplicationData,
                    Environment.SpecialFolder.InternetCache
        };
        foreach (Environment.SpecialFolder specialFolder in specialFolders)
            directories.Add(Path.Combine(Environment.GetFolderPath(specialFolder), subDirectoryName, executingAssemblyName));
        foreach (string directory in directories)
        {
            for (int i = 1; i < 3; i++)
            {
                if (i == 1)
                    result = directory;
                else
                    result = string.Concat("D", directory.Substring(1));
                try
                {
                    if (!Directory.Exists(result))
                        _ = Directory.CreateDirectory(result);
                    traceFile = string.Concat(result, @"\", DateTime.Now.Ticks, ".txt");
                    File.WriteAllText(traceFile, traceFile);
                    File.Delete(traceFile);
                    break;
                }
                catch (Exception) { result = string.Empty; }
            }
            if (!string.IsNullOrEmpty(result))
                break;
        }
        if (string.IsNullOrEmpty(result))
            throw new Exception("Unable to set working directory!");
        return result;
    }

}