using System.Collections;
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.Reflection;

namespace TFramework
{
    [CustomEditor(typeof(LanguageManager))]
    public class LanguageManagerInspector : TEditor
    {
        SerializedProperty m_languageData;
        SerializedProperty m_initKind;

        List<string> kinds = new List<string>();

        LanguageData data;

        LanguageData Data
        {
            get => data;
            set
            {
                data = value;
                if (data != null)
                {
                    FieldInfo[] fieldInfos = GlobalTool.GetTypesInRunTimeAssemblies(type => type.FullName == data.m_languageType)[0].GetFields();

                    kinds.Clear();
                    for (int i = 0; i < fieldInfos.Length; i++)
                    {
                        LanguageAttribute attribute = fieldInfos[i].GetCustomAttribute<LanguageAttribute>();
                        kinds.Add(attribute != null ? attribute.LanguageType : fieldInfos[i].Name);
                    }
                }
                serializedObject.Update();
                (target as LanguageManager).m_languageData = data;
                serializedObject.ApplyModifiedProperties();
            }
        }

        int selecteKind = 0;

        private void OnEnable()
        {
            m_languageData = serializedObject.FindProperty("m_languageData");
            m_initKind = serializedObject.FindProperty("m_initKind");
            data = (target as LanguageManager).m_languageData;
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            using(new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label("语言数据");
                Data = EditorGUILayout.ObjectField(Data, typeof(LanguageData), false) as LanguageData;
            }
            using(new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label("初始语种");
                selecteKind = kinds.IndexOf(m_initKind.stringValue);
                selecteKind = EditorGUILayout.Popup(selecteKind, kinds.ToArray());
                if (kinds.Count > 0)
                    m_initKind.stringValue = kinds[selecteKind];
            }
            serializedObject.ApplyModifiedProperties();
        }
    }
}