---------------------------------------------------------------------------------
-- Helpers sound - all sound related functions
---------------------------------------------------------------------------------

---------------------------------------------------------------------------------
sound_types =
{
    sound_track = 0,
    sfx_track = 1,
    music_track = 2,
    ambient_track = 3,
    voice_track = 4,
}
---------------------------------------------------------------------------------


-------------------------------------------------------------------------------
function getCommonSoundEntity(sound_name)
    local sound_entity = getGameRoot():getEntityNamed(sound_name)

    if not sound_entity then
        local soundPath = 'Data/Sounds/Common/'..sound_name..'.ogg'

        if resourceExists(soundPath) then
            sound_entity = CreateSFXTrack(soundPath)
            sound_entity:setName(sound_name)
        end
    end
    
    return sound_entity
end

-------------------------------------------------------------------------------
function playCommonSound(sound_name)
    local sound_entity = getCommonSoundEntity(sound_name)

    if sound_entity then
        sound_entity:play()
    end
end

-------------------------------------------------------------------------------
function parseSounds(folder)
    local tracks = {}        
        local vecSounds = getResourceFolderFiles(folder)
    
        for i = 0, vecSounds:size() - 1 do
            local sound_path = vecSounds:get(i)
            local name       = ExtractFileName(sound_path)

            if string.find(sound_path, '.ogg') then
                tracks[name] = 
                {
                    ['path'] = sound_path,
                    ['entity'] = nil,
                    ['repeated'] = false,
                    ['type'] = sound_types.sound_track,
                }
            end

        end
    
    return tracks
end

-------------------------------------------------------------------------------
function parseLevelSounds(level_name)
    if IsLevelCollectorsEdition(level_name) then
        return parseSounds('Data/ExtraContent/Sounds/Scenes/' .. level_name:sub(1))
    else
        return parseSounds('Data/Sounds/Scenes/' .. level_name:sub(1))
    end
end

-------------------------------------------------------------------------------
function parseMusic()
    return parseSounds('Data/Music/')
end

-------------------------------------------------------------------------------
function parseAmbients()
    local ambients_list = parseSounds('Data/Sounds/InterNoise/')
    
    if isCollectorsEdition() then
        local ambients_list_CE = parseSounds('Data/ExtraContent/Sounds/InterNoise/')
        for k, v in pairs(ambients_list_CE) do
            ambients_list[k] = v
        end        
    end
    
    for k, v in pairs(ambients_list) do
        v.repeated = true
    end

    return ambients_list
end

-------------------------------------------------------------------------------
function changeAudioVolume(entity, start_volume, volume, time)
	if entity.properties == nil then
		entity.properties = {}
	end

	entity.destroyEffectors =
		function(entity)
			if entity.properties then
				if entity.properties.volume_fade_signal_connection then
					entity.properties.volume_fade_signal_connection:remove()
					entity.properties.volume_fade_signal_connection = nil
				end
			end
		end
	
	entity:destroyEffectors()
	
	if start_volume then
		entity:setVolume(start_volume)
	else
		start_volume = entity:getVolume()
	end

	local accumulated_time = 0
	entity.properties.volume_fade_signal_connection = entity.onUpdate:add(
		function(entity, dt)
			accumulated_time = accumulated_time + dt
			entity:setVolume(start_volume + (volume - start_volume)*(accumulated_time/time))
			if accumulated_time >= time then
				entity:destroyEffectors()
				entity:setVolume(volume)
			end
		end
	)
end

function AudioTrack:changeVolume(start_volume, volume, time)
	changeAudioVolume(self, start_volume, volume, time)
end

function AudioTrack:changeVolumeTo(volume, time)
	changeAudioVolume(self, nil, volume, time)
end

function SoundTrack:changeVolume(start_volume, volume, time)
	changeAudioVolume(self, start_volume, volume, time)
end

function SoundTrack:changeVolumeTo(volume, time)
	changeAudioVolume(self, nil, volume, time)
end

function MusicTrack:changeVolume(start_volume, volume, time)
	changeAudioVolume(self, start_volume, volume, time)
end
  
function MusicTrack:changeVolumeTo(volume, time)
	changeAudioVolume(self, nil, volume, time)
end

function AmbientTrack:changeVolume(start_volume, volume, time)
	changeAudioVolume(self, start_volume, volume, time)
end
  
function AmbientTrack:changeVolumeTo(volume, time)
	changeAudioVolume(self, nil, volume, time)
end

function VoiceTrack:changeVolume(start_volume, volume, time)
	changeAudioVolume(self, start_volume, volume, time)
end
  
function VoiceTrack:changeVolumeTo(volume, time)
	changeAudioVolume(self, nil, volume, time)
end
