thisActor = ...

-- ============================================================================
-- Stage setup
-- ============================================================================
-- The content has been designed to support arbitrary aspect ratios
-- with appropriate safe zones. Changing scale mode to 'No Border', 
-- as we want the content to scale up and we aren't worried if the
-- the content is chopped on the larger dimension.
scaleform.Stage.set_view_scale_mode(scaleform.ViewScaleModes.NoBorder)

fov = 0.0
audio_on = false
volume = 0.0

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

local main = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "main")
local mainAnimation = scaleform.Actor.component_by_name(main, "animation")

local options = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "options")
local optionsAnimation = scaleform.Actor.component_by_name(options, "animation")

local background = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "background")
local backgroundAnimation = scaleform.Actor.component_by_name(background, "animation")

scaleform.Actor.set_visible(options, false);

-- Audio functions
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 function play_main_menu_ambient_layer2()
    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_02")
    end
end

local function play_small_reveal()
    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_small_reveal")
    end
end

local mainTimelineTriggerListener = scaleform.EventListener.create(mainTimelineTriggerListener, function(e, thisListener)
    if e.name == "open" then
        play_small_reveal()
        print("print small reveal")
        scaleform.Actor.set_visible(options, true)
        scaleform.AnimationComponent.goto_frame(optionsAnimation, 2)
        scaleform.AnimationComponent.play(optionsAnimation)
        scaleform.AnimationComponent.goto_frame(backgroundAnimation, 2)
        scaleform.AnimationComponent.play(backgroundAnimation)
        -- Audio swap to main menu ambient layer 2
        play_main_menu_ambient_layer2()

    elseif e.name == "off" then
        scaleform.Actor.set_visible(main, false)
    end
end )

local optionsTimelineTriggerListener = scaleform.EventListener.create(optionsTimelineTriggerListener, function(e, thisListener)
    if e.name == "open" then
        play_small_reveal()
        scaleform.Actor.set_visible(main, true)
        scaleform.AnimationComponent.goto_frame(mainAnimation, 1);
        scaleform.AnimationComponent.play(mainAnimation)
        scaleform.AnimationComponent.goto_frame(backgroundAnimation, 2)
        scaleform.AnimationComponent.play(backgroundAnimation)
        -- Audio swap back to main menu ambient layer 1
        play_main_menu_ambient_layer1()
    elseif e.name == "off" then
        scaleform.Actor.set_visible(options, false)
    end
end )

local customListener = scaleform.EventListener.create(customListener, function(e)
    if e.name == "set_options_values" then
        fov = e.data.fov
        volume = e.data.volume
        audio_on = e.data.audio

        local background_normal = scaleform.Stage.actor_by_name_path("main_menu.options.sound.control.background_normal")
        local content_normal = scaleform.Stage.actor_by_name_path("main_menu.options.sound.control.content_normal")
        local background_checked = scaleform.Stage.actor_by_name_path("main_menu.options.sound.control.background_checked")
        local content_checked = scaleform.Stage.actor_by_name_path("main_menu.options.sound.control.content_checked")

        scaleform.Actor.set_visible(background_normal, not audio_on)
        scaleform.Actor.set_visible(content_normal, not audio_on)
        scaleform.Actor.set_visible(background_checked, audio_on)
        scaleform.Actor.set_visible(content_checked, audio_on)
    end    
end );

scaleform.EventListener.connect(customListener, thisActor,scaleform.EventTypes.Custom)
scaleform.EventListener.connect(mainTimelineTriggerListener, main, scaleform.EventTypes.TimelineTrigger)
scaleform.EventListener.connect(optionsTimelineTriggerListener, options, scaleform.EventTypes.TimelineTrigger)