MapSelector = {}
class("MapSelector").extends(BaseScene)
local scene = MapSelector
local fontMed = Graphics.font.new('assets/fonts/onyx_9')
local fontBig = Graphics.font.new('assets/fonts/opal_12')
local elapsedTime = 0

function scene:init()
    playdate.graphics.setImageDrawMode(playdate.graphics.kDrawModeXOR)
    scene.super.init(self)
    scene.menuIndex = 1

    self.aKey = Graphics.image.new("assets/sprites/buttons/A")
    self.bKey = Graphics.image.new("assets/sprites/buttons/B")

    scene.menuConfirmSound = playdate.sound.fileplayer.new("assets/audio/confirm")
    scene.menuBackSound = playdate.sound.fileplayer.new("assets/audio/back")

    scene.menuSelSound = playdate.sound.fileplayer.new("assets/audio/menu_select")
    scene.menuSelSound:setVolume(0.5)

    scene.currentX = 0
    scene.targetX = 0
end

function scene:start()
    scene.super.start(self)

    Noble.showFPS = false

    self.optionsMenu:addMenuItem("Main Menu", function() Noble.transition(Menu) end)
end


function scene:enter()
    scene.super.enter(self)
    scene.cards = {}
    for i = 1, #Maps do
        scene.cards[i] = MapCard(0, 0, Maps[i])
    end
end

function scene:update()
    scene.super.update(self)
    if not scene.cards then return end
    elapsedTime = elapsedTime + 1 / playdate.display.getRefreshRate()

    local dy = 2 * math.sin(20 * elapsedTime)

    local speed = 40
    if math.abs(scene.targetX - scene.currentX) < speed then
        scene.currentX = scene.targetX
    else
        scene.currentX = scene.currentX + speed * ((scene.targetX > scene.currentX) and 1 or -1)
    end

    local x = 0
    for i = 1, #scene.cards do
        x = 400 * (i - 1)
        scene.cards[i]:moveTo(x + scene.currentX, 0)
    end

    -- Bottom background
    if Maps[scene.menuIndex].locked == false then
        self.aKey:draw(315, 207 + dy)
        Noble.Text.draw("Select", 333, 210, Noble.Text.ALIGN_LEFT, false, fontMed)
    end

    self.bKey:draw(15, 207 + dy)
    Noble.Text.draw("Back", 33, 210, Noble.Text.ALIGN_LEFT, false, fontMed)

    Noble.Text.draw(string.upper(Maps[scene.menuIndex].name), 200, 210, Noble.Text.ALIGN_CENTER, false, fontBig)
    

end

function scene:exit()
    scene.super.exit(self)
    for i = 1, #scene.cards do
        scene.cards[i]:remove()
    end
    Noble.showFPS = false
    playdate.graphics.setImageDrawMode(playdate.graphics.kDrawModeXOR)
end

function scene:finish()
    scene.super.finish(self)
    for i = 1, #scene.cards do
        scene.cards[i]:remove()
    end
    playdate.display.setScale(1)
end

scene.inputHandler = {
    AButtonDown = function()
        if Maps[scene.menuIndex].locked then
            return
        end
        scene.menuConfirmSound:play(1)
        Noble.transition(DroneCardSelector)
    end,
    BButtonDown = function()
        scene.menuBackSound:play(1)
        Noble.transition(Menu)
    end,
    leftButtonDown = function()
        if scene.menuIndex <= 1 then
            return
        end
        scene.menuSelSound:play(1)
        scene.targetX = scene.targetX + 400
        scene.menuIndex = scene.menuIndex - 1
    end,
    rightButtonDown = function()
        if scene.menuIndex >= #Maps then
            return
        end
        scene.menuSelSound:play(1)
        scene.targetX = scene.targetX - 400
        scene.menuIndex = scene.menuIndex + 1
    end,
    upButtonDown = function()
    end,
}