using UnityEngine;
using UnityEngine.UIElements;

[System.Serializable]
public class TownShopUIDebugger : MonoBehaviour
{
    [ContextMenu("Debug Shop UI State")]
    public void DebugShopUIState()
    {
        var shopUIs = FindObjectsByType<TownShopUI>(FindObjectsSortMode.None);
        Debug.Log($"Found {shopUIs.Length} TownShopUI instances");

        foreach (var shopUI in shopUIs)
        {
            Debug.Log($"=== TownShopUI: {shopUI.gameObject.name} ===");

            var uiDoc = shopUI.GetComponent<UIDocument>();
            if (uiDoc != null)
            {
                Debug.Log($"UIDocument found - enabled: {uiDoc.enabled}");
                Debug.Log($"PanelSettings: {uiDoc.panelSettings != null}");
                Debug.Log($"SortingOrder: {uiDoc.sortingOrder}");
                Debug.Log($"VisualTreeAsset: {uiDoc.visualTreeAsset != null}");

                if (uiDoc.rootVisualElement != null)
                {
                    var root = uiDoc.rootVisualElement;
                    Debug.Log($"Root element - visible: {root.visible}, display: {root.style.display.value}, position: {root.style.position.value}");
                    Debug.Log($"Root worldBound: {root.worldBound}");

                    var shopContainer = root.Q<VisualElement>("shop-container");
                    if (shopContainer != null)
                    {
                        Debug.Log($"Shop container - visible: {shopContainer.visible}, display: {shopContainer.style.display.value}");
                        Debug.Log($"Shop container worldBound: {shopContainer.worldBound}");
                    }
                    else
                    {
                        Debug.LogError("shop-container element not found!");
                    }
                }
                else
                {
                    Debug.LogError("Root visual element is null!");
                }
            }
            else
            {
                Debug.LogError("No UIDocument component found!");
            }
        }

        // Check all UIDocuments in scene
        var allUIDocuments = FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
        Debug.Log($"\n=== All UIDocuments in scene ({allUIDocuments.Length}) ===");
        foreach (var doc in allUIDocuments)
        {
            var panelSort = doc.panelSettings?.sortingOrder ?? -999;
            Debug.Log($"UIDocument '{doc.gameObject.name}' - sortingOrder: {doc.sortingOrder}, PanelSettings sortingOrder: {panelSort}");
        }
    }

    [ContextMenu("Force Open Test Shop")]
    public void ForceOpenTestShop()
    {
        // Create a test shop
        var testShop = new TownShop
        {
            buildingName = "Test Shop",
            shopkeeperName = "Test Keeper",
            shopType = ShopType.General
        };

        var townManager = FindFirstObjectByType<TownManager>();
        if (townManager != null)
        {
            townManager.OnBuildingClicked(testShop);
        }
        else
        {
            Debug.LogError("TownManager not found!");
        }
    }
}
