#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PotionTextureSetup))]
public class PotionTextureSetupEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PotionTextureSetup script = (PotionTextureSetup)target;

        DrawDefaultInspector();

        EditorGUILayout.Space(15);

        Renderer rend = script.GetComponent<Renderer>();
        int materialCount = rend != null ? rend.sharedMaterials.Length : 0;

        if (materialCount > 0)
        {
            bool isPrepared = false;
            if (rend != null)
            {
                Material[] sharedMats = rend.sharedMaterials;
                if (sharedMats != null && sharedMats.Length > 0)
                {
                    foreach (var mat in sharedMats)
                    {
                        if (mat != null && mat.name.Contains("(Instance)"))
                        {
                            isPrepared = true;
                            break;
                        }
                    }
                }
            }

            EditorGUILayout.LabelField("Timeline Animation Setup", EditorStyles.boldLabel);

            if (!isPrepared)
            {
                EditorGUILayout.HelpBox(
                    "To animate material properties in Timeline, you need to prepare this object first.\n\n" +
                    "This creates material instances that allow:\n" +
                    "• Timeline animations to work properly\n" +
                    "• Each object to have independent grayscale textures\n" +
                    "• Properties to animate without affecting other objects",
                    MessageType.Info
                );

                GUI.backgroundColor = new Color(0.3f, 0.8f, 0.3f);
                if (GUILayout.Button("🎬 Prepare for Timeline", GUILayout.Height(40)))
                {
                    script.PrepareForTimeline();
                }
                GUI.backgroundColor = Color.white;
            }
            else
            {
                string helpText = "✓ Timeline-ready! Material instances created.\n\n";
                helpText += "Animate in Timeline:\n";
                helpText += "• All properties via: Renderer > Material > [Property]\n";
                helpText += "• Colors, emission, smoothness, etc.\n\n";

                if (materialCount >= 2)
                {
                    helpText += "⚠️ For _GradientIndex:\n";
                    helpText += "ADD MaterialIndexController component (REQUIRED)\n";
                    helpText += "Without it, all materials will animate together.\n\n";
                }
                else if (materialCount == 1)
                {
                    helpText += "For _GradientIndex:\n";
                    helpText += "• Animate directly via Renderer > Material, OR\n";
                    helpText += "• Add MaterialIndexController for a cleaner slider (optional)\n\n";
                }

                helpText += "💡 Tip: Timeline preview in Edit Mode can be unreliable.\n";
                helpText += "Always test in Play Mode for accurate animation results.";

                EditorGUILayout.HelpBox(helpText, MessageType.None);

                EditorGUILayout.Space(5);

                // Undo button
                GUI.backgroundColor = new Color(0.9f, 0.5f, 0.3f);
                if (GUILayout.Button("↩ Remove Instances (Restore Originals)", GUILayout.Height(30)))
                {
                    if (EditorUtility.DisplayDialog(
                        "Remove Material Instances?",
                        "This will restore the original shared materials.\n\nTimeline animations will no longer work until you prepare again.",
                        "Remove",
                        "Cancel"))
                    {
                        script.RemoveInstances();
                    }
                }
                GUI.backgroundColor = Color.white;
            }
        }
    }
}
#endif