--------------------------------------------------------------------------------
currentMusicTrack = nil
previousMusicTrack = nil
--------------------------------------------------------------------------------

function LoadSounds(folder)
    local tracks = {}
    
    local vecSounds = ParseDirectory(folder)
    
    for i = 0, vecSounds:size() - 1 do
        local sound_path = vecSounds:get(i)
        local name = ExtractFileName(sound_path)
        
        LoadSound(name, sound_path)
        
        table.insert(tracks, name)
    end
    
    return tracks
end
--------------------------------------------------------------------------------

function LoadMusics(folder, callback_f)
    local vecMusics = ParseDirectory(folder)
    for i = 0, vecMusics:size() - 1 do
        local music_path = vecMusics:get(i)
        local name = ExtractFileName(music_path)
        
        LoadMusic(name, music_path)
        
        if callback_f then
            callback_f(name)
        end
    end
end
-------------------------------------------------------------------------------

function MakeSoundEntity(soundName)
    local result = CreateSoundEntity(soundName)
    result:AttachTo(play_field)
    return result
end
-------------------------------------------------------------------------------

function MakeVoiceEntity(voiceName)
    local result = CreateVoiceEntity(voiceName)
    result:AttachTo(play_field)
    return result
end
-------------------------------------------------------------------------------

function MakeSoundEntities(Sounds)
    assert( type(Sounds) == 'table' )
    local result = {}
    
    for i,soundName in pairs(Sounds) do
        local soundEtity = CreateSoundEntity(soundName)
        soundEtity:AttachTo(play_field)
        
        result[soundName] = soundEtity
    end
    return result
end
--------------------------------------------------------------------------------

function MakeVoiceEntities(voicesNames)
	local voices = {}
	
	for i, name in ipairs(voicesNames) do
	    local voiceEntity = MakeVoiceEntity(name)
	    voiceEntity:AttachTo(play_field)
	    
		voices[name] = voiceEntity
	end
	
	return voices
end
--------------------------------------------------------------------------------

function PlayAmbientMusic(musicTrack, fade, func, target)
    if currentMusicTrack ~= musicTrack then
        if currentMusicTrack then
            StopMusic(currentMusicTrack, fade or false)
        end
        
        currentMusicTrack = musicTrack
        
        if currentMusicTrack then
            if func and target then
                PlayMusic(currentMusicTrack, fade or false, func, target)
            elseif func then
                PlayMusic(currentMusicTrack, fade or false, func)
            else
                PlayMusic(currentMusicTrack, fade or false)
            end
        end
    end
end
--------------------------------------------------------------------------------

function StopAmbientMusic(fade)
    PlayAmbientMusic(nil, fade)
end
--------------------------------------------------------------------------------

function PauseAmbientMusic(fade)
    previousMusicTrack = currentMusicTrack
    StopAmbientMusic(fade)
end
--------------------------------------------------------------------------------

function ContinueAmbientMusic(fade)
    if currentMusicTrack == nil and previousMusicTrack ~= nil then
        PlayAmbientMusic(previousMusicTrack, fade)
        previousMusicTrack = nil
    end
end
--------------------------------------------------------------------------------

function PlayNextRandomAmbientMusic()
    local musicTrack = table.random_value(global_music_list)
    
    if #global_music_list > 1 then
        if currentMusicTrack then
            while musicTrack == currentMusicTrack do
                musicTrack = table.random_value(global_music_list)
            end
        end
    end
    
    PlayAmbientMusic(musicTrack, true, 'OnMusicTrackEnd', GetCurrentLevel())
end

--------------------------------------------------------------------------------
function PlayRandomAmbientMusic()
    local musicTrack = table.random_value(global_music_list)
    
    if currentMusicTrack then
        if not table.has_value(global_music_list, currentMusicTrack) then
            PlayAmbientMusic(musicTrack, true, 'OnMusicTrackEnd', GetCurrentLevel())
        end
    else
        PlayAmbientMusic(musicTrack, true, 'OnMusicTrackEnd', GetCurrentLevel())
    end
end
--------------------------------------------------------------------------------

local previous_ambient_sound_volume = nil
local previous_musics_volume = nil

function VolumeTurnDownAmbientMusic()
    do
        previous_musics_volume = {}
        
        for i, name in ipairs(global_music_list) do
            local volume = GetMusicTrackSound(name)
            SetMusicTrackSound(name, volume * 0.2)
            
            previous_musics_volume[name] = volume
        end
    end
    
    local level = GetCurrentLevel()
    if level.ambient_sound_entety then
        previous_ambient_sound_volume = 1 --level.ambient_sound_entety:GetVolume()
        level.ambient_sound_entety:SetVolume(previous_ambient_sound_volume * 0.3)
    end
end
--------------------------------------------------------------------------------

function VolumeRestoreAmbientMusic()
	if previous_musics_volume then
	    for name, volume in pairs(previous_musics_volume) do
            SetMusicTrackSound(name, volume)
        end
        
        previous_musics_volume = nil
	end
	
	if previous_ambient_sound_volume then
        local level = GetCurrentLevel()
        
        if level.ambient_sound_entety then
            level.ambient_sound_entety:SetVolume(previous_ambient_sound_volume)
        end
        
        previous_ambient_sound_volume = nil
    end
end
--------------------------------------------------------------------------------