------------------------------------------------------------------------------
-- class 'MusicManager'
------------------------------------------------------------------------------

class 'MusicManager'

------------------------------------------------------------------------------
function MusicManager:__init(game_scene)
	self.game_scene = game_scene
	self.music_list = parseMusic()
	assert(table.size(self.music_list))
	self.ambients_list = parseAmbients()
	assert(table.size(self.ambients_list))
	self.current_music_track = nil
	self.current_ambient_track = nil
end

------------------------------------------------------------------------------
function MusicManager:changeMusic()
	local music_list = self.game_scene:getPlayField():getMusic()
	if self.current_music_track then
		if music_list then
			if not table.has_value(music_list, self.current_music_track) then
				self:stopMusic(self.current_music_track)
				self:playMusic(table.random_value(music_list))
			end
		end
	else
		if not self.game_scene:getPlayField():isOutdoorScene() then
			if music_list then
				self:playMusic(table.random_value(music_list))
			elseif self.music_list then
				self:playMusic(table.random_index(self.music_list))
			end
		end
	end
end

---------------------------------------------------------------------------------------------
function MusicManager:playMusic(music_name)
	assert(self.music_list[music_name])
	local music_entity = self.music_list[music_name].entity
	if not music_entity then
		music_entity = CreateMusicTrack(self.music_list[music_name].path)
		music_entity:setName(music_name)
		music_entity:attachTo(self.game_scene:getMainEntity())
		self.music_list[music_name].entity = music_entity
	end

	music_entity:setFading(1)
	music_entity:play()
	music_entity.onEndTrack:removeAll()
	music_entity.onEndTrack:add(bind(MusicManager.playRandomMusic, self))
	self.current_music_track = music_name

	if self.isVolumeTurnDownMusic then
		music_entity:setVolume(0.1)
	else
		music_entity:setVolume(1)
	end

	-- Console:pushText('playMusic  '..music_name)
end

---------------------------------------------------------------------------------------------
function MusicManager:stopMusic(music_name)
	assert(self.music_list[music_name])
	local music_entity = self.music_list[music_name].entity
	assert(music_entity)

	music_entity:setFading(2)
	music_entity:stop()
	self.current_music_track = nil
end

---------------------------------------------------------------------------------------------
function MusicManager:stopCurrentMusic()
	if self.current_music_track then
		self:stopMusic(self.current_music_track)
	end
end

---------------------------------------------------------------------------------------------
function MusicManager:playRandomMusic()
	local prev_music_track = self.current_music_track
	self:stopCurrentMusic()

	local music_list = self.game_scene:getPlayField():getMusic()
	if music_list then
		if table.size(music_list) > 1 then
			local prev_music_list = table.copy(music_list)
			table.remove_by_value(prev_music_list, prev_music_track)
			self:playMusic( table.random_value(prev_music_list) )
		else
			self:playMusic( music_list[1] )
		end
	else
		local prev_music_list = table.copy(self.music_list)
		prev_music_list[prev_music_track] = nil
		self:playMusic( table.random_index(prev_music_list) )
	end
end

---------------------------------------------------------------------------------------------
function MusicManager:changeAmbient()
	if self.current_ambient_track then
		self:stopAmbient(self.current_ambient_track)
	end

	local ambient_name = self.game_scene:getPlayField():getName()..'_Internoise'
	if self.ambients_list[ambient_name] then
		self:stopCurrentMusic()
		self:playAmbient(ambient_name)
	end
end

---------------------------------------------------------------------------------------------
function MusicManager:playAmbient(ambient_name)
	local ambient_track = self.ambients_list[ambient_name].entity
	if not ambient_track then
		ambient_track = CreateAmbientTrack(self.ambients_list[ambient_name].path)
		ambient_track:setName(ambient_name)
		ambient_track:setRepeated(self.ambients_list[ambient_name].repeated)
		ambient_track:attachTo(self.game_scene:getMainEntity())
		self.ambients_list[ambient_name].entity = ambient_track
	end

	ambient_track:play()
	self.current_ambient_track = ambient_name

	return ambient_track
end

---------------------------------------------------------------------------------------------
function MusicManager:stopAmbient(ambient_name)
	assert(self.ambients_list[ambient_name])
	self.current_ambient_track = nil
	local ambient_track = self.ambients_list[ambient_name].entity
	assert(ambient_track)

	ambient_track:setFading(2)
	ambient_track:stop()
end

---------------------------------------------------------------------------------------------
function MusicManager:volumeTurnDownMusic()
	for _, v in pairs(self.music_list) do
		if v.entity then
			v.entity:setVolume(0.1)
		end
	end
end

---------------------------------------------------------------------------------------------
function MusicManager:volumeRestoreMusic()
	for _, v in pairs(self.music_list) do
		if v.entity then
			v.entity:setVolume(1)
		end
	end 

	self.isVolumeTurnDownMusic = nil
end

---------------------------------------------------------------------------------------------
function MusicManager:volumeTurnDownInternoise()
    if self.ambients_list[self.current_ambient_track] then
        self.ambients_list[self.current_ambient_track].entity:setVolume(0.1)
    end
    self.isVolumeTurnDownInternoise = true
end

---------------------------------------------------------------------------------------------
function MusicManager:volumeRestoreInternoise()
    if self.ambients_list[self.current_ambient_track] then
        self.ambients_list[self.current_ambient_track].entity:setVolume(1)
    end
    self.isVolumeTurnDownInternoise = false
end