local thisActor = ... -- our hud scene
local paused = false
local prevButtonIdx = 0
local buttonIdx = 1
local pause_menu_buttons = {}
local pause_menu_scripts = {}
local widgetCount = 2

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

-- hud
local hudActor = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "hud")
local hudAnimation = scaleform.Actor.component_by_name(hudActor, "animation")

local topLeft = scaleform.ContainerComponent.actor_by_name(hudAnimation, "top_left")
local topRight = scaleform.ContainerComponent.actor_by_name(hudAnimation, "top_right")
local bottomLeft = scaleform.ContainerComponent.actor_by_name(hudAnimation, "bottom_left")
local bottomRight = scaleform.ContainerComponent.actor_by_name(hudAnimation, "bottom_right")

-- pause
local pause = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "pause")
local pauseAnimation = scaleform.Actor.component_by_name(pause, "animation")

-- background
local background = scaleform.ContainerComponent.actor_by_name(sceneAnimation, "background")
scaleform.Actor.set_visible(pause, false)
scaleform.Actor.set_visible(background, false)

scaleform.Stage.set_view_scale_mode(scaleform.ViewScaleModes.NoBorder)

-- align content
local function updateViewport()
    -- Get the new viewport size
    local stageSz = scaleform.Stage.dimensions()
    local viewFrame = scaleform.Stage.visible_frame_rect()
    -- Affix background on right edge
    local xshift = (stageSz.width - (viewFrame.x2 - viewFrame.x1)) * 0.5
    local yshift = (stageSz.height - (viewFrame.y2 - viewFrame.y1)) * 0.5

    scaleform.Actor.set_local_position(topLeft, {x = -640 + xshift, y = -360 - yshift})
    scaleform.Actor.set_local_position(topRight, {x = 640 - xshift, y = -360 - yshift})
    scaleform.Actor.set_local_position(bottomLeft, {x = -640 + xshift, y = 360 - yshift})
    scaleform.Actor.set_local_position(bottomRight, {x = 640 - xshift, y = 360 - yshift})
end

-- keyboard listener
local keyDownListener = scaleform.EventListener.create(keyDownListener, function(e, thisListener)
    if e.key == "Escape" then
        if not paused then
            scaleform.AnimationComponent.goto_label(pauseAnimation, "open")
            scaleform.AnimationComponent.play(pauseAnimation)
            scaleform.Actor.set_visible(pause, true)
            scaleform.Actor.set_visible(background, true)
        else
            scaleform.AnimationComponent.goto_label(pauseAnimation, "close")
            scaleform.AnimationComponent.play(pauseAnimation)
        end
        
        paused = not paused
        
        local evt = {   eventId = scaleform.EventTypes.Custom, 
                        name = "pause",
                        data = {}
                    }

        if not paused then
            evt.name = "unpause"
        end

        scaleform.Stage.dispatch_event(evt)
        
    elseif e.key == "Down" then
        buttonIdx = buttonIdx + 1
    elseif e.key == "Up" then
        buttonIdx = buttonIdx - 1
    elseif e.key == "Return" then
        pause_menu_scripts[buttonIdx].click()
    end
    
    if buttonIdx > widgetCount then
        buttonIdx = 1
    elseif buttonIdx < 1 then
        buttonIdx = widgetCount
    end
end )

-- button udpates
local enterFrameListener = scaleform.EventListener.create(enterFrameListener, function(e, thisListener)
    if paused and buttonIdx ~= prevButtonIdx then
        if prevButtonIdx > 0 and prevButtonIdx < widgetCount + 1 then
            pause_menu_scripts[prevButtonIdx].normal()
        end
        
        pause_menu_scripts[buttonIdx].over()

        prevButtonIdx = buttonIdx
    end
end )

local pauseButtonOverEventListener = scaleform.EventListener.create(pauseButtonOverEventListener, function(e, thisListener)
    local name = scaleform.Actor.name(e.target)
    if name == "main_menu" then
        buttonIdx = 1
    elseif name == "resume" then
        buttonIdx = 2
    end
end )

local customListener = scaleform.EventListener.create(customListener, function(e)
    if e.name == "unpause" then
        paused = false
    end    
end );

local pauseTimelineTriggerListener = scaleform.EventListener.create(pauseTimelineTriggerListener, function(e)
    if e.name == "off" then
        scaleform.Actor.set_visible(pause, false)
    end
end)

-- ============================================================================
-- Event Handlers
-- ============================================================================
-- On viewport resize, we resposition the controls
local viewportResizeEventListener = scaleform.EventListener.create(viewportResizeEventListener, function(e)
	updateViewport()
end )

-- pause menu buttons
pause_menu_buttons[1] = scaleform.ContainerComponent.actor_by_name(pauseAnimation, "main_menu")
pause_menu_buttons[2] = scaleform.ContainerComponent.actor_by_name(pauseAnimation, "resume")

for idx = 1, 2 do
    scaleform.EventListener.connect(pauseButtonOverEventListener, pause_menu_buttons[idx], scaleform.EventTypes.MouseOver)
    
    local comp = scaleform.Actor.component_by_name(pause_menu_buttons[idx], "WidgetBase")
    pause_menu_scripts[idx] = scaleform.ScriptComponent.script_results(comp)
end

scaleform.EventListener.connect(customListener, thisActor,scaleform.EventTypes.Custom)
scaleform.EventListener.connect(viewportResizeEventListener, thisActor, scaleform.EventTypes.ViewportResize)
scaleform.EventListener.connect(keyDownListener, scaleform.EventTypes.KeyDown)
scaleform.EventListener.connect(enterFrameListener, thisActor, scaleform.EventTypes.EnterFrame)
scaleform.EventListener.connect(pauseTimelineTriggerListener, pause, scaleform.EventTypes.TimelineTrigger)

updateViewport()