-- ============================================================================
-- File: ArenaCore/modules/SimpleCCTest.lua
-- Simple CC test mode: slaps a couple CC icons over class icons.
-- ============================================================================
local AC = _G.ArenaCore
if not AC then return end

-- Test mode state
local testActive = false
local testFrames = {}

-- Test CC auras (arena 2 + 3)
local testAuras = {
    [2] = { spellId = 15487, name = "Silence", icon = "Interface\\Icons\\ability_priest_silence", duration = 5 }, -- Shadow Priest Silence (Spell ID 15487)
    [3] = { spellId = 118, name = "Polymorph", icon = "Interface\\Icons\\spell_nature_polymorph", duration = 8 }, -- Mage Polymorph (Spell ID 118)
}

local function IsSpiralGlowLineEnabled()
    local db = AC.DB and AC.DB.profile
    if not (db and db.classIcons) then return true end
    return db.classIcons.spiralGlowLine ~= false
end

-- Show CC aura on a class icon
local function ShowCCAura(arenaIndex)
    local MFM = AC.MasterFrameManager
    
    local frame = MFM and MFM.frames and MFM.frames[arenaIndex]
    
    if not frame then 
        return 
    end
    
    if not frame.classIcon then 
        return 
    end
    
    if not frame.classIcon.icon then 
        return 
    end
    
    local classIcon = frame.classIcon
    local aura = testAuras[arenaIndex]
    if not aura then return end
    
    -- Store original texture so we can put it back later.
    if not testFrames[arenaIndex] then
        testFrames[arenaIndex] = {
            originalTexture = classIcon.icon:GetTexture()
        }
    end
    
    -- Create cooldown frame if needed.
    if not classIcon.testCooldown then
        classIcon.testCooldown = CreateFrame("Cooldown", nil, classIcon, "CooldownFrameTemplate")
        classIcon.testCooldown:SetAllPoints(classIcon.icon)
        classIcon.testCooldown:SetDrawEdge(IsSpiralGlowLineEnabled())
        classIcon.testCooldown:SetDrawSwipe(true)
        classIcon.testCooldown:SetSwipeColor(0, 0, 0, 0.6)
    end

    if classIcon.testCooldown and classIcon.testCooldown.SetDrawEdge then
        classIcon.testCooldown:SetDrawEdge(IsSpiralGlowLineEnabled())
    end
    
    -- Set CC aura icon.
    classIcon.icon:SetTexture(aura.icon)
    
    -- Start cooldown.
    local startTime = GetTime()
    classIcon.testCooldown:SetCooldown(startTime, aura.duration)
    
    -- Loop the cooldown.
    C_Timer.After(aura.duration, function()
        if testActive and testFrames[arenaIndex] then
            ShowCCAura(arenaIndex)
        end
    end)
end

-- Hide CC aura and restore class icon
local function HideCCAura(arenaIndex)
    local MFM = AC.MasterFrameManager
    local frame = MFM and MFM.frames and MFM.frames[arenaIndex]
    if not frame or not frame.classIcon then return end
    
    local classIcon = frame.classIcon
    local testData = testFrames[arenaIndex]
    
    if testData and testData.originalTexture and classIcon.icon then
        classIcon.icon:SetTexture(testData.originalTexture)
    end
    
    if classIcon.testCooldown then
        classIcon.testCooldown:Clear()
    end
    
    testFrames[arenaIndex] = nil
end

-- Public API
AC.SimpleCCTest = {
    Show = function()
        if testActive then 
            return 
        end
        testActive = true
        
        -- Only show for arena 2 and 3.
        ShowCCAura(2)
        ShowCCAura(3)
    end,
    
    Hide = function()
        if not testActive then 
            return 
        end
        testActive = false
        
        -- Hide for arena 2 and 3.
        HideCCAura(2)
        HideCCAura(3)
    end,
    
    IsActive = function()
        return testActive
    end,

    UpdateSpiralGlowLine = function()
        local enabled = IsSpiralGlowLineEnabled()
        for i = 1, 3 do
            local frame = AC.MasterFrameManager and AC.MasterFrameManager.frames and AC.MasterFrameManager.frames[i]
            local classIcon = frame and frame.classIcon
            local cd = classIcon and classIcon.testCooldown
            if cd and cd.SetDrawEdge then
                cd:SetDrawEdge(enabled)
            end
        end
    end
}

-- Hook into main test mode so this auto-runs.
C_Timer.After(1.0, function()
    if AC.MasterFrameManager then
        -- Hook EnableTestMode to auto-enable CC auras.
        local oldEnableTestMode = AC.MasterFrameManager.EnableTestMode
        AC.MasterFrameManager.EnableTestMode = function(self, ...)
            local result = oldEnableTestMode(self, ...)
            -- Let the main test mode spin up first.
            C_Timer.After(0.3, function()
                if AC.SimpleCCTest then
                    AC.SimpleCCTest:Show()
                end
            end)
            return result
        end
        
        -- Hook DisableTestMode to auto-disable CC auras.
        local oldDisableTestMode = AC.MasterFrameManager.DisableTestMode
        AC.MasterFrameManager.DisableTestMode = function(self, ...)
            local result = oldDisableTestMode(self, ...)
            -- Stop our loop when test mode ends.
            if AC.SimpleCCTest then
                AC.SimpleCCTest:Hide()
            end
            return result
        end
    end
end)
