---@type Orbit
local Orbit = Orbit
local L = Orbit.L
local OrbitEngine = Orbit.Engine

-- [ PLUGIN REGISTRATION ]----------------------------------------------------------------------------
local SYSTEM_ID = "Orbit_TooltipAnchor"
-- Small identifiable rect at Blizzard's HUD Tooltip default spot (BOTTOMRIGHT -9,85); tooltips corner-pin to it, so its size only shapes the grab target.
local WIDTH, HEIGHT = 80, 20
local DEFAULT_X, DEFAULT_Y = -9, 85

local Plugin = Orbit:RegisterPlugin("Tooltip Anchor", SYSTEM_ID, {
    displayName = L.PLG_NAME_TOOLTIP_ANCHOR,
    defaults = {},
})

function Plugin:AddSettings(dialog, systemFrame)
    OrbitEngine.Config:Render(dialog, systemFrame, self, { hideNativeSettings = true, controls = {} })
end

-- Default-anchored tooltips pin to whichever frame corner faces the nearest screen corner, so they grow toward screen centre — the same intent as Blizzard's nearest-corner logic in GameTooltip_SetDefaultAnchor.
local function TooltipCorner(frame)
    local cx, cy = frame:GetCenter()
    local ux, uy = UIParent:GetCenter()
    if not cx or not ux then return "BOTTOMRIGHT" end
    return (cy >= uy and "TOP" or "BOTTOM") .. (cx >= ux and "RIGHT" or "LEFT")
end

-- [ LIFECYCLE ]--------------------------------------------------------------------------------------
function Plugin:OnLoad()
    self.frame = CreateFrame("Frame", "OrbitTooltipAnchor", UIParent)
    self.frame:SetSize(WIDTH, HEIGHT)
    self.frame:SetClampedToScreen(true)
    self.frame.systemIndex = SYSTEM_ID
    self.frame.editModeName = self.displayName
    self.frame.anchorOptions = { horizontal = false, vertical = false }
    self.frame:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", DEFAULT_X, DEFAULT_Y)
    self.frame.orbitTooltipCorner = TooltipCorner
    -- Edit-Mode-only label so the rect is identifiable; Blizzard's localized system name when the GlobalString exists.
    local label = self.frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    label:SetPoint("CENTER")
    label:SetText(HUD_EDIT_MODE_TOOLTIP_LABEL or self.displayName)
    label:Hide()
    self.frame.Label = label

    OrbitEngine.Frame:AttachSettingsListener(self.frame, self, SYSTEM_ID)
    OrbitEngine.Frame:RestorePosition(self.frame, self, SYSTEM_ID)
    self:RegisterStandardEvents()

    -- Guarded keep-hidden, not NativeFrame:KeepAliveHidden (permanent): Blizzard re-Shows the HUD Tooltip box on every Edit Mode entry AND via its HUD Tooltip checkbox mid-session; the orbitAnchorActive gate hands the box back once this plugin is disabled.
    local container = GameTooltipDefaultContainer
    if container then
        hooksecurefunc(container, "Show", function(c)
            if self.frame and self.frame.orbitAnchorActive and not InCombatLockdown() then c:Hide() end
        end)
    end
end

function Plugin:ApplySettings()
    local frame = self.frame
    if not frame then return end
    frame:Show()
    frame.orbitAnchorActive = true
    frame.Label:SetShown(Orbit:IsEditMode())
    -- Covers a box already shown when this runs (enable mid-Edit-Mode); the OnLoad Show-hook handles every later re-Show. Insecure Hide on the insecure container is taint-free, and hidden frames still answer GetPoint, so Blizzard's own anchor fallback keeps working if the QoL module is off.
    if Orbit:IsEditMode() and GameTooltipDefaultContainer then GameTooltipDefaultContainer:Hide() end
end

function Plugin:OnDisable()
    if not self.frame then return end
    self.frame.orbitAnchorActive = false
    self.frame:Hide()
end
