-- Arena frame tooltips.
-- Shows a GameTooltip when you hover an arena frame.

local AC = _G.ArenaCore
if not AC then return end

local Tooltips = {}
AC.ArenaFrameTooltips = Tooltips

-- CONFIG

local TOOLTIP_ANCHOR = "ANCHOR_RIGHT"  -- Right side of the frame.

-- HELPERS

local function GetSettings()
    if not (AC.DB and AC.DB.profile) then
        return { enabled = true, showInCombat = true }
    end

    if not AC.DB.profile.arenaTooltips then
        AC.DB.profile.arenaTooltips = {
            enabled = true,
            showInCombat = true,
        }
    end

    return AC.DB.profile.arenaTooltips
end

-- HANDLERS

local function OnEnter(self)
    local settings = GetSettings()
    if not settings.enabled then return end

    -- Optional: hide tooltips during combat.
    if not settings.showInCombat and InCombatLockdown() then return end
    
    local unit = self.unit
    if not unit or not UnitExists(unit) then return end
    -- Anchor tooltip.
    GameTooltip:SetOwner(self, TOOLTIP_ANCHOR)
    -- Use WoW's built-in unit tooltip.
    GameTooltip:SetUnit(unit)
    GameTooltip:Show()
end

local function OnLeave(self)
    if GameTooltip then
        GameTooltip:Hide()
    end
end

-- FRAME SETUP

function Tooltips:EnableTooltip(frame)
    if not frame then return end

    -- This is kind of important I guess: don't call EnableMouse in combat.
    -- (Test mode is fine since those frames aren't secure.)
    local AC = _G.ArenaCore
    if InCombatLockdown() and not (AC and AC.testModeEnabled) then
        return
    end

    frame:SetScript("OnEnter", OnEnter)
    frame:SetScript("OnLeave", OnLeave)

    frame:EnableMouse(true)
end

function Tooltips:DisableTooltip(frame)
    if not frame then return end

    frame:SetScript("OnEnter", nil)
    frame:SetScript("OnLeave", nil)
end

function Tooltips:RefreshAll()
    -- This is kind of important I guess: if we're in combat, defer (unless test mode).
    local AC = _G.ArenaCore
    if InCombatLockdown() and not (AC and AC.testModeEnabled) then
        local combatFrame = CreateFrame("Frame")
        combatFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
        combatFrame:SetScript("OnEvent", function(self)
            self:UnregisterEvent("PLAYER_REGEN_ENABLED")
            if Tooltips and Tooltips.RefreshAll then
                Tooltips:RefreshAll()
            end
        end)
        return
    end

    local settings = GetSettings()

    local frames = {}
    if AC.MasterFrameManager and AC.MasterFrameManager.frames then
        frames = AC.MasterFrameManager.frames
    elseif AC.arenaFrames then
        frames = AC.arenaFrames
    end

    for i = 1, 3 do
        local frame = frames[i]
        if frame then
            if settings.enabled then
                self:EnableTooltip(frame)
            else
                self:DisableTooltip(frame)
            end
        end
    end
end

-- INIT

function Tooltips:Initialize()
    -- Small delay so frames exist.
    C_Timer.After(0.5, function()
        self:RefreshAll()
    end)
    
    -- Hook frame creation so new frames get tooltips too.
    if AC.MasterFrameManager and AC.MasterFrameManager.CreateArenaFrame then
        if hooksecurefunc then
            hooksecurefunc(AC.MasterFrameManager, "CreateArenaFrame", function(self, index)
                local frame = self and self.frames and self.frames[index]
                if frame then
                    C_Timer.After(0.1, function()
                        Tooltips:EnableTooltip(frame)
                    end)
                end
            end)
        end
    end
end

-- PUBLIC API

function Tooltips:Enable()
    local settings = GetSettings()
    settings.enabled = true
    self:RefreshAll()
end

function Tooltips:Disable()
    local settings = GetSettings()
    settings.enabled = false
    self:RefreshAll()
end

function Tooltips:SetShowInCombat(enabled)
    local settings = GetSettings()
    settings.showInCombat = enabled
end

-- AUTO-INIT

-- Init when ArenaCore is ready.
if AC.MasterFrameManager then
    C_Timer.After(1.0, function()
        Tooltips:Initialize()
    end)
else
    -- Wait for MasterFrameManager.
    local initFrame = CreateFrame("Frame")
    initFrame:RegisterEvent("ADDON_LOADED")
    initFrame:SetScript("OnEvent", function(self, event, addonName)
        if addonName == "ArenaCore" and AC.MasterFrameManager then
            C_Timer.After(1.0, function()
                Tooltips:Initialize()
            end)
            self:UnregisterAllEvents()
        end
    end)
end
