using XGame.Framework.Assertions;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;

namespace XGame.Framework
{
    //[DebuggerStepThrough]
    public static partial class Assert
    {
        //public static bool raiseExceptions = false;
        static StringBuilder userMsgStringBuilder = new StringBuilder(1024);
        static void Fail(string message, string format, params object[] args)
        {
            userMsgStringBuilder.Length = 0;
            string userMessage = string.Empty;

            if (!string.IsNullOrEmpty(format))
                userMessage = userMsgStringBuilder.AppendFormat(format, args).ToString();

            ThrowException(message, userMessage);
            LogDisk(message, userMessage);
        }

        static void Fail<T>(string message, T e) where T : Exception
        {
            ThrowException(message, e);
            LogDisk(message, e.Message);
        }

        [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.UNITY_STANDALONE_WIN)]
        private static void ThrowException(string message, string userMessage)
        {

            throw new AssertionException(message, userMessage);
        }

        [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.UNITY_STANDALONE_WIN)]
        private static void ThrowException<T>(string message, T e) where T : Exception
        {
            throw (T)Activator.CreateInstance(typeof(T), $"\n【XGame Assert】{e.Message}\n{message}", new Exception("<----"));
        }

        private static void LogDisk(string message, string userMessage)
        {
            Log.Error(string.Format("【XGame Assert】{0} {1}", userMessage, message));
        }


        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Assert.Equals should not be used for Assertions", true)]
        public new static bool Equals(object obj1, object obj2)
        {
            throw new InvalidOperationException("Assert.Equals should not be used for Assertions");
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Assert.ReferenceEquals should not be used for Assertions", true)]
        public new static bool ReferenceEquals(object obj1, object obj2)
        {
            throw new InvalidOperationException("Assert.ReferenceEquals should not be used for Assertions");
        }

    }
}
