-- Arena frame stealth.
-- Only handles frame alpha based on stealth state. Nothing fancy.

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

-- MODULE SETUP

local Stealth = {}
AC.ArenaFrameStealth = Stealth

-- ALPHA

local ALPHA = {
    VISIBLE = 1.0,      -- Enemy visible (normal state)
    STEALTH = 0.5,      -- Enemy in stealth (50% transparent)
    DESTROYED = 0.3,    -- Enemy left arena (30% transparent)
    CLEARED = 0         -- Match ended (hidden)
}

-- HELPERS

-- Get arena frames.
local function GetArenaFrames()
    local MFM = AC.MasterFrameManager
    if MFM and MFM.frames then
        return MFM.frames
    end
    if AC.FrameManager and AC.FrameManager.GetFrames then
        return AC.FrameManager:GetFrames()
    end
    return AC.arenaFrames
end

-- Check if we're in an active arena.
local function IsInArena()
    local _, instanceType = IsInInstance()
    -- This is kind of important I guess: use both checks so BGs don't count.
    return instanceType == "arena" and IsActiveBattlefieldArena()
end

-- Validate unit token.
local function IsValidUnit(unit)
    return unit and unit:match("^arena[1-3]$")
end

-- CORE

-- ARENA_OPPONENT_UPDATE.
function Stealth:OnArenaOpponentUpdate(unit, updateType)
    -- Safety checks.
    if not IsInArena() then return end
    if not IsValidUnit(unit) then return end

    -- Extract arena index from unit token ("arena1" -> 1).
    local arenaIndex = tonumber(unit:match("arena(%d)"))
    if not arenaIndex then return end

    local frames = GetArenaFrames()
    if not frames then return end

    local frame = frames[arenaIndex]
    if not frame then return end

    -- Simple alpha changes based on update type.
    if updateType == "seen" then
        -- Always restore on "seen" so gate-open alpha doesn't get stuck.
        frame:SetAlpha(ALPHA.VISIBLE)
    elseif updateType == "unseen" then
        frame:SetAlpha(ALPHA.STEALTH)
    elseif updateType == "destroyed" then
        frame:SetAlpha(ALPHA.DESTROYED)
    elseif updateType == "cleared" then
        frame:SetAlpha(ALPHA.CLEARED)
    end
end

-- PREP ROOM

-- ARENA_PREP_OPPONENT_SPECIALIZATIONS.
function Stealth:OnArenaPrepOpponents()
    if not IsInArena() then return end

    local frames = GetArenaFrames()
    if not frames then return end

    -- Set all frames to stealth alpha so gate-open stealth looks right.
    for i = 1, 3 do
        if frames[i] then
            frames[i]:SetAlpha(ALPHA.STEALTH)
        end
    end
end

-- EVENTS

-- Initialize.
function Stealth:Initialize()
    self.eventFrame = CreateFrame("Frame")

    self.eventFrame:RegisterEvent("ARENA_OPPONENT_UPDATE")
    self.eventFrame:RegisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS")

    self.eventFrame:SetScript("OnEvent", function(_, event, unit, updateType)
        if event == "ARENA_OPPONENT_UPDATE" then
            self:OnArenaOpponentUpdate(unit, updateType)
        elseif event == "ARENA_PREP_OPPONENT_SPECIALIZATIONS" then
            self:OnArenaPrepOpponents()
        end
    end)
end

-- AUTO-INIT
Stealth:Initialize()
