using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using gay.lilyy.MenuIconReplacer;

namespace gay.lilyy.MenuIconReplacer.Editor
{
    [CustomEditor(typeof(MenuIconReplacerConfig))]
    public class MenuIconReplacerInspector : UnityEditor.Editor
    {
        private SerializedProperty replacementsProperty;
        private bool showReplacements = true;

        private void OnEnable()
        {
            replacementsProperty = serializedObject.FindProperty("replacements");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Menu Icon Replacer", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("Configure texture replacements for menu icons. The left texture will be replaced with the right texture in the expressions menu.", MessageType.Info);
            
            EditorGUILayout.Space();

            showReplacements = EditorGUILayout.Foldout(showReplacements, $"Replacements ({replacementsProperty.arraySize})", true);
            
            if (showReplacements)
            {
                EditorGUI.indentLevel++;
                
                // Add new replacement button
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Add Replacement"))
                {
                    replacementsProperty.InsertArrayElementAtIndex(replacementsProperty.arraySize);
                }
                
                if (GUILayout.Button("Clear All") && replacementsProperty.arraySize > 0)
                {
                    if (EditorUtility.DisplayDialog("Clear All Replacements", 
                        "Are you sure you want to clear all replacements?", "Yes", "Cancel"))
                    {
                        replacementsProperty.ClearArray();
                    }
                }
                EditorGUILayout.EndHorizontal();
                
                EditorGUILayout.Space();

                // Display replacements
                for (int i = 0; i < replacementsProperty.arraySize; i++)
                {
                    var element = replacementsProperty.GetArrayElementAtIndex(i);
                    var fromProperty = element.FindPropertyRelative("from");
                    var toProperty = element.FindPropertyRelative("to");
                    
                    EditorGUILayout.BeginVertical("box");
                    
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField($"Replacement {i + 1}", EditorStyles.boldLabel, GUILayout.Width(100));
                    
                    if (GUILayout.Button("Remove", GUILayout.Width(60)))
                    {
                        replacementsProperty.DeleteArrayElementAtIndex(i);
                        break;
                    }
                    EditorGUILayout.EndHorizontal();
                    
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("From:", GUILayout.Width(40));
                    EditorGUILayout.PropertyField(fromProperty, GUIContent.none);
                    EditorGUILayout.EndHorizontal();
                    
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("To:", GUILayout.Width(40));
                    EditorGUILayout.PropertyField(toProperty, GUIContent.none);
                    EditorGUILayout.EndHorizontal();
                    
                    // Validation
                    var fromTexture = fromProperty.objectReferenceValue as Texture2D;
                    var toTexture = toProperty.objectReferenceValue as Texture2D;
                    
                    if (fromTexture != null && toTexture != null && fromTexture == toTexture)
                    {
                        EditorGUILayout.HelpBox("Warning: Source and target textures are the same.", MessageType.Warning);
                    }
                    else if (fromTexture != null && toTexture == null)
                    {
                        EditorGUILayout.HelpBox("Target texture is not set.", MessageType.Warning);
                    }
                    else if (fromTexture == null && toTexture != null)
                    {
                        EditorGUILayout.HelpBox("Source texture is not set.", MessageType.Warning);
                    }
                    
                    EditorGUILayout.EndVertical();
                    EditorGUILayout.Space();
                }
                
                EditorGUI.indentLevel--;
            }

            serializedObject.ApplyModifiedProperties();
        }

    }
}
