thisActor = ...

-- Audio functions
local function play_back_sound()
    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_back")
    end
end

local prevButtonIdx = {0,0}
local buttonIdx = {1,1}

local MAIN = 1
local OPTIONS = 2

local widgetCount = {4, 5}

local currentMenu = MAIN

local main_menu_buttons = {}
local main_menu_scripts = {}

local options_widgets = {}
local options_scripts = {}
local optionsListItems = {}
local optionsListAnimations = {}

local closingOptions = false
local keyDown = false

local keyDownListener = scaleform.EventListener.create(keyDownListener, function(e, thisListener)
    
    -- This is used to tell between mouse focus changes in buttons and keyevent changes
    keyDown = true
    
    if e.key == "Down" then
        buttonIdx[currentMenu] = buttonIdx[currentMenu] + 1
    elseif e.key == "Up" then
        buttonIdx[currentMenu] = buttonIdx[currentMenu] - 1
    elseif e.key == "Return" then
        if currentMenu == MAIN then
            main_menu_scripts[buttonIdx[currentMenu]].click()
        elseif currentMenu == OPTIONS then
            options_scripts[buttonIdx[currentMenu]].click()
        end
    elseif e.key == "Right" then
        if currentMenu == OPTIONS and buttonIdx[currentMenu] > 1 and buttonIdx[currentMenu] < 4 then
            local currentValue = options_scripts[buttonIdx[currentMenu]].getValue()
            options_scripts[buttonIdx[currentMenu]].setValue(currentValue + 1)
        elseif currentMenu == OPTIONS and (buttonIdx[currentMenu] == 1 or buttonIdx[currentMenu] == 4) then
            options_scripts[buttonIdx[currentMenu]].click()
        end
    elseif e.key == "Left" then
        if currentMenu == OPTIONS and buttonIdx[currentMenu] > 1 and buttonIdx[currentMenu] < 4 then
            local currentValue = options_scripts[buttonIdx[currentMenu]].getValue()
            options_scripts[buttonIdx[currentMenu]].setValue(currentValue - 1)
        elseif currentMenu == OPTIONS and (buttonIdx[currentMenu] == 1 or buttonIdx[currentMenu] == 4) then
            options_scripts[buttonIdx[currentMenu]].click()
        end
    elseif e.key == "Escape" then
        if currentMenu == OPTIONS and not closingOptions then
            local options = scaleform.Stage.actor_by_name_path("main_menu.options")
            local animation = scaleform.Actor.component_by_name(options, "animation")
            scaleform.AnimationComponent.goto_label(animation, "close")
            scaleform.AnimationComponent.play(animation)
            closingOptions = true
        end
        play_back_sound()
    end

    if buttonIdx[currentMenu] > widgetCount[currentMenu] then
        buttonIdx[currentMenu] = 1
    elseif buttonIdx[currentMenu] < 1 then
        buttonIdx[currentMenu] = widgetCount[currentMenu]
    end
end )

local mainButtonOverEventListener = scaleform.EventListener.create(mainButtonOverEventListener, function(e, thisListener)
    local name = scaleform.Actor.name(e.target)
    if name == "start" then
        buttonIdx[currentMenu] = 1
    elseif name == "options" then
        buttonIdx[currentMenu] = 2
    elseif name == "credits" then
        buttonIdx[currentMenu] = 3
    elseif name == "quit" then
        buttonIdx[currentMenu] = 4
    end
end )

local optionsButtonOverEventListener = scaleform.EventListener.create(optionsButtonOverEventListener, function(e, thisListener)
    local targetParent = scaleform.Actor.parent(e.target)
    local name = scaleform.Actor.name(targetParent)
    if name == "list_item" then
        name = scaleform.Actor.name(scaleform.Actor.parent(targetParent))
    end

    if name == "sound" then
        buttonIdx[currentMenu] = 1
    elseif name == "volume" then
        buttonIdx[currentMenu] = 2
    elseif name == "fov" then
        buttonIdx[currentMenu] = 3
    elseif name == "antialiasing" then
        buttonIdx[currentMenu] = 4
    end
end )

