using UnityEngine;

/// <summary>
/// Enhanced debug helper for testing the click management system specifically for ActiveQuestUI.
/// This provides detailed debugging information to help troubleshoot click blocking issues.
/// </summary>
public class ActiveQuestUIDebugger : MonoBehaviour
{
    [Header("Debug Settings")]
    public bool enableDebugMode = true;
    public bool showClickDetails = true;
    public bool showUIBounds = true;
    public KeyCode testKey = KeyCode.F9;

    private ActiveQuestUI activeQuestUI;

    void Start()
    {
        activeQuestUI = FindFirstObjectByType<ActiveQuestUI>();
        if (activeQuestUI == null)
        {
            Debug.LogWarning("ActiveQuestUIDebugger: No ActiveQuestUI found in scene!");
        }

        // Enable ClickManager debug logs
        if (ClickManager.Instance != null)
        {
            ClickManager.Instance.showDebugLogs = enableDebugMode;
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(testKey))
        {
            TestActiveQuestUIBlocking();
        }

        if (Input.GetMouseButtonDown(0) && showClickDetails)
        {
            TestSpecificClick();
        }
    }

    void TestActiveQuestUIBlocking()
    {
        Debug.Log("=== ActiveQuestUI Debug Test ===");

        if (activeQuestUI == null)
        {
            Debug.LogError("❌ ActiveQuestUI not found!");
            return;
        }

        Debug.Log($"✅ ActiveQuestUI found");
        Debug.Log($"📊 IsVisible: {activeQuestUI.IsVisible}");

        // Test ClickManager registration
        if (ClickManager.Instance != null)
        {
            Debug.Log($"🔧 ClickManager blockers count: {ClickManager.Instance.GetRegisteredBlockersCount()}");
            ClickManager.Instance.ListRegisteredBlockers();
        }
        else
        {
            Debug.LogError("❌ ClickManager not available!");
        }

        // Test a specific screen position (center of screen)
        Vector2 centerScreen = new Vector2(Screen.width / 2f, Screen.height / 2f);
        bool wouldBlock = activeQuestUI.IsBlockingClick(centerScreen);
        Debug.Log($"🎯 Would block center screen click: {wouldBlock}");
    }

    void TestSpecificClick()
    {
        Vector2 mousePos = Input.mousePosition;

        Debug.Log($"=== Click Test at {mousePos} ===");

        if (activeQuestUI != null)
        {
            bool questUIBlocks = activeQuestUI.IsBlockingClick(mousePos);
            Debug.Log($"📝 ActiveQuestUI blocks: {questUIBlocks}");
            Debug.Log($"📊 ActiveQuestUI.IsVisible: {activeQuestUI.IsVisible}");
        }

        if (ClickManager.Instance != null)
        {
            bool anyBlocks = ClickManager.Instance.IsClickBlocked(mousePos);
            Debug.Log($"🔒 Any UI blocks: {anyBlocks}");
        }
    }

    void OnGUI()
    {
        if (!showUIBounds || activeQuestUI == null) return;

        // Draw UI bounds visualization
        GUI.color = Color.red;
        GUI.Label(new Rect(10, 10, 400, 20), $"ActiveQuestUI Debug - Visible: {activeQuestUI.IsVisible}");
        GUI.Label(new Rect(10, 30, 400, 20), $"ClickManager Blockers: {ClickManager.Instance?.GetRegisteredBlockersCount() ?? 0}");
        GUI.Label(new Rect(10, 50, 400, 20), $"Press {testKey} to run detailed test");
        GUI.color = Color.white;
    }
}
