#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/Eye Type SO", false, 210)]
        public static void CreateEyeType()
        {
            var item = ScriptableObject.CreateInstance<EyeType_SO>();
            ProjectWindowUtil.CreateAsset(item, "Assets/Resources/EyeTypes/EyeType_New.asset");
        }

        [MenuItem("IStalk/Create/Wearable SO", false, 211)]
        public static void CreateWearable()
        {
            var item = ScriptableObject.CreateInstance<WearableData_SO>();
            ProjectWindowUtil.CreateAsset(item, "Assets/Resources/Items/Wearables/Wearable_New.asset");
        }

        [MenuItem("IStalk/Create/Extraction Room Prefab Setup", false, 110)]
        public static void CreateExtractionRoomPrefab()
        {
            var root = new GameObject("ExtractionRoom");

            var boxCol = root.AddComponent<BoxCollider>();
            boxCol.isTrigger = true;
            boxCol.size = new Vector3(15, 4, 15);
            boxCol.center = new Vector3(0, 2, 0);

            for (int i = 0; i < 6; i++)
            {
                var prop = new GameObject($"IncarnationProp_{i + 1}");
                prop.transform.parent = root.transform;
                float angle = i * 60f * Mathf.Deg2Rad;
                prop.transform.localPosition = new Vector3(Mathf.Cos(angle) * 4f, 0.5f, Mathf.Sin(angle) * 4f);
                var slot = prop.AddComponent<SpawnSlot>();
                slot.slotType = SpawnSlotType.PropIncarnation;
            }

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

            EditorUtility.DisplayDialog("Extraction Room Created",
                "Extraction Room prefab structure created.\n\n" +
                "1. Add room geometry (floor, walls, ceiling)\n" +
                "2. Replace SpawnSlot children with actual IncarnationProp prefabs\n" +
                "3. Adjust BoxCollider to fit your room size\n" +
                "4. Add NetworkObject + ExtractionRoom.cs + AntiAfkRitual.cs\n" +
                "5. Save as prefab in Assets/Prefabs/Props/\n" +
                "6. Register in FishNet DefaultPrefabObjects",
                "OK");
        }

        [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/Resources/Items/Wearables",
                "Assets/Resources/EyeTypes",
                "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
