-- Highlight the correct answer when a question is answered incorrectly
function WoWStudyFunction.HighlightCorrectButton(button)
    if not button then
        print("Error: Button is nil for highlight animation.")
        return
    end

    -- Add a texture to create a glowing effect
    local glowTexture = button:CreateTexture(nil, "OVERLAY")
    glowTexture:SetAllPoints(button)
    glowTexture:SetColorTexture(0, 1, 0, 0.5) -- Green with 50% opacity
    glowTexture:Hide() -- Initially hidden
    button.glowTexture = glowTexture

    -- Create animation group for pulsing effect
    highlightAnimation = glowTexture:CreateAnimationGroup()
    button.highlightAnimation = highlightAnimation

    local fadeOut = highlightAnimation:CreateAnimation("Alpha")
    fadeOut:SetFromAlpha(0.5)
    fadeOut:SetToAlpha(0)
    fadeOut:SetDuration(0.5)
    fadeOut:SetOrder(1)

    local fadeIn = highlightAnimation:CreateAnimation("Alpha")
    fadeIn:SetFromAlpha(0)
    fadeIn:SetToAlpha(0.5)
    fadeIn:SetDuration(0.5)
    fadeIn:SetOrder(2)

    highlightAnimation:SetLooping("REPEAT")

    -- Show the glow texture and play the animation
    if button.glowTexture then
        button.glowTexture:Show()
    end
    highlightAnimation:Play()

    -- Stop the animation and reset appearance after a delay
    C_Timer.After(3, function()
        if highlightAnimation:IsPlaying() then
            highlightAnimation:Stop()
        end
        if button.glowTexture then
            button.glowTexture:Hide()
        end
    end)
end

-- Create a glow animation for the flashcard to indicate a correct/incorrect answer
function CreateGlowAnimation(frame)
    if not frame then
        print("Error: Frame is nil for glow animation.")
        return nil
    end

    -- Reuse the animation if it already exists
    if frame.glowAnimation then
        return frame.glowAnimation
    end

    -- Feedback glow texture
    local glowTexture = frame:CreateTexture(nil, "ARTWORK")
    glowTexture:SetAllPoints(frame)
    glowTexture:SetColorTexture(0, 0, 0, 0) -- Init transparent
    frame.glowTexture = glowTexture

    -- Create animation group
    local glowAnimation = glowTexture:CreateAnimationGroup()
    glowAnimation:SetLooping("NONE") -- Ensure it doesn't loop

    -- Alpha animation (fade in and out)
    local fadeIn = glowAnimation:CreateAnimation("Alpha")
    fadeIn:SetFromAlpha(0)
    fadeIn:SetToAlpha(0.8) -- Glow visibility
    fadeIn:SetDuration(0.6) -- Time to fade in
    fadeIn:SetOrder(1)

    local fadeOut = glowAnimation:CreateAnimation("Alpha")
    fadeOut:SetFromAlpha(0.8)
    fadeOut:SetToAlpha(0)
    fadeOut:SetDuration(1) -- Time to fade out
    fadeOut:SetOrder(2)

    -- Hide the glow texture when animation finishes
    glowAnimation:SetScript("OnFinished", function()
        frame.glowTexture:SetColorTexture(0, 0, 0, 0)
    end)

    frame.glowAnimation = glowAnimation
    return glowAnimation
end

-- Play a glow animation on a frame with a specified color
function WoWStudyFunction.PlayGlowAnimation(frame, color)
    if not frame then
        print("Error: Frame is nil for glow animation.")
        return
    end

    -- Create or reuse the animation
    local glowAnimation = CreateGlowAnimation(frame)
    if glowAnimation then
        -- Update the glow texture color
        if frame.glowTexture then
            frame.glowTexture:SetColorTexture(unpack(color)) -- Update with new color
        else
            print("Error: Glow texture not found.")
        end

        -- Play the animation
        glowAnimation:Play()
    else
        print("Error: Glow animation could not be created.")
    end
end