local enterFrameListener = scaleform.EventListener.create(enterFrameListener, function(e, thisListener)
    if not keyDown then
        keyDown = false;
        prevButtonIdx[currentMenu] = buttonIdx[currentMenu]
        return;
    end
    
    if buttonIdx[currentMenu] ~= prevButtonIdx[currentMenu] then
        if prevButtonIdx[currentMenu] > 0 and prevButtonIdx[currentMenu] < widgetCount[currentMenu] + 1 then
            if currentMenu == MAIN then
                main_menu_scripts[prevButtonIdx[currentMenu]].normal()
            elseif currentMenu == OPTIONS then
                options_scripts[prevButtonIdx[currentMenu]].normal()
                    
                if prevButtonIdx[currentMenu] < widgetCount[currentMenu] then
                    scaleform.AnimationComponent.play_label(optionsListAnimations[prevButtonIdx[currentMenu]], "normal")
                end
            end
        end
        
        if currentMenu == MAIN then
            main_menu_scripts[buttonIdx[currentMenu]].over()
        elseif currentMenu == OPTIONS then
            options_scripts[buttonIdx[currentMenu]].over()
            if buttonIdx[currentMenu] < widgetCount[currentMenu] then
                scaleform.AnimationComponent.play_label(optionsListAnimations[buttonIdx[currentMenu]], "over")
            end
        end

        prevButtonIdx[currentMenu] = buttonIdx[currentMenu]
    end
end )

local addedListener = scaleform.EventListener.create(addedListener, function(e, thisListener)
    main_menu_buttons[1] = scaleform.Stage.actor_by_name_path("main_menu.main.start")
    main_menu_buttons[2] = scaleform.Stage.actor_by_name_path("main_menu.main.options")
    main_menu_buttons[3] = scaleform.Stage.actor_by_name_path("main_menu.main.credits")
    main_menu_buttons[4] = scaleform.Stage.actor_by_name_path("main_menu.main.quit")
    
    options_widgets[1] = scaleform.Stage.actor_by_name_path("main_menu.options.sound.control")
    options_widgets[2] = scaleform.Stage.actor_by_name_path("main_menu.options.volume.control")
    options_widgets[3] = scaleform.Stage.actor_by_name_path("main_menu.options.fov.control")
    options_widgets[4] = scaleform.Stage.actor_by_name_path("main_menu.options.antialiasing.control")
    options_widgets[5] = scaleform.Stage.actor_by_name_path("main_menu.options.ok")
    
    optionsListItems[1] = scaleform.Stage.actor_by_name_path("main_menu.options.sound.list_item")
    optionsListItems[2] = scaleform.Stage.actor_by_name_path("main_menu.options.volume.list_item")
    optionsListItems[3] = scaleform.Stage.actor_by_name_path("main_menu.options.fov.list_item")
    optionsListItems[4] = scaleform.Stage.actor_by_name_path("main_menu.options.antialiasing.list_item")
    
    for idx = 1, 4 do
        local comp = scaleform.Actor.component_by_name(main_menu_buttons[idx], "WidgetBase")
        scaleform.EventListener.connect(mainButtonOverEventListener, main_menu_buttons[idx], scaleform.EventTypes.MouseOver)
        main_menu_scripts[idx] = scaleform.ScriptComponent.script_results(comp)
        optionsListAnimations[idx] = scaleform.Actor.component_by_name(optionsListItems[idx], "animation")
    end
    
    for idx = 1, 5 do
        local comp = scaleform.Actor.component_by_name(options_widgets[idx], "WidgetBase")
        scaleform.EventListener.connect(optionsButtonOverEventListener, scaleform.Actor.parent(options_widgets[idx]), scaleform.EventTypes.MouseOver)
        options_scripts[idx] = scaleform.ScriptComponent.script_results(comp)
    end
end )

local mainTimelineTriggerListener = scaleform.EventListener.create(mainTimelineTriggerListener, function(e, thisListener)
    if e.name == "open" then
        currentMenu = OPTIONS
        buttonIdx[currentMenu] = 1
        closingOptions = false
    end
end )

local optionsTimelineTriggerListener = scaleform.EventListener.create(optionsTimelineTriggerListener, function(e, thisListener)
    if e.name == "open" then
        currentMenu = MAIN
    end
end )

local sceneAnimation = scaleform.Actor.component_by_name(thisActor, "animation")
local main = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "main")
scaleform.EventListener.connect(mainTimelineTriggerListener, main, scaleform.EventTypes.TimelineTrigger)
local options = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "options")
scaleform.EventListener.connect(optionsTimelineTriggerListener, options, scaleform.EventTypes.TimelineTrigger)

scaleform.EventListener.connect(addedListener, thisActor, scaleform.EventTypes.Added)
scaleform.EventListener.connect(keyDownListener, scaleform.EventTypes.KeyDown)
scaleform.EventListener.connect(enterFrameListener, thisActor, scaleform.EventTypes.EnterFrame)