using System;
using UnityEngine;

namespace SGModule.Common.Helper {
    public static class Log {
        // 预定义模块实例
        public static readonly ModuleLogger Common = new("Common");
        public static readonly ModuleLogger Net = new("Net", ConfigManager.NetworkConfig?.showNetworkLog ?? false);
        public static readonly ModuleLogger MarkdownKit = new("MarkdownKit");
        public static readonly ModuleLogger ApplePay = new("ApplePay");
        public static readonly ModuleLogger ConfigLoader = new("ConfigLoader");
        public static readonly ModuleLogger NetKit = new("NetKit");
        public static readonly ModuleLogger DataStorage = new("DataStorage");
        public static readonly ModuleLogger IAP = new("IAP");

        private static bool IsEnabled => true;//ConfigManager.GameConfig?.enabledLog ?? false;

        public static void Info(string label, string msg, bool showStack = true) {
            if (IsEnabled) {
                InternalLog(LogType.Log, label, msg, LogColors.Info, showStack);
            }
        }

        public static void Warning(string label, string msg, bool showStack = true) {
            if (IsEnabled) {
                InternalLog(LogType.Warning, label, msg, LogColors.Warning, showStack);
            }
        }

        public static void Error(string label, string msg, bool showStack = true) {
            if (IsEnabled) {
                InternalLog(LogType.Error, label, msg, LogColors.Error, showStack);
            }
        }

        public static void Exception(string label, Exception ex, bool showStack = true) {
            if (!IsEnabled || ex == null) {
                return;
            }


            var msg = $"Exception: {ex.Message}";
            if (showStack && !string.IsNullOrEmpty(ex.StackTrace)) {
                msg += $"\n{ex.StackTrace}";
            }

            InternalLog(LogType.Error, label, msg, LogColors.Exception, showStack);
        }

        internal static void InternalLog(LogType type, string label, string msg, string color, bool showStack) {
            var formattedMsg = $"<color={color}>[{label}]</color> {msg}";

            var original = Application.GetStackTraceLogType(type);
            if (!showStack) {
                Application.SetStackTraceLogType(type, StackTraceLogType.None);
            }

            Debug.unityLogger.Log(type, formattedMsg);

            if (!showStack) {
                Application.SetStackTraceLogType(type, original);
            }
        }
    }

    public class ModuleLogger {
        private readonly bool _isEnabled;
        private readonly string _label;

        public ModuleLogger(string label, bool isEnabled = true) {
            _label = label;
            _isEnabled = isEnabled;
        }

        private bool IsEnabled => (ConfigManager.GameConfig?.enabledLog ?? false) && _isEnabled;

        public void Info(string msg, bool showStack = true) {
            if (IsEnabled) {
                Log.InternalLog(LogType.Log, _label, msg, LogColors.Info, showStack);
            }
        }

        public void Warning(string msg, bool showStack = true) {
            if (IsEnabled) {
                Log.InternalLog(LogType.Warning, _label, msg, LogColors.Warning, showStack);
            }
        }

        public void Error(string msg, bool showStack = true) {
            if (IsEnabled) {
                Log.InternalLog(LogType.Error, _label, msg, LogColors.Error, showStack);
            }
        }

        public void Exception(Exception ex, bool showStack = true) {
            if (!IsEnabled || ex == null) {
                return;
            }

            var msg = $"Exception: {ex.Message}";
            if (showStack && !string.IsNullOrEmpty(ex.StackTrace)) {
                msg += $"\n{ex.StackTrace}";
            }

            Log.InternalLog(LogType.Error, _label, msg, LogColors.Exception, showStack);
        }
    }

    public static class LogColors {
        public const string Info = "#4CAF50";
        public const string Warning = "#CC9A06";
        public const string Error = "#CC423B";
        public const string Exception = "red"; // 可选保留原文
    }
}
