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

scene.inputHandler = {
    AButtonDown = function()
        if Drones[scene.menuIndex].locked == true then
            return
        end
        scene.menuConfirmSound:play(1)
        local mode = Drones[scene.menuIndex].mode
        local soundTable = playdate.sound.playingSources()
        for i=1, #soundTable do
            soundTable[i]:stop()
        end
        if mode == Modes.bomber then
            Noble.transition(BomberScene)
        else
            Noble.transition(Assemble)
        end
    end,
    BButtonDown = function()
        scene.menuBackSound:play(1)
        Noble.transition(MapSelector)
    end,
    downButtonDown = function()
    end,
    leftButtonDown = function()
        if scene.menuIndex <= 1 then
            return
        end
        scene.menuSelSound:play(1)
        scene.targetX = scene.targetX + 355
        scene.menuIndex = scene.menuIndex - 1
        scene.paginator:set(scene.menuIndex)
    end,
    rightButtonDown = function()
        if scene.menuIndex >= 4 then
            return
        end
        scene.menuSelSound:play(1)
        scene.targetX = scene.targetX - 355
        scene.menuIndex = scene.menuIndex + 1
        scene.paginator:set(scene.menuIndex)
    end,
    upButtonDown = function()
    end,
}

function scene:setValues()
    self.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:init()
    scene.super.init(self)
    scene:setValues()
end

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

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

function scene:enter()
    scene.super.enter(self)
    scene.cards = {}
    for i = 1, #Drones do
        scene.cards[i] = DroneCard(0, 0, Drones[i])
    end
    scene.paginator = PageSprite(200, 207)
end

function scene:update()
    scene.super.update(self)
    if scene.cards == nil then
        print("scene.cards is nil")
        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 = 29 + (339 + 16) * (i - 1)
        scene.cards[i]:moveTo(x + scene.currentX, 25)
    end

    if Drones[scene.menuIndex].locked == false then
        self.aKey:draw(315, 207 + dy)
        Noble.Text.draw("Assemble", 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)

    scene.paginator:moveTo(200, 207)
end

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

function scene:finish()
    scene.super.finish(self)
    playdate.display.setScale(1)
end
