--'-------------------------------------------------------------------------------------------------------------------
--'	Схема озвучки
--'	автор:  Диденко Руслан  (Stohe)
--'--------------------------------------------------------------------------------------------------------------------

--function printf()
--end

sound_table = {}

function set_sound_play(npc_id, sound, faction, point)
	--printf("SET_SOUND_PLAY. theme[%s] object[%s]", tostring(sound), npc_id)
	if sound == nil then
		return
	end
	if sound_theme.theme[sound] == nil then
		--abort("set_sound_play. Wrong sound theme [%s], npc[%s]", tostring(sound), npc_id)
		return
	end
	local snd_theme = sound_theme.theme[sound]
	if snd_theme.class_id == "looped_sound" then
		abort("You trying to play sound [%s] which type is looped", sound)
	end
    if sound_table[npc_id] == nil or snd_theme.play_always == true then
		if sound_table[npc_id] ~= nil then
			--printf("sound table not nil")
			if(sound_table[npc_id].reset~=nil) then
				sound_table[npc_id]:reset(npc_id)
			else
				--printf("--------------"..type(sound_table[npc_id]))
				--printf("npc_id="..npc_id)
				--printf("sound="..sound)
			end
		end
		--printf("PLAY. theme[%s] object[%s]", tostring(sound), npc_id)
        if snd_theme:play(npc_id, faction, point) then
			--printf("PLAY2. theme[%s] object[%s]", tostring(sound), npc_id)
		 	--' fill table
			sound_table[npc_id] = snd_theme
		end
    else
		--printf("xr_sound: cannot play sound [%s] because i'm [%s] already play snd [%s]",tostring(sound), npc_id, sound_table[npc_id].path)
		return sound_table[npc_id].snd_obj
	end
    return sound_table[npc_id] and sound_table[npc_id].snd_obj
end

function update(npc_id)
    if sound_table[npc_id] then
--		local t = type(sound_table[npc_id])
		if not(sound_table[npc_id]:is_playing(npc_id)) then
--		if(t=="string") or not(sound_table[npc_id]:is_playing(npc_id)) then
--	        if(t=="string") then
--				sound_table[npc_id] = sound_theme.theme[sound_table[npc_id]]
--			end

	        printf("SOUND_CALLBACK from [%s]",npc_id)
	        sound_table[npc_id]:callback(npc_id)
	        sound_table[npc_id] = nil
		end
	end
end

looped_sound = {}

function play_sound_looped(npc_id, sound)
	local snd_theme = sound_theme.theme[sound]
	if snd_theme == nil then
		abort("play_sound_looped. Wrong sound theme [%s], npc[%s]", tostring(sound), npc_id)
	end
	if snd_theme.class_id ~= "looped_sound" then
		abort("You trying to play sound [%s] which type is not looped", sound)
	end
	if snd_theme:play(npc_id) then
	    printf("PLAY2. looped theme[%s] object[%s]", tostring(sound), npc_id)
	    if looped_sound[npc_id] == nil then
			looped_sound[npc_id] = {}
		end
		looped_sound[npc_id][sound] = snd_theme
	end
end
function stop_sound_looped(npc_id, sound)
	if sound then
		if type(looped_sound[npc_id][sound]) ~= "string" then
	        if looped_sound[npc_id][sound] and looped_sound[npc_id][sound]:is_playing(npc_id) then
	            printf("looped sound ["..tostring(sound).."] stop for obj [%s]",npc_id)
	            looped_sound[npc_id][sound]:stop()
	            looped_sound[npc_id][sound] = nil
	        end
		end
	else
		if looped_sound[npc_id] then
	        for k,v in pairs(looped_sound[npc_id]) do
	            if v and type(v) ~= "string" and v:is_playing(npc_id) then
	                printf("looped sound ["..tostring(k).."] stop for obj [%s]",npc_id)
	                v:stop()
	            end
	        end
	        looped_sound[npc_id] = nil
		end
	end
