-- ============================================================================
-- File: ArenaCore/modules/SimpleCCTest.lua
-- Purpose: Simple CC aura test mode - shows CC icons over class icons only
-- ============================================================================
local AC = _G.ArenaCore
if not AC then return end

-- Simple test mode state
local testActive = false
local testFrames = {}

-- Test CC auras for arena 2 and 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)
}

-- 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
    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(true)
        classIcon.testCooldown:SetDrawSwipe(true)
        classIcon.testCooldown:SetSwipeColor(0, 0, 0, 0.6)
    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
}

-- Hook into main test mode to auto-show CC auras
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, ...)
            -- Auto-enable CC auras after main test mode starts
            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, ...)
            -- Auto-disable CC auras when main test mode ends
            if AC.SimpleCCTest then
                AC.SimpleCCTest:Hide()
            end
            return result
        end
    end
end)
