#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Linq;
using IStalk.Data;
using IStalk.EditorTools.MapGen;

namespace IStalk.Editor.MapGen
{
    [CustomEditor(typeof(RoomNode_SO))]
    public class RoomNodeEditor : UnityEditor.Editor
    {
        private RoomNode_SO _room;
        private bool _showValidation = true;

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

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("Room Tools", EditorStyles.boldLabel);

            if (GUILayout.Button("Auto-Populate from Prefab"))
                AutoPopulate();

            if (GUILayout.Button("Validate Room"))
                ValidateRoom();

            if (_showValidation)
                DrawValidationResults();
        }

        private void AutoPopulate()
        {
            if (_room.roomPrefab == null)
            {
                EditorUtility.DisplayDialog("Error", "Assign a room prefab first.", "OK");
                return;
            }

            Undo.RecordObject(_room, "Auto-Populate Room Node");

            var prefab = _room.roomPrefab;

            var connectors = prefab.GetComponentsInChildren<ConnectorMarker>(true);
            _room.openConnections.Clear();
            foreach (var c in connectors)
            {
                if (!_room.openConnections.Contains(c.connectionDirection))
                    _room.openConnections.Add(c.connectionDirection);
            }

            var bounds = prefab.GetComponentInChildren<RoomBounds>(true);
            if (bounds != null)
                _room.gridSize = bounds.gridSize;

            var slots = prefab.GetComponentsInChildren<SpawnSlot>(true);
            _room.lootSpawnSlots = slots.Count(s => s.slotType == SpawnSlotType.LootItem);
            _room.incarnationPropSlots = slots.Count(s => s.slotType == SpawnSlotType.PropIncarnation);
            _room.isValidRunnerSpawn = slots.Any(s => s.slotType == SpawnSlotType.RunnerSpawn);
            _room.isValidAltarLocation = slots.Any(s => s.slotType == SpawnSlotType.AltarLocation);
            _room.canContainShrine = slots.Any(s => s.slotType == SpawnSlotType.Shrine);

            EditorUtility.SetDirty(_room);
            Debug.Log($"[RoomNodeEditor] Auto-populated: {connectors.Length} connectors, {slots.Length} slots, grid {_room.gridSize}");
        }

        private void ValidateRoom()
        {
            _showValidation = true;
            Repaint();
        }

        private void DrawValidationResults()
        {
            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Validation", EditorStyles.boldLabel);

            bool valid = true;

            if (string.IsNullOrEmpty(_room.roomId))
            {
                EditorGUILayout.HelpBox("Room ID is empty.", MessageType.Error);
                valid = false;
            }

            if (_room.roomPrefab == null)
            {
                EditorGUILayout.HelpBox("No prefab assigned.", MessageType.Error);
                valid = false;
            }

            if (_room.openConnections.Count == 0)
            {
                EditorGUILayout.HelpBox("No connections defined — room will be isolated.", MessageType.Warning);
            }

            if (_room.gridSize.x <= 0 || _room.gridSize.y <= 0 || _room.gridSize.z <= 0)
            {
                EditorGUILayout.HelpBox("Grid size has zero or negative dimension.", MessageType.Error);
                valid = false;
            }

            if (_room.spawnWeight <= 0)
            {
                EditorGUILayout.HelpBox("Spawn weight is 0 — room will never appear.", MessageType.Warning);
            }

            if (valid)
            {
                EditorGUILayout.HelpBox("Room passes all checks.", MessageType.Info);
            }
        }
    }
}
#endif