end

function set_volume_sound_looped(npc_id, sound, level)
	if looped_sound[npc_id] then
		if looped_sound[npc_id][sound] and looped_sound[npc_id][sound]:is_playing(npc_id) then
			printf("looped sound ["..tostring(sound).."] for obj ["..npc_id.."] set volume "..level)
			looped_sound[npc_id][sound]:set_volume(level)
		end
	end
end

function actor_save(thread)
	set_save_marker(thread, "save", false, "sound_actor_save")
	for k,v in pairs(sound_theme.theme) do
		v:save(thread)
	end
	local n = 0
	for k,v in pairs(sound_table) do
		n = n + 1
	end
	thread:w_u16(n)
	for k,v in pairs(sound_table) do
		thread:w_u16(k)
--[[
		if(type(v.section)~="string") then
			thread:w_stringZ(v)
		else
]]
			thread:w_stringZ(v.section)
--		end
	end
	n = 0
	for k,v in pairs(looped_sound) do
		n = n + 1
	end
	thread:w_u16(n)
	for k,v in pairs(looped_sound) do
		thread:w_u16(k)
        n = 0
		for kk,vv in pairs(looped_sound[k]) do
			n = n + 1
		end
		thread:w_u16(n)
		for kk,vv in pairs(looped_sound[k]) do
            thread:w_stringZ(kk)
--[[
	        if(type(vv.section)~="string") then
	            thread:w_stringZ(vv)
	        else
	            thread:w_stringZ(vv.section)
	        end
]]
		end
	end
	set_save_marker(thread, "save", true, "sound_actor_save")
end
function actor_load(thread)
	set_save_marker(thread, "load", false, "sound_actor_save")
	for k,v in pairs(sound_theme.theme) do
		v:load(thread)
	end
	sound_table = {}
	local n = thread:r_u16()
	for i = 1,n do
		local id = thread:r_u16()
		local theme = thread:r_stringZ()
--		sound_table[id] = thread:r_stringZ()
        sound_table[id] = sound_theme.theme[theme]
	end
	looped_sound = {}
	n = thread:r_u16()
	for i = 1,n do
		local id = thread:r_u16()
		looped_sound[id] = {}
		n = thread:r_u16()
		for i = 1,n do
			local sound = thread:r_stringZ()
--            looped_sound[id][sound] = thread:r_stringZ()
            looped_sound[id][sound] = sound_theme.theme[sound]
		end
	end
	set_save_marker(thread, "load", true, "sound_actor_save")
end

function save_npc(thread, npc_id)
	set_save_marker(thread, "save", false, "sound_npc_save")
	for k,v in pairs(sound_theme.theme) do
		v:save_npc(thread, npc_id)
	end
	set_save_marker(thread, "save", true, "sound_npc_save")
end
function load_npc(thread, npc_id)
	set_save_marker(thread, "load", false, "sound_npc_save")
	for k,v in pairs(sound_theme.theme) do
		v:load_npc(thread, npc_id)
	end
	set_save_marker(thread, "load", true, "sound_npc_save")
end


function start_game_callback()
	sound_table = {}
end

function stop_all_sounds()
	for k,v in pairs(sound_table) do
		if(type(v)~="string") then
			v:stop()
		end
	end
	for k,v in pairs(looped_sound) do
		for k,v in pairs(looped_sound[k]) do
			if v and v:is_playing(npc_id) then
				printf("looped sound ["..tostring(k).."] stopped ")
				v:stop()
			end
		end
	end
end

function stop_sounds_by_id(obj_id)
	local sound = sound_table[obj_id]
	if sound and sound.stop then
		sound:stop()
	end
	local looped_snd = looped_sound[obj_id]
	if looped_snd then
		for k,v in pairs(looped_snd) do
			if v and v:is_playing(obj_id) then
				v:stop()
			end
		end
	end
end