---@diagnostic disable: undefined-field, undefined-global
local _, namespace = ...

local WTUI = namespace.WildThingsUI
local Theme = namespace.Theme

--[[
Shared clamped, combat-suppressed tooltip widget (DRAGUX-03). ONE lazily-created
plain Frame at TOOLTIP strata -- deliberately NOT the built-in game-tooltip frame
type, and NOT a reuse of this addon's pre-existing GameTooltip-based shared
tooltip mechanism (see UI/UI.lua's tooltip-frame construction and
UI/Functions.lua's AddTooltip, used by status badges elsewhere).
The two tooltip systems coexist by design: this file's ShowWidgetTooltip /
HideWidgetTooltip cover new hover and disabled-control-explanation needs;
existing AddTooltip call sites are NOT migrated in this phase.
]]

local MAX_WIDTH = 250
local PAD = 8

local tooltipFrame

local function GetTooltipFrame()
    if not tooltipFrame then
        tooltipFrame = CreateFrame("Frame", nil, UIParent)
        tooltipFrame:SetFrameStrata("TOOLTIP")

        WTUI:AddRoundedFill(tooltipFrame, Theme.RADIUS_MD)
        tooltipFrame:SetRoundedFillColor(Theme.PANEL_BG.r, Theme.PANEL_BG.g, Theme.PANEL_BG.b, Theme.PANEL_BG.a)

        -- No border: fills only, soften borders (SEED-004). This used to build a square one
        -- and immediately hide it; nothing ever showed it again, so the whole thing is gone.

        tooltipFrame.text = tooltipFrame:CreateFontString(nil, "ARTWORK")
        tooltipFrame.text:SetFontObject(WildThingsFont13)
        tooltipFrame.text:SetTextColor(Theme.TEXT.r, Theme.TEXT.g, Theme.TEXT.b, Theme.TEXT.a)
        tooltipFrame.text:SetPoint("TOPLEFT", PAD, -PAD)
        tooltipFrame.text:SetWordWrap(true)
        tooltipFrame.text:SetJustifyH("LEFT")

        tooltipFrame:Hide()
    end
    return tooltipFrame
end

-- WTUI:ShowWidgetTooltip(anchor, textOrFn)
-- textOrFn is a string, or a zero-arg function re-evaluated on every show. No-op
-- (guard clause) while combat-suppressed, or when the resolved text is nil/empty.
-- Sizes to natural text width capped at ~250px, then clamps against the screen
-- edges so the tooltip never runs off-screen.
function WTUI:ShowWidgetTooltip(anchor, textOrFn)
    if not anchor then return end
    if WTUI.combatSuppressTooltips then return end

    local text = (type(textOrFn) == "function") and textOrFn() or textOrFn
    if not text or text == "" then return end

    local tt = GetTooltipFrame()
    tt:ClearAllPoints()
    tt:SetPoint("BOTTOMLEFT", anchor, "TOPLEFT", 0, 4)

    tt.text:SetWidth(MAX_WIDTH - PAD * 2)
    tt.text:SetText(text)

    local naturalWidth = tt.text:GetStringWidth() + PAD * 2
    local width = math.min(naturalWidth, MAX_WIDTH)
    tt.text:SetWidth(width - PAD * 2)
    tt:SetSize(width, tt.text:GetStringHeight() + PAD * 2)

    tt:Show()

    -- Screen-edge clamp (horizontal + vertical).
    local scale = tt:GetEffectiveScale()
    local screenWidth = GetScreenWidth() * UIParent:GetEffectiveScale()
    local screenHeight = GetScreenHeight() * UIParent:GetEffectiveScale()

    local left, right = (tt:GetLeft() or 0) * scale, (tt:GetRight() or 0) * scale
    local top, bottom = (tt:GetTop() or 0) * scale, (tt:GetBottom() or 0) * scale

    local dx, dy = 0, 0
    if left < 0 then
        dx = -left / scale
    elseif right > screenWidth then
        dx = -(right - screenWidth) / scale
    end
    if bottom < 0 then
        dy = -bottom / scale
    elseif top > screenHeight then
        dy = -(top - screenHeight) / scale
    end

    if dx ~= 0 or dy ~= 0 then
        local point, relativeTo, relativePoint, offsetX, offsetY = tt:GetPoint(1)
        tt:ClearAllPoints()
        tt:SetPoint(point, relativeTo, relativePoint, offsetX + dx, offsetY + dy)
    end
end

-- WTUI:HideWidgetTooltip()
function WTUI:HideWidgetTooltip()
    if tooltipFrame then tooltipFrame:Hide() end
end

--[[
Persistent combat-gate frame, created once at file-load time for the addon's
lifetime -- mirrors UI/WindowRaiser.lua's persistent PLAYER_REGEN_DISABLED
registration idiom. Suppresses and hides the widget tooltip at
PLAYER_REGEN_DISABLED; re-enables it at PLAYER_REGEN_ENABLED. Self-contained to
this shipped widget -- unrelated to the dev-only grid's own combat lock, which is
a later plan's concern.
]]
local tooltipCombatGate = CreateFrame("Frame")
tooltipCombatGate:RegisterEvent("PLAYER_REGEN_DISABLED")
tooltipCombatGate:RegisterEvent("PLAYER_REGEN_ENABLED")
tooltipCombatGate:SetScript("OnEvent", function(_, event)
    WTUI.combatSuppressTooltips = (event == "PLAYER_REGEN_DISABLED")
    if WTUI.combatSuppressTooltips then
        WTUI:HideWidgetTooltip()
    end
end)
