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

namespace TFramework
{
    [CustomEditor(typeof(LanguageData))]
    public class LanguageDataInspector : TEditor
    {
        SerializedProperty m_languageType;
        SerializedProperty m_languages;
        ReorderableList languageList;
        List<string> languageTypes = new List<string>();
        int selectType = 0;
        Type currentType;

        LanguageData languageData;

        private void OnEnable()
        {
            m_languageType = serializedObject.FindProperty("m_languageType");
            m_languages = serializedObject.FindProperty("m_languages");


            languageData = target as LanguageData;
            currentType = GlobalTool.GetTypesInRunTimeAssemblies(type => type.FullName == m_languageType.stringValue)[0];
            languageList = new ReorderableList(languageData.m_languages, currentType, true, true, true, true);

            foreach (var item in GlobalTool.GetTypesInRunTimeAssemblies(type =>
            type==typeof(Language) || (type.IsSubclassOf(typeof(Language)) || (type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(Language)))
            && type.IsClass))
            {
                languageTypes.Add(item.FullName);
            }
            languageList.elementHeight = currentType.GetFields().Length * 23;

            languageList.drawElementCallback = (rect, index, isActive, isFocused) =>
              {
                  serializedObject.Update();
                  FieldInfo[] fields = (target as LanguageData).m_languages[index]?.GetType()?.GetFields();
                  Rect rect1 = Rect.zero;
                  if(fields!=null)
                  for (int i = 0; i < fields.Length; i++)
                  {
                      LanguageAttribute attribute = fields[i].GetCustomAttribute<LanguageAttribute>();
                      string label = attribute == null ? fields[i].Name : attribute.LanguageType;
                      rect1.Set(rect.x, rect.y+i*23, 50, 20);
                      GUI.Label(rect1, label);
                      rect1.Set(rect.x + 50, rect.y + i * 23, rect.width - 53, 20);
                      string value = fields[i].GetValue((target as LanguageData).m_languages[index])?.ToString();
                      string newValue = EditorGUI.TextField(rect1, value);
                      if(newValue!=value)
                      {
                          fields[i].SetValue((target as LanguageData).m_languages[index], newValue);
                      }
                  }
                  serializedObject.ApplyModifiedProperties();
              };

            languageList.onAddCallback = list =>
            {
                serializedObject.Update();
                list.index = (target as LanguageData).m_languages.Count;
                (target as LanguageData).m_languages.Add(currentType.Assembly.CreateInstance(m_languageType.stringValue) as Language);
                serializedObject.ApplyModifiedProperties();
            };
        }

        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
            serializedObject.Update();
            using(new EditorGUILayout.HorizontalScope())
            {
                GUI.enabled = !(languageData.m_languages != null && languageData.m_languages.Count > 0);
                GUILayout.Label("语言数据类型:");
                selectType = languageTypes.IndexOf(m_languageType.stringValue);
                selectType = selectType == -1 ? 0 : selectType;
                selectType = EditorGUILayout.Popup(selectType, languageTypes.ToArray());
                if(m_languageType.stringValue!=languageTypes[selectType])
                {
                    m_languageType.stringValue = languageTypes[selectType];
                }
                GUI.enabled=true;
            }
            languageList.DoLayoutList();
            serializedObject.ApplyModifiedProperties();
        }
    }
}