using UnityEngine;
using UnityEngine.UIElements;

/// <summary>
/// Helper script to automatically assign the correct UXML file to the UI GameObject
/// and verify the travel UI setup.
/// </summary>
[System.Serializable]
public class MapUISetupHelper : MonoBehaviour
{
    [Header("UI Setup")]
    [SerializeField] private VisualTreeAsset mapWithTravelUITemplate;

    [Header("Debug Info")]
    [SerializeField] private bool showDebugInfo = true;

    void Start()
    {
        SetupUI();
    }

    [ContextMenu("Setup UI")]
    public void SetupUI()
    {
        // Find the UI GameObject
        GameObject uiGameObject = GameObject.Find("UI");
        if (uiGameObject == null)
        {
            Debug.LogError("UI GameObject not found!");
            return;
        }

        // Get the UIDocument component
        UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
        if (uiDocument == null)
        {
            Debug.LogError("UIDocument component not found on UI GameObject!");
            return;
        }

        // Try to load the UXML file if not assigned in inspector
        if (mapWithTravelUITemplate == null)
        {
            mapWithTravelUITemplate = Resources.Load<VisualTreeAsset>("UI/Map/MapWithTravelUI");
            if (mapWithTravelUITemplate == null)
            {
                // Try direct path
                mapWithTravelUITemplate = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/Map/MapWithTravelUI.uxml");
            }
        }

        // Assign the UXML template
        if (mapWithTravelUITemplate != null)
        {
            uiDocument.visualTreeAsset = mapWithTravelUITemplate;
            Debug.Log("Successfully assigned MapWithTravelUI.uxml to UI GameObject!");

            if (showDebugInfo)
            {
                VerifyUIElements();
            }
        }
        else
        {
            Debug.LogError("Could not find MapWithTravelUI.uxml! Please assign it manually in the inspector.");
        }
    }

    [ContextMenu("Verify UI Elements")]
    public void VerifyUIElements()
    {
        GameObject uiGameObject = GameObject.Find("UI");
        if (uiGameObject == null) return;

        UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
        if (uiDocument == null || uiDocument.rootVisualElement == null) return;

        var root = uiDocument.rootVisualElement;

        Debug.Log("=== UI ELEMENT VERIFICATION ===");
        Debug.Log($"Root element exists: {root != null}");

        // Check MapLegend
        var mapLegend = root.Q("MapLegend");
        Debug.Log($"MapLegend found: {mapLegend != null}");

        // Check TravelContainer and its children
        var travelContainer = root.Q("TravelContainer");
        Debug.Log($"TravelContainer found: {travelContainer != null}");

        if (travelContainer != null)
        {
            var travelPanel = root.Q("TravelPanel");
            var travelHeader = root.Q("TravelHeader");
            var closeButton = root.Q<Button>("CloseButton");
            var distanceLabel = root.Q<Label>("DistanceLabel");
            var timeLabel = root.Q<Label>("TimeLabel");
            var specialCostsLabel = root.Q<Label>("SpecialCostsLabel");
            var startTravelButton = root.Q<Button>("StartTravelButton");
            var cancelTravelButton = root.Q<Button>("CancelTravelButton");
            var avoidTunnelsToggle = root.Q<Toggle>("AvoidTunnelsToggle");
            var avoidFerriesToggle = root.Q<Toggle>("AvoidFerriesToggle");
            var alternativeRouteButton = root.Q<Button>("AlternativeRouteButton");

            Debug.Log($"TravelPanel: {travelPanel != null}");
            Debug.Log($"TravelHeader: {travelHeader != null}");
            Debug.Log($"CloseButton: {closeButton != null}");
            Debug.Log($"DistanceLabel: {distanceLabel != null}");
            Debug.Log($"TimeLabel: {timeLabel != null}");
            Debug.Log($"SpecialCostsLabel: {specialCostsLabel != null}");
            Debug.Log($"StartTravelButton: {startTravelButton != null}");
            Debug.Log($"CancelTravelButton: {cancelTravelButton != null}");
            Debug.Log($"AvoidTunnelsToggle: {avoidTunnelsToggle != null}");
            Debug.Log($"AvoidFerriesToggle: {avoidFerriesToggle != null}");
            Debug.Log($"AlternativeRouteButton: {alternativeRouteButton != null}");

            // Check if TravelContainer is hidden (which is correct)
            var displayStyle = travelContainer.style.display;
            Debug.Log($"TravelContainer display style: {displayStyle.value}");
            Debug.Log($"TravelContainer should be hidden by default: {displayStyle.value == DisplayStyle.None}");
        }

        Debug.Log("=== END UI ELEMENT VERIFICATION ===");
    }

    [ContextMenu("Show Travel UI (Test)")]
    public void ShowTravelUITest()
    {
        GameObject uiGameObject = GameObject.Find("UI");
        if (uiGameObject == null) return;

        UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
        if (uiDocument == null || uiDocument.rootVisualElement == null) return;

        var travelContainer = uiDocument.rootVisualElement.Q("TravelContainer");
        if (travelContainer != null)
        {
            travelContainer.style.display = DisplayStyle.Flex;
            Debug.Log("Travel UI forced to show for testing");

            // Set some test data
            var distanceLabel = uiDocument.rootVisualElement.Q<Label>("DistanceLabel");
            var timeLabel = uiDocument.rootVisualElement.Q<Label>("TimeLabel");
            var specialCostsLabel = uiDocument.rootVisualElement.Q<Label>("SpecialCostsLabel");
            var startButton = uiDocument.rootVisualElement.Q<Button>("StartTravelButton");

            if (distanceLabel != null) distanceLabel.text = "Distance: 12.5 leagues";
            if (timeLabel != null) timeLabel.text = "Travel Time: 8.3 hours";
            if (specialCostsLabel != null) specialCostsLabel.text = "Special Costs: 15 gold for tunnel passage";
            if (startButton != null) startButton.text = "Start Journey (15 gold)";
        }
        else
        {
            Debug.LogError("TravelContainer not found!");
        }
    }

    [ContextMenu("Hide Travel UI")]
    public void HideTravelUITest()
    {
        GameObject uiGameObject = GameObject.Find("UI");
        if (uiGameObject == null) return;

        UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
        if (uiDocument == null || uiDocument.rootVisualElement == null) return;

        var travelContainer = uiDocument.rootVisualElement.Q("TravelContainer");
        if (travelContainer != null)
        {
            travelContainer.style.display = DisplayStyle.None;
            Debug.Log("Travel UI hidden");
        }
    }
}
