﻿#region Copyright

/* -----------------------------------------------------------------------------
 *  Copyright © 2024 • Chomp Studios • All Rights Reserved
 * -----------------------------------------------------------------------------
 *  Date: 30/10/2024
 *  -----------------------------------------------------------------------------
 *  Description: Houses the logic for the custom NLog Logger
 *  -----------------------------------------------------------------------------
 */

#endregion

#if UNITY_EDITOR

#region Imports

using System;
using System.IO;
using NLog;
using NLog.Config;
using NLog.Targets;
using UnityEngine;
using Logger = NLog.Logger;
#if UNITY_6000_5_OR_NEWER
using Unity.Scripting.LifecycleManagement;
#endif

#endregion

namespace Workflow_Toolkit.Utilities {
    public static class LoggingElements {
        [NoAutoStaticsCleanup]
        private static readonly Logger logger;

        [NoAutoStaticsCleanup]
        private static readonly string LOGTag = " [LOGGER] ";

        static LoggingElements() {
            try {
                var logDir = Path.Combine(Application.dataPath, "..", "EditorLogs", "Workflow Toolkit");

                if (!Directory.Exists(logDir)) {
                    Directory.CreateDirectory(logDir);
                    Debug.Log($"Created log directory at {logDir}");
                }

                var config = new LoggingConfiguration();

                var fileTarget = new FileTarget("file") {
                    FileName = Path.Combine(logDir, "workflowtoolkit.log"),
                    Layout =
                        "${longdate} | ${uppercase:${level}} | ${message} | ${exception:format=tostring} | ${callsite:methodName=true:fileName=true:includeSourcePath=false:lineNumber=true}",
                    DeleteOldFileOnStartup = true,
                    ConcurrentWrites = false,
                    KeepFileOpen = true
                };

                config.AddTarget(fileTarget);
                config.AddRuleForAllLevels(fileTarget);

                LogManager.Configuration = config;

                logger = LogManager.GetCurrentClassLogger();
                Debug.Log("Global NLog configuration loaded successfully.");
            }
            catch (Exception ex) {
                Debug.LogError($"Error initializing GlobalLogger: {ex.Message}");
            }
        }

        /// <summary>
        ///     Gets the current NLog Instance
        /// </summary>
        /// <returns>The NLog <see cref="Logger" /> instance.</returns>
        public static Logger GetLogger() {
            return logger;
        }

        /// <summary>
        ///     Logs an informational message.
        /// </summary>
        /// <param name="message">The message to log.</param>
        public static void LogInfo(string message) {
            logger.Info(LOGTag + message);
        }

        /// <summary>
        ///     Logs a warning message.
        /// </summary>
        /// <param name="message">The warning message to log.</param>
        public static void LogWarning(string message) {
            logger.Warn(LOGTag + message);
        }

        /// <summary>
        ///     Logs an error message.
        /// </summary>
        /// <param name="message">The error message to log.</param>
        public static void LogError(string message) {
            logger.Error(LOGTag + message);
        }

        /// <summary>
        ///     Logs a debug message.
        /// </summary>
        /// <param name="message">The debug message to log.</param>
        public static void LogDebug(string message) {
            logger.Debug(LOGTag + message);
        }

        /// <summary>
        ///     Logs an exception.
        /// </summary>
        /// <param name="exception">The exception to log.</param>
        public static void LogException(Exception exception) {
            logger.Error(exception);
        }
    }
}
#endif