
---- Randomly selects music files and offers some modification methods ----
-------------------------------------------------------------------------------

MusicPlayer = {
	menulist = System.listDirectory("music/menu/"),
	gamelist = System.listDirectory("music/game/"),
	bosslist = System.listDirectory("music/boss/"),
	fast = false, oldTurboBpm = 0,
	slow = false, oldStopBpm = 0,
	paused = false,
	title = "", composer = ""
}

function MusicPlayer:playMenu()
	self:play(self.menulist, "music/menu/")
end

function MusicPlayer:playGame()
	self:play(self.gamelist, "music/game/")
end

function MusicPlayer:playBoss()
	self:play(self.bosslist, "music/boss/")
end

function MusicPlayer:play(list, path)
	self:stopTurboSpeed()
	self:stopStopSpeed()
	Music.stop()
	local f = list[math.random(3, table.getn(list))] -- skip . and ..
	Music.playFile(path .. f.name, false)
	Music.volume(50)
	
	self.title = Music.title()
	self.composer = Music.composer()
end

function MusicPlayer:turboSpeed()
	if not self.fast then
		self.fast = true
		self.oldTurboBpm = Music.bpm()
		Music.bpm(self.oldTurboBpm*1.7)
	end
end
function MusicPlayer:stopTurboSpeed()
	if self.fast then
		self.fast = false
		Music.bpm(self.oldTurboBpm)
	end
end

function MusicPlayer:stopSpeed()
	if not self.slow then
		self.slow = true
		self.oldStopBpm = Music.bpm()
		Music.bpm(self.oldStopBpm*0.6)
	end
end
function MusicPlayer:stopStopSpeed()
	if self.slow then
		self.slow = false
		Music.bpm(self.oldStopBpm)
	end
end

function MusicPlayer:togglePause()
	if self.paused then Music.resume()
	else Music.pause() end
	self.paused = not self.paused
end

function MusicPlayer:description()
	if self.composer and string.len(self.composer) > 0 then
		return self.title .. " by " .. self.composer
	else return self.title end
end
