Menu = {}
class("Menu").extends(BaseScene)
local scene = Menu

function scene:setValues()
	self.background = Graphics.image.new("assets/images/menuBg")

	self.color1 = Graphics.kColorBlack
	self.color2 = Graphics.kColorWhite

	self.menu = nil
	self.sequence = nil

	self.menuX = 15

	self.menuYFrom = -50
	self.menuY = 15
	self.menuYTo = 240

	scene.musicEnabled = Noble.Settings.get("music")

	scene.levelAudio = playdate.sound.fileplayer.new("assets/audio/menu")
	scene.levelAudio:setVolume(0.7)
end

function scene:init()
	local soundTable = playdate.sound.playingSources()
    for i=1, #soundTable do
        soundTable[i]:stop()
    end
	scene.super.init(self)

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

	local menuConfirmSound = playdate.sound.fileplayer.new("assets/audio/confirm")
	-- menuConfirmSound:setVolume(0.5)

	self:setValues()

	self.menu = Noble.Menu.new(false, Noble.Text.ALIGN_CENTER, false, self.color2, 4, 6, 0, Noble.Text.FONT_SMALL)

	self:setupMenu(self.menu)

	local crankTick = 0

	self.inputHandler = {
		upButtonDown = function()
			self.menu:selectPrevious()
			menuSelSound:play(1)
		end,
		downButtonDown = function()
			self.menu:selectNext()
			menuSelSound:play(1)
		end,
		cranked = function(change, acceleratedChange)
			crankTick = crankTick + change
			if (crankTick > 30) then
				crankTick = 0
				self.menu:selectNext()
				menuSelSound:play(1)
			elseif (crankTick < -30) then
				crankTick = 0
				self.menu:selectPrevious()
				menuSelSound:play(1)
			end
		end,
		AButtonDown = function()
			menuConfirmSound:play(1)
			self.menu:click()
		end
	}
end

function scene:enter()
	scene.super.enter(self)
	self.sequence = Sequence.new():from(self.menuYFrom):to(self.menuY, 1.5, Ease.outBounce):start()

	if scene.musicEnabled then
		scene.levelAudio:play(0)
	end
end

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

	self.menu:activate()
end

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

	self.background:draw(0, 0)
end

local logo_image <const> = Graphics.image.new("assets/images/logo")

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

	Graphics.setColor(self.color1)
	Graphics.setDitherPattern(0.2, Graphics.image.kDitherTypeScreen)
	Graphics.fillRoundRect(200 - 38, 160 - 8, 75, 60, 15)
	self.menu:draw(200, 160)

	-- logo_image:draw(120, 55)

	Graphics.setColor(Graphics.kColorBlack)
end

function scene:exit()
	scene.super.exit(self)
	scene.levelAudio:stop()
	self.sequence = Sequence.new():from(self.menuY):to(self.menuYTo, 0.5, Ease.inSine)
	self.sequence:start()
end

function scene:setupMenu(__menu)
	__menu:addItem("Start", function() Noble.transition(MapSelector, nil, Noble.Transition.DipToWhite) end)
	__menu:addItem("Tutorial", function()
		local debug = Noble.Settings.get("debug")
		if debug then
			Noble.Settings.set("debug", false)
		else
			Noble.Settings.set("debug", true)
		end
		return
	end)
	__menu:addItem("Credits", function() return end)
	__menu:select("Start")
end
