#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using IStalk.Core;
using IStalk.Data;

namespace IStalk.Editor.Inspectors
{
    [CustomEditor(typeof(ExtractionRoom))]
    public class ExtractionRoomEditor : UnityEditor.Editor
    {
        private ExtractionRoom _room;

        private void OnEnable()
        {
            _room = (ExtractionRoom)target;
        }

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            EditorGUILayout.Space(15);
            EditorGUILayout.LabelField("Extraction Room Setup", EditorStyles.boldLabel);

            var rules = FindRules();
            if (rules != null)
            {
                EditorGUILayout.LabelField($"Channel Duration: {rules.extractionChannelDuration:F0}s");
                EditorGUILayout.LabelField($"Anti-AFK Ritual: {rules.antiAfkRitualDuration:F0}s (min {rules.antiAfkMinVoters} voters)");
            }

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("Design Notes", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox(
                "Extraction Room Rules:\n" +
                "• All alive runners must be inside simultaneously\n" +
                "• Timer pauses/resets if anyone leaves\n" +
                "• Downed runners are ignored (not required)\n" +
                "• Room should be dense with IncarnationProp objects\n" +
                "• Place a Trigger collider covering the entire room area",
                MessageType.Info);

            EditorGUILayout.Space(5);
            DrawSetupValidator();

            if (Application.isPlaying && _room.IsSpawned)
            {
                EditorGUILayout.Space(10);
                EditorGUILayout.LabelField("Runtime State", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Unlocked: {_room.IsUnlocked}");
                EditorGUILayout.LabelField($"  Timer: {_room.Timer:F1}s");
                EditorGUILayout.LabelField($"  Runners Inside: {_room.RunnersInside}");
                Repaint();
            }
        }

        private void DrawSetupValidator()
        {
            EditorGUILayout.LabelField("Setup Checklist", EditorStyles.boldLabel);

            var trigger = _room.GetComponent<Collider>();
            if (trigger == null)
            {
                trigger = _room.GetComponentInChildren<Collider>();
            }

            if (trigger == null)
                EditorGUILayout.HelpBox("No Collider found! Add a trigger collider covering the room.", MessageType.Error);
            else if (!trigger.isTrigger)
                EditorGUILayout.HelpBox("Collider is not set as Trigger. Enable 'Is Trigger'.", MessageType.Error);
            else
                EditorGUILayout.LabelField("  ✓ Trigger collider present");

            int incarnationCount = _room.GetComponentsInChildren<MonoBehaviour>().Length;
            var incarnationProps = _room.transform.GetComponentsInChildren<Transform>();
            int propCount = 0;
            foreach (var t in incarnationProps)
            {
                if (t.GetComponent<FishNet.Object.NetworkObject>() != null && t != _room.transform)
                    propCount++;
            }

            if (propCount == 0)
                EditorGUILayout.HelpBox("No IncarnationProp children found. Add props for the Eye to incarnate during extraction.", MessageType.Warning);
            else
                EditorGUILayout.LabelField($"  ✓ {propCount} networked prop(s) in room");
        }

        private GlobalRules_SO FindRules()
        {
            if (GlobalRules_SO.Instance != null) return GlobalRules_SO.Instance;
            string[] guids = AssetDatabase.FindAssets("t:GlobalRules_SO");
            if (guids.Length == 0) return null;
            return AssetDatabase.LoadAssetAtPath<GlobalRules_SO>(AssetDatabase.GUIDToAssetPath(guids[0]));
        }
    }
}
#endif
