using Serilog;
using UnityEngine;
using SerilogLogger = Serilog.Core.Logger;

namespace Logging
{
    public static class UnityLogger
    {
        private static readonly SerilogLogger Logger = new LoggerConfiguration()
            .WriteTo.File("Logs/UnityLog.txt")
            .CreateLogger();

        public static void Init()
        {
            Application.logMessageReceived += OnLogMessageReceived;
        }

        private static void OnLogMessageReceived(string logString, string stackTrace, LogType type)
        {
            switch (type)
            {
                case LogType.Assert:
                {
                    Logger.Warning("\nContent: {0}", logString);
                    break;
                }
                case LogType.Log:
                {
                    Logger.Information("\nContent: {0}", logString);
                    break;
                }
                case LogType.Warning:
                {
                    Logger.Warning("\nContent: {0}", logString);
                    break;
                }
                case LogType.Error:
                {
                    Logger.Error("\nContent: {0}\nStackTrace:\n{1}", logString, stackTrace[..^1]);
                    break;
                }
                case LogType.Exception:
                {
                    Logger.Fatal("\nContent: {0}\nStackTrace:\n{1}", logString, stackTrace[..^1]);
                    break;
                }
                default:
                {
                    Logger.Information("\nContent: {0}", logString);
                    break;
                }
            }
        }
    }
}