using UnityEngine;
using UnityEngine.UIElements;

public class TooltipSystem : MonoBehaviour
{
    [Header("UI References")]
    public UIDocument uiDocument;

    [Header("Debug")]
    public bool debugMode = true;

    private VisualElement tooltipContainer;
    private Label tooltipLabel;
    private static TooltipSystem instance;
    private bool isSetup = false;

    public static TooltipSystem Instance => instance; void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    void Start()
    {
        if (debugMode) Debug.Log("TooltipSystem Start() called");
        SetupTooltip();
    }

    void SetupTooltip()
    {
        if (debugMode) Debug.Log("Setting up tooltip system...");

        if (uiDocument == null)
            uiDocument = GetComponent<UIDocument>();

        if (uiDocument == null)
        {
            Debug.LogError("UIDocument component not found on TooltipSystem!");
            return;
        }

        if (debugMode) Debug.Log("UIDocument found, creating tooltip UI...");

        // Create tooltip UI programmatically
        var root = uiDocument.rootVisualElement;

        tooltipContainer = new VisualElement();
        tooltipContainer.name = "tooltip-container";
        tooltipContainer.style.position = Position.Absolute;
        tooltipContainer.style.backgroundColor = new Color(0, 0, 0, 0.8f);
        tooltipContainer.style.borderTopWidth = 2;
        tooltipContainer.style.borderBottomWidth = 2;
        tooltipContainer.style.borderLeftWidth = 2;
        tooltipContainer.style.borderRightWidth = 2;
        tooltipContainer.style.borderTopColor = Color.white;
        tooltipContainer.style.borderBottomColor = Color.white;
        tooltipContainer.style.borderLeftColor = Color.white;
        tooltipContainer.style.borderRightColor = Color.white;
        tooltipContainer.style.borderTopLeftRadius = 5;
        tooltipContainer.style.borderTopRightRadius = 5;
        tooltipContainer.style.borderBottomLeftRadius = 5;
        tooltipContainer.style.borderBottomRightRadius = 5;
        tooltipContainer.style.paddingTop = 8;
        tooltipContainer.style.paddingBottom = 8;
        tooltipContainer.style.paddingLeft = 12;
        tooltipContainer.style.paddingRight = 12;
        tooltipContainer.style.display = DisplayStyle.None;

        tooltipLabel = new Label();
        tooltipLabel.style.color = Color.white;
        tooltipLabel.style.fontSize = 14;
        tooltipLabel.style.whiteSpace = WhiteSpace.NoWrap;

        tooltipContainer.Add(tooltipLabel);
        root.Add(tooltipContainer);

        isSetup = true;
        if (debugMode) Debug.Log("Tooltip system setup complete!");
    }

    public void ShowTooltip(string text, Vector2 screenPosition)
    {
        if (!isSetup || tooltipContainer == null || tooltipLabel == null)
        {
            if (debugMode) Debug.LogWarning("Tooltip system not setup properly!");
            return;
        }

        tooltipLabel.text = text;
        tooltipContainer.style.display = DisplayStyle.Flex;

        // Position tooltip near mouse but keep it on screen
        var containerRect = tooltipContainer.parent.contentRect;
        float tooltipWidth = 200; // Estimated width
        float tooltipHeight = 30; // Estimated height

        float x = screenPosition.x + 10;
        float y = screenPosition.y - 30;

        // Keep tooltip on screen
        if (x + tooltipWidth > containerRect.width)
            x = screenPosition.x - tooltipWidth - 10;
        if (y < 0)
            y = screenPosition.y + 10;
        if (y + tooltipHeight > containerRect.height)
            y = containerRect.height - tooltipHeight;

        tooltipContainer.style.left = x;
        tooltipContainer.style.top = y;
    }

    public void HideTooltip()
    {
        if (tooltipContainer != null)
        {
            tooltipContainer.style.display = DisplayStyle.None;
        }
    }

    void Update()
    {
        // Update tooltip position if it's visible
        if (tooltipContainer != null && tooltipContainer.style.display == DisplayStyle.Flex)
        {
            Vector2 mousePos = Input.mousePosition;
            mousePos.y = Screen.height - mousePos.y; // Flip Y coordinate for UI
            ShowTooltip(tooltipLabel.text, mousePos);
        }
    }
}
