using System;
using System.Text;
using Core.Utility;


#if COMBAT_SERVER
using NLog;
#endif
#if !COMBAT_SERVER
using UnityEngine;
#endif
using Object = System.Object;

namespace Fort23.UTool
{
    public class LogTool
    {
        public static ILogSend LogSend;
        public static string ColorForGreen = "green";
        public static string ColorForOrange = "orange";
        public static string ColorForRed = "red";
#if COMBAT_SERVER
        public static NLog.Logger logger
        {
            get
            {
                if (_logger == null)
                {
                    _logger = NLog.LogManager.GetCurrentClassLogger();
                }

                return _logger;
            }
        }

        private static NLog.Logger _logger;
#endif
#if UNITY_EDITOR
        public static bool IsDebug = true;
#else
#if COMBAT_SERVER
        public static bool IsDebug = true;
#else
        public static bool IsDebug = false;
#endif
#endif


        private static StringBuilder ColorLog(object str, string color)
        {
            string colorFormat = color;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<color={1}>[{0}]</color>", str, colorFormat);
            return sb;
        }

        public static void Log(object msg)
        {
            if (!IsDebug)
            {
                return;
            }

#if !COMBAT_SERVER
            Debug.Log(ColorLog(msg, ColorForGreen));

#elif COMBAT_SERVER
            logger.Log(LogLevel.Info, msg);


#endif
        }

        public static void Log(string msg)
        {
            if (!IsDebug)
            {
                return;
            }

#if !COMBAT_SERVER
            Debug.Log(ColorLog(msg, ColorForGreen));

#elif COMBAT_SERVER
            logger.Log(LogLevel.Info, msg);
            // logger.Log(LogLevel.Info, msg);

#endif
        }


        public static void Warning(string msg)
        {
#if!COMBAT_SERVER
            Debug.LogWarning(ColorLog(msg, ColorForOrange));
#elif COMBAT_SERVER
            logger.Log(LogLevel.Warn, msg);
#endif
        }

        /// <summary>
        /// 错误是任何情况下都会打印。
        /// </summary>
        /// <param name="msg"></param>
        public static void Error(string msg)
        {
            // System.Diagnostics.StackTrace stackTrace=   new System.Diagnostics.StackTrace();

            LogSend?.SendError(msg);
#if !COMBAT_SERVER
            Debug.LogError(ColorLog(msg, ColorForRed));
            // Debug.LogError(msg);
#elif COMBAT_SERVER
            logger.Error(msg);
#endif
        }


        /// <summary>
        /// 错误是任何情况下都会打印。
        /// </summary>
        /// <param name="e"></param>
        public static void Error(Exception e)
        {
            LogSend?.SendException(e);
#if !COMBAT_SERVER
            Exception(e);
#elif COMBAT_SERVER
            logger.Error(e);
#endif
        }

        public static void Exception(Exception e)
        {
            LogSend?.SendException(e);
#if !COMBAT_SERVER
            Debug.LogException(e);
#elif COMBAT_SERVER
            Console.WriteLine(e);
            logger.Error(e);
#endif
        }

#if UNITY_EDITOR
        /// <summary>
        /// 暂停游戏
        /// </summary>
        public static void Paused()
        {
            UnityEditor.EditorApplication.isPaused = true;
        }
#endif
    }
}