/*
 *Copyright(C) 2023 by Chief All rights reserved.
 *Unity版本：2023.2.5f1c1 
 *作者:Chief  
 *创建日期: 2024-01-19 
 *模块说明：通用模块：打印日志管理
 *版本: 1.0
*/

using System;
using UnityEngine;

namespace Aster
{
    /// <summary>
    /// 日志打印管理
    /// </summary>
    public static class Logger
    {

        public static bool IsOpenLog(E_LogType type)
        {
#if UNITY_EDITOR
            return type.HasFlag(E_LogType.Editor);
#else
            return type.HasFlag(E_LogType.NotEditor);
#endif
        }


        public static void LogError(object message, E_LogType type = E_LogType.All)
        {
            if (!IsOpenLog(type))
                return;

            Debug.LogError($"[{AsterUtils.TimeNowMMSSFF}]{message}");
        }

        public static void LogAggregateException(AggregateException aggregateException, E_LogType type = E_LogType.All)
        {
            if (!IsOpenLog(type))
                return;

            string errorStr = $" Message: \n{aggregateException.Message} \n StackTrace: \n {aggregateException.StackTrace}";

            var exs = aggregateException.InnerExceptions;
            int length = exs.Count;
            for (int i = 0; i < length; i++)
            {
                var ex = exs[i];
                errorStr = $"{errorStr}\n InnerExceptions [{i}]: \n Message:\n{ex.Message}\nInnerException: \n{ex.InnerException}\nSource: \n {ex.Source} \n StackTrace: \n {ex.StackTrace}";
            }
            Debug.LogError(errorStr);
        }


        public static void LogException(Exception ex, E_LogType type = E_LogType.All)
        {
            if (!IsOpenLog(type))
                return;

            Debug.LogException(ex);
        }


        public static void LogWarning(object message, E_LogType type = E_LogType.Editor)
        {
            if (!IsOpenLog(type))
                return;

            Debug.LogWarning($"[{AsterUtils.TimeNowMMSSFF}]{message}");
        }

        /// <summary>
        /// 普通的Log，以红颜色打印；
        /// </summary>
        /// <param name="message"></param>
        public static void Log_Red(object message, E_LogType type = E_LogType.Editor)
        {
            if (!IsOpenLog(type))
                return;

            Log($"<color=red>{message}</color>", type);
        }

        /// <summary>
        /// 普通的Log，以红颜色打印；
        /// </summary>
        /// <param name="message"></param>
        public static void Log_Green(object message, E_LogType type = E_LogType.Editor)
        {
            if (!IsOpenLog(type))
                return;

            Log($"<color=green>{message}</color>", type);
        }

        public static void Log_Blue(object message, E_LogType type = E_LogType.Editor)
        {
            if (!IsOpenLog(type))
                return;

            Log($"<color=#0080FF>{message}</color>", type);
        }

        public static void Log_Yellow(object message, E_LogType type = E_LogType.Editor)
        {
            if (!IsOpenLog(type))
                return;

            Log($"<color=yellow>{message}</color>", type);
        }

        public static void Log_Orange(object message, E_LogType type = E_LogType.Editor)
        {
            if (!IsOpenLog(type))
                return;

            Log($"<color=FF8000>{message}</color>", type);
        }


        /// <summary>
        /// 这个Log只会在编辑器下生效；
        /// </summary>
        /// <param name="message"></param>
        public static void Log(object message, E_LogType type = E_LogType.Editor)
        {
            if (!IsOpenLog(type))
                return;

            Debug.Log($"[{AsterUtils.TimeNowMMSSFF}]{message}");
        }

    }
}