-- CursorEffects - Advanced Effects Module
-- Provides various cursor visual effects

local CE = CursorEffects
local effects = {}

-- Initialize effects module
function effects:Initialize()
    self.particleFrames = {}
    self.sparkles = {}
    self.auras = {}
end

-- Create particle effect
function effects:CreateParticle(x, y, color)
    local particle = CreateFrame("Frame", nil, UIParent)
    particle:SetSize(16, 16)
    particle:SetFrameStrata("TOOLTIP")
    particle:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y)
    
    local texture = particle:CreateTexture(nil, "BACKGROUND")
    texture:SetAllPoints()
    texture:SetColorTexture(color.r, color.g, color.b, 1)
    texture:SetTexture("Interface/Addons/CursorEffects/textures/particle.tga")
    
    particle.lifetime = 0.5
    particle.x = x
    particle.y = y
    particle.vx = (math.random() - 0.5) * 100
    particle.vy = (math.random() - 0.5) * 100
    
    table.insert(self.particleFrames, particle)
    return particle
end

-- Create sparkle effect
function effects:CreateSparkle(x, y, color)
    local sparkle = CreateFrame("Frame", nil, UIParent)
    sparkle:SetSize(8, 8)
    sparkle:SetFrameStrata("TOOLTIP")
    sparkle:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y)
    
    local texture = sparkle:CreateTexture(nil, "BACKGROUND")
    texture:SetAllPoints()
    texture:SetColorTexture(color.r, color.g, color.b, 1)
    
    sparkle.lifetime = 0.3
    sparkle.startTime = GetTime()
    sparkle.color = color
    
    table.insert(self.sparkles, sparkle)
    return sparkle
end

-- Create aura effect around cursor
function effects:CreateAura(frame, color, size)
    local aura = {
        frame = frame,
        color = color,
        size = size or 100,
        rotation = 0,
        speed = 1,
    }
    
    if not aura.texture then
        aura.texture = frame:CreateTexture(nil, "OVERLAY")
        aura.texture:SetPoint("CENTER")
        aura.texture:SetTexture("Interface/Addons/CursorEffects/textures/aura.tga")
        aura.texture:SetBlendMode("ADD")
    end
    
    table.insert(self.auras, aura)
    return aura
end

-- Update particles
function effects:UpdateParticles(elapsed)
    for i = #self.particleFrames, 1, -1 do
        local p = self.particleFrames[i]
        p.lifetime = p.lifetime - elapsed
        
        if p.lifetime <= 0 then
            p:Hide()
            table.remove(self.particleFrames, i)
        else
            local alpha = p.lifetime / 0.5
            p.x = p.x + (p.vx * elapsed)
            p.y = p.y + (p.vy * elapsed)
            
            -- Apply gravity
            p.vy = p.vy - (300 * elapsed)
            
            p:SetPoint("CENTER", UIParent, "BOTTOMLEFT", p.x, p.y)
            local texture = p:GetRegions()
            if texture then
                texture:SetAlpha(alpha)
            end
        end
    end
end

-- Update auras
function effects:UpdateAuras(elapsed)
    for i = #self.auras, 1, -1 do
        local aura = self.auras[i]
        aura.rotation = aura.rotation + (aura.speed * elapsed)
        
        if aura.texture then
            aura.texture:SetSize(aura.size, aura.size)
            aura.texture:SetColorTexture(aura.color.r, aura.color.g, aura.color.b, 0.5)
            
            -- Simple rotation via opacity modulation
            local alphaVar = math.abs(math.sin(aura.rotation)) * 0.3 + 0.2
            aura.texture:SetAlpha(alphaVar)
        end
    end
end

-- Enhanced trail effect with multiple points
function effects:CreateTrailPoint(x, y, color, lifespan)
    local point = CreateFrame("Frame", nil, UIParent)
    point:SetSize(20, 20)
    point:SetFrameStrata("BACKGROUND")
    point:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y)
    
    local texture = point:CreateTexture(nil, "BACKGROUND")
    texture:SetAllPoints()
    texture:SetColorTexture(color.r, color.g, color.b, 0.6)
    texture:SetBlendMode("ADD")
    
    point.lifetime = lifespan
    point.maxLifetime = lifespan
    
    return point
end

-- Trail cleanup
function effects:CleanupTrails()
    -- Trails will be managed by the main update loop
end

-- Particle burst effect
function effects:BurstParticles(x, y, color, count)
    for i = 1, count do
        self:CreateParticle(x, y, color)
    end
end

-- Ring pulse effect
function effects:CreateRingPulse(x, y, color)
    local ring = CreateFrame("Frame", nil, UIParent)
    ring:SetSize(40, 40)
    ring:SetFrameStrata("TOOLTIP")
    ring:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y)
    
    local texture = ring:CreateTexture(nil, "BACKGROUND")
    texture:SetAllPoints()
    texture:SetColorTexture(color.r, color.g, color.b, 0.8)
    texture:SetBlendMode("ADD")
    
    ring.lifetime = 0.4
    ring.maxLifetime = 0.4
    ring.maxSize = 80
    ring.x = x
    ring.y = y
    ring.color = color
    
    return ring
end

-- Radial sweep effect (like a radar)
function effects:CreateRadialSweep(x, y, color)
    local sweep = CreateFrame("Frame", nil, UIParent)
    sweep:SetSize(100, 100)
    sweep:SetFrameStrata("TOOLTIP")
    sweep:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y)
    
    local texture = sweep:CreateTexture(nil, "BACKGROUND")
    texture:SetAllPoints()
    texture:SetColorTexture(color.r, color.g, color.b, 0.4)
    texture:SetBlendMode("ADD")
    
    sweep.rotation = 0
    sweep.speed = 10
    sweep.lifetime = 1.0
    
    return sweep
end

-- Shimmer effect
function effects:CreateShimmer(x, y, color)
    local shimmer = CreateFrame("Frame", nil, UIParent)
    shimmer:SetSize(30, 30)
    shimmer:SetFrameStrata("TOOLTIP")
    shimmer:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y)
    
    local texture = shimmer:CreateTexture(nil, "BACKGROUND")
    texture:SetAllPoints()
    texture:SetColorTexture(color.r, color.g, color.b, 1)
    texture:SetBlendMode("ADD")
    
    shimmer.lifetime = 0.2
    shimmer.startTime = GetTime()
    shimmer.color = color
    
    return shimmer
end

_G.CursorEffectsEffects = effects
return effects
