-- Party class indicator event handling.
-- Split out from BlackoutEngine so it still works in Midnight.

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

-- This is kind of important I guess: don't grab AC at file load.
-- (It might not exist yet.)
local function GetAC()
    return _G.ArenaCore
end

-- NAME_PLATE_UNIT_ADDED

local nameplateFrame
local function InitializeNameplateFrame()
    if nameplateFrame then return end
    
    nameplateFrame = CreateFrame("Frame")
    nameplateFrame:RegisterEvent("NAME_PLATE_UNIT_ADDED")

nameplateFrame:SetScript("OnEvent", function(self, event, unit)
    local AC = GetAC()
    if not AC then return end
    
    local nameplate = C_NamePlate.GetNamePlateForUnit(unit)
    if not nameplate then return end
    
    -- Nameplates can expose the unit frame in different places, so try both.
    local frame = nameplate.unitFrame or nameplate.UnitFrame
     
    if frame and not frame:IsForbidden() and not frame:IsProtected() then
            -- This is kind of important I guess: nameplates get recycled.
            -- If this isn't a friendly player, nuke any leftover icons/pointers.
            
            -- Note: custom nameplate addons can be protected/forbidden.
            if AC.UpdatePartyClassIndicator then
                -- This is kind of important I guess: don't pass the real nameplate frame.
                -- (Protected/forbidden frames will just explode.)
                local dummyFrame = { unit = unit }
                AC.UpdatePartyClassIndicator(dummyFrame)
            end
            
            -- If we have independent frames tracked, clean them up.
            if AC.PartyClassIndicators and AC.PartyClassIndicators.independentFrames then
                local independentFrames = AC.PartyClassIndicators.independentFrames
                
                local isFriendlyPlayer = UnitIsPlayer(unit) and UnitIsFriend("player", unit)
                
                if not isFriendlyPlayer then
                    local classFrame = independentFrames[unit]
                    if classFrame then
                        classFrame:Hide()
                    end
                    
                    local pointerFrame = independentFrames[unit .. "_pointer"]
                    if pointerFrame then
                        pointerFrame:Hide()
                    end
                end
            end
    end
    end)
end

-- NAME_PLATE_UNIT_REMOVED

local function HandleNamePlateRemoved(unit)
    local AC = GetAC()
    if not AC then return end
    
    -- This is kind of important I guess: clean up independent frames so they don't stick around.
    if AC.PartyClassIndicators then
        local independentFrames = AC.PartyClassIndicators.independentFrames
        if independentFrames then
            -- Class icon.
            local classFrame = independentFrames[unit]
            if classFrame then
                classFrame:Hide()
                classFrame:SetScript("OnUpdate", nil)
                classFrame.unit = nil
                independentFrames[unit] = nil
            end
            
            -- Pointer.
            local pointerFrame = independentFrames[unit .. "_pointer"]
            if pointerFrame then
                pointerFrame:Hide()
                pointerFrame:SetScript("OnUpdate", nil)
                pointerFrame.unit = nil
                independentFrames[unit .. "_pointer"] = nil
            end
        end
    end
end

-- Register NAME_PLATE_UNIT_REMOVED.
local nameplateRemovedFrame = CreateFrame("Frame")
nameplateRemovedFrame:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
nameplateRemovedFrame:SetScript("OnEvent", function(self, event, unit)
    HandleNamePlateRemoved(unit)
end)

-- INIT

C_Timer.After(0.5, function()
    local AC = GetAC()
    if not AC then return end
    
    -- Kick the event handler.
    InitializeNameplateFrame()
    
    if AC and AC.PARTY_CLASS_DEBUG then
        print("|cff8B45FF[Party Class Indicators]|r Event handler initialized")
    end
end)

-- This is kind of important I guess: make sure health bar hiding sticks on fresh login.
C_Timer.After(2, function()
    local AC = GetAC()
    if not AC then return end
    
    -- Start the persistence ticker if hideHealthBars is enabled.
    if AC.EnsureHealthBarHidingPersistence then
        AC:EnsureHealthBarHidingPersistence()
    end
end)

-- Backup trigger for zone changes / reloads.
local playerEnterFrame = CreateFrame("Frame")
playerEnterFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
playerEnterFrame:SetScript("OnEvent", function(self, event)
    -- Small delay so the DB is ready.
    C_Timer.After(0.5, function()
        local AC = GetAC()
        if not AC then return end
        
        if AC.EnsureHealthBarHidingPersistence then
            AC:EnsureHealthBarHidingPersistence()
        end
    end)
end)

-- Module loaded.
if AC and AC.PARTY_CLASS_DEBUG then
    print("|cff8B45FF[Party Class Indicators]|r Engine module loaded")
end
