--[[--------------------------------------------------------------------
  PaladinHolyPower - ui/components/tooltip.lua
  Custom Tooltip Styling and Utility Attachment
----------------------------------------------------------------------]]

local PHP = _G.PaladinHolyPower
if not PHP then return end

local Tooltip = {}

-- Shared tooltip frame
local tooltipFrame = CreateFrame("Frame", "PHPCustomTooltip", UIParent, "BackdropTemplate")
tooltipFrame:SetSize(1, 1)
tooltipFrame:SetFrameStrata("TOOLTIP")
tooltipFrame:SetClampedToScreen(true)
tooltipFrame:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8",
    edgeSize = 1,
})
tooltipFrame:SetBackdropColor(0.102, 0.102, 0.102, 0.8)
tooltipFrame:SetBackdropBorderColor(0, 0, 0, 1)
tooltipFrame:Hide()

tooltipFrame.title = tooltipFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
tooltipFrame.title:SetTextColor(1, 1, 1)
tooltipFrame.title:SetJustifyH("LEFT")
tooltipFrame.title:SetPoint("TOPLEFT", 6, -6)
tooltipFrame.title:SetPoint("TOPRIGHT", -6, -6)

tooltipFrame.text = tooltipFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
tooltipFrame.text:SetTextColor(0.8, 0.8, 0.8)
tooltipFrame.text:SetJustifyH("LEFT")
tooltipFrame.text:SetPoint("TOPLEFT", tooltipFrame.title, "BOTTOMLEFT", 0, -4)
tooltipFrame.text:SetPoint("TOPRIGHT", tooltipFrame.title, "BOTTOMRIGHT", 0, -4)

-- Show tooltip with title + body
function Tooltip.Show(anchorFrame, title, body)
    tooltipFrame.title:SetText(title or "")
    tooltipFrame.text:SetText(body or "")

    local w = math.max(tooltipFrame.title:GetStringWidth(), tooltipFrame.text:GetStringWidth()) + 12
    local h = tooltipFrame.title:GetStringHeight() + tooltipFrame.text:GetStringHeight() + 18
    tooltipFrame:SetSize(w, h)

    tooltipFrame:ClearAllPoints()
    tooltipFrame:SetPoint("BOTTOM", anchorFrame, "TOP", 0, 4)
    tooltipFrame:Show()
end

-- Hide tooltip
function Tooltip.Hide()
    tooltipFrame:Hide()
end

-- Attach tooltip to any frame, preserving existing scripts
function Tooltip.Attach(widget, title, body)
    if not widget or not title then return end
    widget:HookScript("OnEnter", function()
        Tooltip.Show(widget, title, body)
    end)
    widget:HookScript("OnLeave", function()
        Tooltip.Hide()
    end)
end

PHP.Tooltip = Tooltip