
using XGame.Framework.Logger;
using XGame.Framework.Utils;
using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Logger 
{
    public class UnityLogSettingWindow : EditorWindow
    {
        [MenuItem("Tools/Log Setting/Color Setting")]
        public static void ShowUnityLogSetting() 
        {
            UnityLogSettingWindow window = EditorWindow.GetWindowWithRect<UnityLogSettingWindow>(new Rect(0, 0, 600, 200) ,false, "LogSetting");
            window.Show();
        }
        //[MenuItem("Tools/Log Setting/Test")]
        //public static void Test()
        //{
        //    Debug.Log($"{typeof(LogLevel)}.{LogLevel.Event}");
        //}

        private Color32 _debug;
        private Color32 _info;
        private Color32 _warn;
        private Color32 _error;
        private Color32 _exception;
        private Color32 _evnet;

        private GUIStyle titleStyle = new GUIStyle();

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

        private void WriteColor(LogLevel level, Color32 color) 
        {
            var key = $"{typeof(LogLevel)}.{level}";
            EditorPrefs.SetInt(key, (int)ColorUtils.ColorToHex(color));
        }

        private void Save() 
        {
            WriteColor(LogLevel.Debug, _debug);
            WriteColor(LogLevel.Info, _info);
            WriteColor(LogLevel.Warn, _warn);
            WriteColor(LogLevel.Error, _error);
            WriteColor(LogLevel.Exception, _exception);
            WriteColor(LogLevel.Event, _evnet);
        }

        private void OnEnable()
        {
            _debug = ReadColor(LogLevel.Debug, ColorUtils.HexToColor(0xFF8428D9));
            _info = ReadColor(LogLevel.Info, Color.white);
            _warn = ReadColor(LogLevel.Warn, Color.yellow);
            _error = ReadColor(LogLevel.Error, Color.red);
            _exception = ReadColor(LogLevel.Exception, ColorUtils.HexToColor(0xFFF000FF));
            _evnet = ReadColor(LogLevel.Event, Color.white);
        }

        private void DrawLine(LogLevel level, ref Color32 color) 
        {
            EditorGUILayout.BeginHorizontal();
            titleStyle.normal.textColor = color;
            EditorGUILayout.LabelField($"日志等级[{level}]文本颜色", titleStyle, GUILayout.Width(300));
            color = EditorGUILayout.ColorField(color);
            EditorGUILayout.EndHorizontal();
            GUILayout.Space(2);
        }

        private void OnGUI()
        {
            GUILayout.Space(5);

            DrawLine(LogLevel.Debug, ref _debug);
            DrawLine(LogLevel.Info, ref _info);
            DrawLine(LogLevel.Warn, ref _warn);
            DrawLine(LogLevel.Error, ref _error);
            DrawLine(LogLevel.Exception, ref _exception);
            DrawLine(LogLevel.Event, ref _evnet);

            EditorGUILayout.Space();
            EditorGUILayout.Space();
            if (GUILayout.Button("Save"))
                Save();
        }
    }

}
