using System;
using System.Runtime.CompilerServices;

namespace VCommon.Logging
{
    /// <summary> 日志入口,可在运行时指定目标 </summary>
    public static class Logger
    {
        private static ILogger _target = new ConsoleLogger();

        private static bool _enableDebugLevel;
        private static Action<string, object> _debug;

        /// <summary> 指定日志目标 </summary>
        public static void Init(ILogger target)
        {
            if (target is DefaultLogger) throw new ArgumentException($"Can not use {nameof(DefaultLogger)}");
            _target = target;
            if (EnableDebugLevel) _debug = _target.Debug;
        }

        /// <summary> 启用Debug级别日志 </summary>
        public static bool EnableDebugLevel
        {
            get => _enableDebugLevel;
            set
            {
                _enableDebugLevel = value;
                _debug = value ? _target.Debug : (Action<string, object>)null;
            }
        }

        [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
        public static void Debug(string summary, object moreInfo = null) => _debug?.Invoke(summary, moreInfo);

        [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
        public static void Trace(string summary, object moreInfo = null) => _target.Trace(summary, moreInfo);

        [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
        public static void Info(string summary, object moreInfo = null) => _target.Info(summary, moreInfo);

        [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
        public static void Warn(string summary, object moreInfo = null) => _target.Warn(summary, moreInfo);

        [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
        public static void Error(string summary, object moreInfo = null) => _target.Error(summary, moreInfo);

        [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
        public static void Fatal(string summary, object moreInfo = null) => _target.Fatal(summary, moreInfo);
    }
}