#if UNITY_EDITOR
using XGame.Framework.Utils;
using System.Collections.Generic;
using UnityEngine;

namespace XGame.Framework.Logger
{
    internal class UnityLogColors : ILogColors
    {
        private Dictionary<LogLevel, Color32> _colorMap;
        public UnityLogColors()
        {
            _colorMap = new Dictionary<LogLevel, Color32>();
            Initialize();
        }
        #region 接口实现
        public void Initialize()
        {
            Debug.Log("初始化UnityLogColor.");
            _colorMap[LogLevel.Debug] = ReadColor(LogLevel.Debug, ColorUtils.HexToColor(0xFF8428D9));
            _colorMap[LogLevel.Info] = ReadColor(LogLevel.Info, Color.white);
            _colorMap[LogLevel.Warn] = ReadColor(LogLevel.Warn, Color.yellow);
            _colorMap[LogLevel.Error] = ReadColor(LogLevel.Error, Color.red);
            _colorMap[LogLevel.Exception] = ReadColor(LogLevel.Exception, ColorUtils.HexToColor(0xFFF000FF));
            _colorMap[LogLevel.Event] = ReadColor(LogLevel.Event, Color.white);
        }
        public string Format(LogLevel level, string logInfo)
        {
            var color = _colorMap[level];
            return $"<color=#{ColorUtils.ColorToHexString(color)}>{logInfo}</color>";
        }

        public Color32 GetColor(LogLevel level)
        {
            return _colorMap[level];
        }
        #endregion

        private Color32 ReadColor(LogLevel level, Color32 defaultColor)
        {
            var key = $"{typeof(LogLevel)}.{level}";
            if (UnityEditor.EditorPrefs.HasKey(key))
            {
                var hexColor = (uint)UnityEditor.EditorPrefs.GetInt(key);
                return ColorUtils.HexToColor(hexColor);
            }
            return defaultColor;
        }


    }
}
#endif
