using System;
using System.Diagnostics;

namespace XGame.Framework
{
    public static class Profiler
    {
        [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
        public static void BeginSample(string sample)
        {
            UnityEngine.Profiling.Profiler.BeginSample(sample);
            Log(sample);
        }

        [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
        public static void EndSample()
        {
            UnityEngine.Profiling.Profiler.EndSample();
        }

        [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
        public static void BeginSample(object obj, string sample)
        {
            BeginSample(sample, obj.GetType());
        }

        [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
        public static void BeginSample(Type type, string sample)
        {
            BeginSample(sample, type);
        }

        [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
        public static void BeginSample(string sample, Type type)
        {
            string str = $"{type.Namespace}.{type.Name} - {sample}";
            UnityEngine.Profiling.Profiler.BeginSample(str);
            Log(str);
        }

        private static void Log(string msg)
        {
            UnityEngine.Debug.Log($"[Profiler] Time:{UnityEngine.Time.realtimeSinceStartup} msg:{msg}");
        }
    }
}
