using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;
using EnhancedTouchSupport = UnityEngine.InputSystem.EnhancedTouch.EnhancedTouchSupport;
using EnhancedTouchTouch = UnityEngine.InputSystem.EnhancedTouch.Touch;
using EnhancedTouchPhase = UnityEngine.InputSystem.TouchPhase;

// Attach this to any active GameObject in the scene to log what UI elements
// are hit when you click/tap. Helps diagnose why a button isn't firing.
public class UIRaycastInspector : MonoBehaviour
{
    private PointerEventData _eventData;
    private List<RaycastResult> _results = new List<RaycastResult>();

    void Awake()
    {
        if (!EventSystem.current)
        {
            Debug.LogWarning("[UIRaycastInspector] No EventSystem present.");
        }
    }

    void OnEnable()
    {
        // Ensure EnhancedTouch is active so we can read Touch.activeTouches.
        EnhancedTouchSupport.Enable();
    }

    void OnDisable()
    {
        EnhancedTouchSupport.Disable();
    }

    void Update()
    {
        // Prefer the Input System mouse/touch events if available.
        if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
        {
            DiagnosePointer(Mouse.current.position.ReadValue(), "MouseDown");
        }

        if (EnhancedTouchSupport.enabled && EnhancedTouchTouch.activeTouches.Count > 0)
        {
            var touches = EnhancedTouchTouch.activeTouches;
            for (int i = 0; i < touches.Count; ++i)
            {
                var t = touches[i];
                if (!t.valid)
                    continue;

                if (t.phase == EnhancedTouchPhase.Began)
                {
                    DiagnosePointer(t.screenPosition, "TouchBegan");
                    break;
                }
            }
        }
        else
        {
                // Legacy fallback for platforms not using the Input System yet.
                if (Input.touchCount > 0 && Input.touches[0].phase == UnityEngine.TouchPhase.Began)
            {
                DiagnosePointer(Input.touches[0].position, "TouchBegan");
            }
        }
    }

    private void DiagnosePointer(Vector2 screenPos, string source)
    {
        if (!EventSystem.current) return;
        _results.Clear();
        _eventData = new PointerEventData(EventSystem.current) { position = screenPos };
        EventSystem.current.RaycastAll(_eventData, _results);

        if (_results.Count == 0)
        {
            Debug.Log($"[UIRaycastInspector] {source} at {screenPos} hit NOTHING (no UI)." );
            return;
        }

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append($"[UIRaycastInspector] {source} at {screenPos} hits (top->bottom): ");
        for (int i = 0; i < _results.Count; i++)
        {
            var r = _results[i];
            sb.Append(r.gameObject.name);
            if (i < _results.Count - 1) sb.Append(" -> ");
        }
        Debug.Log(sb.ToString());
    }
}