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

namespace IStalk.Editor
{
    public static class IStalkMenuItems
    {
        [MenuItem("IStalk/Create/Room Prefab Setup", false, 100)]
        public static void CreateRoomPrefab()
        {
            var root = new GameObject("Room_NewRoom");
            root.AddComponent<RoomBounds>();

            var connLeft = new GameObject("Connector_Left");
            connLeft.transform.parent = root.transform;
            connLeft.transform.localPosition = new Vector3(-2, 1.5f, 0);
            var ml = connLeft.AddComponent<ConnectorMarker>();
            ml.connectionDirection = ConnectionTag.Left;

            var connRight = new GameObject("Connector_Right");
            connRight.transform.parent = root.transform;
            connRight.transform.localPosition = new Vector3(2, 1.5f, 0);
            var mr = connRight.AddComponent<ConnectorMarker>();
            mr.connectionDirection = ConnectionTag.Right;

            var lootSlot = new GameObject("Slot_Loot");
            lootSlot.transform.parent = root.transform;
            lootSlot.transform.localPosition = new Vector3(0, 0.5f, 0);
            var slot = lootSlot.AddComponent<SpawnSlot>();
            slot.slotType = SpawnSlotType.LootItem;

            Selection.activeGameObject = root;
            Undo.RegisterCreatedObjectUndo(root, "Create Room Prefab");

            EditorUtility.DisplayDialog("Room Created",
                "Room prefab structure created in hierarchy.\n\n" +
                "1. Add your room geometry as children\n" +
                "2. Add more ConnectorMarker and SpawnSlot components\n" +
                "3. Drag to Prefabs/Rooms to create the prefab\n" +
                "4. Create a RoomNode_SO and use 'Auto-Populate from Prefab'",
                "OK");
        }

        [MenuItem("IStalk/Create/Loot Item SO", false, 200)]
        public static void CreateLootItem()
        {
            var item = ScriptableObject.CreateInstance<ItemData_SO>();
            ProjectWindowUtil.CreateAsset(item, "Assets/Resources/Items/Loot/Item_New.asset");
        }

        [MenuItem("IStalk/Create/Shrine Item SO", false, 201)]
        public static void CreateShrineItem()
        {
            var item = ScriptableObject.CreateInstance<ShrineItemData_SO>();
            ProjectWindowUtil.CreateAsset(item, "Assets/Resources/Items/Shrine/ShrineItem_New.asset");
        }

        [MenuItem("IStalk/Create/Trap SO", false, 202)]
        public static void CreateTrap()
        {
            var item = ScriptableObject.CreateInstance<TrapData_SO>();
            ProjectWindowUtil.CreateAsset(item, "Assets/Resources/Items/Traps/Trap_New.asset");
        }

        [MenuItem("IStalk/Create/Room Node SO", false, 203)]
        public static void CreateRoomNode()
        {
            var item = ScriptableObject.CreateInstance<RoomNode_SO>();
            ProjectWindowUtil.CreateAsset(item, "Assets/Resources/Items/Rooms/Room_New.asset");
        }

        [MenuItem("IStalk/Create/Global Rules SO", false, 300)]
        public static void CreateGlobalRules()
        {
            string[] existing = AssetDatabase.FindAssets("t:GlobalRules_SO");
            if (existing.Length > 0)
            {
                if (!EditorUtility.DisplayDialog("GlobalRules exists",
                    "A GlobalRules_SO already exists. Create another?", "Yes", "Cancel"))
                    return;
            }

            var rules = ScriptableObject.CreateInstance<GlobalRules_SO>();
            ProjectWindowUtil.CreateAsset(rules, "Assets/Resources/GlobalRules.asset");
        }

        [MenuItem("IStalk/Quick Setup/Create All Resource Folders", false, 400)]
        public static void CreateResourceFolders()
        {
            string[] folders = {
                "Assets/Resources/Items/Loot",
                "Assets/Resources/Items/Shrine",
                "Assets/Resources/Items/Traps",
                "Assets/Resources/Items/Rooms",
                "Assets/Prefabs/Player",
                "Assets/Prefabs/Props",
                "Assets/Prefabs/UI",
                "Assets/Prefabs/Rooms",
                "Assets/Scenes"
            };

            foreach (var folder in folders)
            {
                if (!AssetDatabase.IsValidFolder(folder))
                {
                    string[] parts = folder.Split('/');
                    string current = parts[0];
                    for (int i = 1; i < parts.Length; i++)
                    {
                        string next = current + "/" + parts[i];
                        if (!AssetDatabase.IsValidFolder(next))
                            AssetDatabase.CreateFolder(current, parts[i]);
                        current = next;
                    }
                }
            }

            AssetDatabase.Refresh();
            Debug.Log("[IStalk] All resource folders created.");
        }

        [MenuItem("IStalk/Quick Setup/Setup Physics Layers", false, 401)]
        public static void SetupPhysicsLayers()
        {
            EditorUtility.DisplayDialog("Physics Layers",
                "Set these layers in Project Settings > Tags and Layers:\n\n" +
                "Layer 6: Wall\n" +
                "Layer 7: Prop\n" +
                "Layer 8: Runner\n" +
                "Layer 9: Eye\n" +
                "Layer 10: SmokeVolume\n" +
                "Layer 11: GrappleAnchor\n" +
                "Layer 12: Trigger\n" +
                "Layer 13: EyeBeam",
                "OK");
        }
    }
}
#endif
