--[[--------------------------------------------------------------------------
	Boom - Modern Healing & Support Raid Frames
	UI/Tooltip.lua

	Unit tooltips on hover. Driven entirely by UnitButton's OnEnter / OnLeave.

	This file holds exactly one opinion: the tooltip never goes near the frame
	it describes. A raid layout is a dense grid, and a cursor-anchored tooltip
	covers the two or three players you are most likely to need next - which is
	the worst possible moment to blind yourself. Boom parks it in the same
	corner of the screen every time, so reading it becomes muscle memory rather
	than a search.
----------------------------------------------------------------------------]]

local ADDON_NAME, Boom = ...

local Tooltip = Boom:NewModule("Tooltip")

local GameTooltip = GameTooltip
local UIParent = UIParent
local InCombatLockdown = InCombatLockdown
local UnitExists = UnitExists

-- Bottom-right of the screen. The Y offset clears the default action bars at
-- 1.0 UI scale, and the X offset keeps it off the very edge on ultrawides.
local ANCHOR_POINT, ANCHOR_X, ANCHOR_Y = "BOTTOMRIGHT", -20, 180

--------------------------------------------------------------------------------
-- Gate
--------------------------------------------------------------------------------

--- Does the user want a tooltip at this instant? Read live: both settings are
--- toggles a player flips mid-session while learning a fight.
local function allowed()
	local general = Boom.profile and Boom.profile.general
	if not general or not general.showTooltips then return false end
	if not general.tooltipsInCombat and InCombatLockdown() then return false end
	return true
end

--------------------------------------------------------------------------------
-- Public API
--------------------------------------------------------------------------------

function Tooltip:Show(button)
	if not button or not allowed() then return end

	-- displayUnit follows the vehicle swap; the secure `unit` attribute cannot,
	-- so the tooltip would otherwise describe the vehicle instead of the rider.
	local unit = button.displayUnit or button.unit
	if not unit or not UnitExists(unit) then return end

	-- ANCHOR_NONE stops GameTooltip from positioning itself against the owner,
	-- while still recording the owner so Hide() (and other addons) can tell
	-- whose tooltip this is.
	GameTooltip:SetOwner(button, "ANCHOR_NONE")
	GameTooltip:ClearAllPoints()
	GameTooltip:SetPoint(ANCHOR_POINT, UIParent, ANCHOR_POINT, ANCHOR_X, ANCHOR_Y)
	GameTooltip:SetUnit(unit)
	GameTooltip:Show()

	self.owner = button
end

function Tooltip:Hide(button)
	-- Never tear down a tooltip somebody else raised: OnLeave can arrive after
	-- another frame has already claimed GameTooltip.
	if button and GameTooltip.GetOwner then
		local owner = GameTooltip:GetOwner()
		if owner and owner ~= button then return end
	end

	self.owner = nil
	GameTooltip:Hide()
end

--------------------------------------------------------------------------------
-- Lifecycle
--------------------------------------------------------------------------------

function Tooltip:OnEnable()
	-- A tooltip left hanging over the UI as the pull starts is precisely the
	-- obstruction this module exists to avoid, and no OnLeave fires if the
	-- cursor never moves off the frame.
	self:RegisterEvent("PLAYER_REGEN_DISABLED", function()
		local general = Boom.profile and Boom.profile.general
		if self.owner and not (general and general.tooltipsInCombat) then
			self:Hide(self.owner)
		end
	end)
end
