thisActor = ...

-- Audio functions
local function play_switch_UI()
    if stingray.Wwise then
        stingray.Wwise.load_bank("content/audio/default")
        local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
        stingray.WwiseWorld.trigger_event(wwise_world, "sfx_UI_music_switch_UI")
    end
end

local function play_main_menu_ambient_layer1()
    if stingray.Wwise then
        stingray.Wwise.load_bank("content/audio/default")
        local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
        stingray.WwiseWorld.trigger_event(wwise_world, "sfx_ui_menu_layer_01")
    end
end

local sceneAnimation = scaleform.Actor.component_by_name(thisActor, "animation")

local creditsMC = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "creditsMC")
local creditsMCAnimation = scaleform.Actor.component_by_name(creditsMC, "animation")

local creditsTimelineTriggerListener = scaleform.EventListener.create(mainTimelineTriggerListener, function(e, thisListener)
    if e.name == "end_credits" then
        scaleform.Stage.remove_scene(thisActor)
        -- Return audio to main menu ambient layer 1
        play_switch_UI()
        play_main_menu_ambient_layer1()
    end
end )

local keyDownListener = scaleform.EventListener.create(keyDownListener, function(e, thisListener)
    local currFrame = scaleform.AnimationComponent.current_frame(creditsMCAnimation)
    
    if currFrame < 29 then
        currFrame = 29 + currFrame
    elseif currFrame == 29 then
        currFrame = 60
    end
    
    scaleform.AnimationComponent.goto_frame(creditsMCAnimation, currFrame)
    scaleform.AnimationComponent.play(creditsMCAnimation)
    scaleform.EventListener.disconnect(thisListener)
end )

scaleform.EventListener.connect(creditsTimelineTriggerListener, creditsMC, scaleform.EventTypes.TimelineTrigger)
scaleform.EventListener.connect(keyDownListener, scaleform.EventTypes.KeyDown)