using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(NPCData))]
public class NPCDataEditor : Editor
{
    private NPCData.Language _selectedLang = NPCData.Language.EN;

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        var data = (NPCData)target;

        // Basic info
        EditorGUILayout.PropertyField(serializedObject.FindProperty("NPCName"));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("Prefab"));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("MoveSpeed"));

        EditorGUILayout.Space(8);
        DrawLine();

        // Language selector
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Dialogues", EditorStyles.boldLabel);
        _selectedLang = (NPCData.Language)EditorGUILayout.EnumPopup(_selectedLang, GUILayout.Width(80));
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Space(4);

        if (_selectedLang == NPCData.Language.EN)
        {
            // Edit legacy StateDialogues directly
            DrawStateDialogues(serializedObject.FindProperty("StateDialogues"));
        }
        else
        {
            // Edit localized
            DrawLocalizedDialogues(data);
        }

        serializedObject.ApplyModifiedProperties();
    }

    void DrawStateDialogues(SerializedProperty dialogues)
    {
        if (dialogues.arraySize == 0)
        {
            if (GUILayout.Button("Initialize (Idle/Move/Talk/Despair)"))
            {
                dialogues.arraySize = 4;
                for (int i = 0; i < 4; i++)
                    dialogues.GetArrayElementAtIndex(i)
                        .FindPropertyRelative("State").enumValueIndex = i;
            }
            return;
        }

        for (int s = 0; s < dialogues.arraySize; s++)
        {
            var entry = dialogues.GetArrayElementAtIndex(s);
            var stateProp = entry.FindPropertyRelative("State");
            var linesProp = entry.FindPropertyRelative("Dialogues");
            string stateName = stateProp.enumDisplayNames[stateProp.enumValueIndex];

            EditorGUILayout.BeginVertical("box");
            EditorGUILayout.LabelField(stateName, EditorStyles.boldLabel);

            for (int l = 0; l < linesProp.arraySize; l++)
            {
                EditorGUILayout.BeginHorizontal();
                linesProp.GetArrayElementAtIndex(l).stringValue =
                    EditorGUILayout.TextField(linesProp.GetArrayElementAtIndex(l).stringValue);
                if (GUILayout.Button("-", GUILayout.Width(20)))
                {
                    linesProp.DeleteArrayElementAtIndex(l);
                    break;
                }
                EditorGUILayout.EndHorizontal();
            }

            if (GUILayout.Button("+ Add Line", EditorStyles.miniButton))
                linesProp.InsertArrayElementAtIndex(linesProp.arraySize);

            EditorGUILayout.EndVertical();
        }
    }

    void DrawLocalizedDialogues(NPCData data)
    {
        var locProp = serializedObject.FindProperty("LocalizedDialogues");

        // Find or create entry for selected language
        int langIdx = -1;
        for (int i = 0; i < locProp.arraySize; i++)
        {
            var lang = locProp.GetArrayElementAtIndex(i).FindPropertyRelative("Lang");
            if (lang.enumValueIndex == (int)_selectedLang)
            {
                langIdx = i;
                break;
            }
        }

        if (langIdx == -1)
        {
            if (GUILayout.Button($"Create {_selectedLang} dialogues"))
            {
                langIdx = locProp.arraySize;
                locProp.InsertArrayElementAtIndex(langIdx);
                var newEntry = locProp.GetArrayElementAtIndex(langIdx);
                newEntry.FindPropertyRelative("Lang").enumValueIndex = (int)_selectedLang;

                // Copy structure from EN
                var enDialogues = serializedObject.FindProperty("StateDialogues");
                var newDialogues = newEntry.FindPropertyRelative("StateDialogues");
                newDialogues.arraySize = enDialogues.arraySize;
                for (int i = 0; i < enDialogues.arraySize; i++)
                {
                    var src = enDialogues.GetArrayElementAtIndex(i);
                    var dst = newDialogues.GetArrayElementAtIndex(i);
                    dst.FindPropertyRelative("State").enumValueIndex =
                        src.FindPropertyRelative("State").enumValueIndex;
                    dst.FindPropertyRelative("AnimationIndex").intValue =
                        src.FindPropertyRelative("AnimationIndex").intValue;
                    dst.FindPropertyRelative("Dialogues").arraySize = 0;
                }

                serializedObject.ApplyModifiedProperties();
            }
            return;
        }

        var entry = locProp.GetArrayElementAtIndex(langIdx);
        var stateDialogues = entry.FindPropertyRelative("StateDialogues");
        DrawStateDialogues(stateDialogues);

        EditorGUILayout.Space(4);
        if (GUILayout.Button($"Delete {_selectedLang} dialogues", EditorStyles.miniButton))
        {
            locProp.DeleteArrayElementAtIndex(langIdx);
            serializedObject.ApplyModifiedProperties();
        }
    }

    void DrawLine()
    {
        var rect = EditorGUILayout.GetControlRect(false, 1);
        EditorGUI.DrawRect(rect, new Color(0.5f, 0.5f, 0.5f, 0.5f));
    }
}
