-- nv170410 used global actor, some optimizations
-- nv170620 fix npc:alive() test in send_tip()
-- nv180918 replaced secret level abbreviation with localized level name
-- nv180925 implemented MrSeyker's fix for missing sr_tip icons reported by zZz1
-- nv181119 corrected agr_u entry in secret_level table (was agru)

-- nv071003 how long the news is displayed before fading away (in milliseconds)
local news_display_time = 5000

-- nv071003 the number of seconds the main task is displayed centered on the screen
local banner_seconds = 7

local xr_sound_get_safe_sound_object = xr_sound.get_safe_sound_object

news = {
	tips_esc_trader_about_anomalies	= xr_sound_get_safe_sound_object([[characters_voice\scenario\trader\trader_tutorial_anomalies_1]]),
	gar_dolg_warning				= xr_sound_get_safe_sound_object([[characters_voice\scenario\duty\duty_warning1]]),
	esc_return_dv					= xr_sound_get_safe_sound_object([[characters_voice\scenario\trader\return_from_dv]]),
	escape_fox_quest				= xr_sound_get_safe_sound_object([[characters_voice\scenario\trader\trader_pda_fox]]),
	tip_petruha_report				= xr_sound_get_safe_sound_object([[characters_voice\scenario\escape\petruha_raport_p]]),


	tips_agr_krot_sos				= xr_sound_get_safe_sound_object([[characters_voice\scenario\agroprom\krot_help_pda_1]]),
	tips_agr_krot_sos1				= xr_sound_get_safe_sound_object([[characters_voice\scenario\agroprom\krot_help_pda_2]]),
	tips_agr_krot_sos2				= xr_sound_get_safe_sound_object([[characters_voice\scenario\agroprom\krot_help_pda_2]]),
	tips_agr_stalker_help_2			= xr_sound_get_safe_sound_object([[characters_voice\scenario\agroprom\stalker_help_2]]),
	tips_agr_stalker_help_1			= xr_sound_get_safe_sound_object([[characters_voice\scenario\agroprom\stalker_help_1]]),
	-- ghost_tips						= xr_sound_get_safe_sound_object([[characters_voice\scenario\agroprom\strelok_pda]]),
	tips_agr_trader_documents		= xr_sound_get_safe_sound_object([[characters_voice\scenario\agroprom\trader_pda_1]]),
	pass_to_1st_door                = xr_sound_get_safe_sound_object([[characters_voice\scenario\val\door1_password]]),
	pass_to_2nd_door                = xr_sound_get_safe_sound_object([[characters_voice\scenario\val\door2_password]]),


	bar_ecolog_crush_heli_down		= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\wolfhound_pda_1]]),
	bar_freedom_attack_attract_actor= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\wolfhound_pda_2]]),
	bar_freedom_spam_1				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\wolfhound_pda_6]]),
	bar_freedom_spam_2				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\wolfhound_pda_4]]),
	bar_freedom_spam_3				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\wolfhound_pda_3]]),
	bar_freedom_spam_4				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\wolfhound_pda_5]]),
	bar_freedom_attack				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\wolfhound_pda_7]]),
	bar_freedom_attack_spy			= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\wolfhound_pda_8]]),
	bar_ecolog_crush_start_heli		= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\kruglov_pda_1]]),
	bar_ecolog_crush_attract_actor  = xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\kruglov_pda_2]]),
	bar_ecolog_spam_1				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\kruglov_pda_3]]),
	bar_ecolog_spam_2				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\kruglov_pda_4]]),
	bar_ecolog_spam_3				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\kruglov_pda_5]]),
	bar_ecolog_spam_4				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\kruglov_pda_6]]),
	bar_ecolog_attack				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\Heli_crush\kruglov_pda_7]]),
	rostok_kruglov_spam_1			= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\kruglov_pda_help_1]]),
	rostok_kruglov_spam_2			= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\kruglov_pda_help_3]]),
	bar_ecolog_escape				= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\volkodav_pda_kruglov_escape_1]]),
	storyline_vasilyev_tip			= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\vasiliev_pda]]),
	storyline_ghost_tip 			= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\ghost_pda]]),
	yan_saharov_message 			= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\professor_to_actor_pda_3]]),
	yan_saharov_message_2 			= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\professor_to_actor_pda_4]]),
	yan_saharov_message_3 			= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\professor_to_actor_pda_5]]),

	yan_scientist_probe				= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\kruglov_radiation_quest_13]]),
	yan_scientist_probe_1			= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\kruglov_radiation_quest_11]]),
	yan_scientist_probe_2			= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\kruglov_radiation_quest_12]]),
	yan_scientist_probe_3			= xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\kruglov_radiation_quest_6]]),

	rostok_kruglov_follow			= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\pda_kruglov_help_6]]),
	bar_freedom_chase			= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\pda_kruglov_stop_enemy_1]]),
	rostok_kruglov_follow_2			= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\pda_kruglov_stop_enemy_2]]),
	rostok_kruglov_follow_3			= xr_sound_get_safe_sound_object([[characters_voice\scenario\Rostok\pda_kruglov_help_5]]),

	bar_territory_dolg_1_hit		= xr_sound_get_safe_sound_object([[characters_voice\scenario\bar\pda\voronin_gunfire_pda_1]]),
	bar_territory_dolg_2_hit		= xr_sound_get_safe_sound_object([[characters_voice\scenario\bar\pda\voronin_gunfire_pda_2]]),
	bar_territory_dolg_3_hit		= xr_sound_get_safe_sound_object([[characters_voice\scenario\bar\pda\voronin_gunfire_pda_3]]),
	bar_territory_dolg_kill			= xr_sound_get_safe_sound_object([[characters_voice\scenario\bar\pda\voronin_gunfire_pda_4]]),

	tips_bun_komand                 = xr_sound_get_safe_sound_object([[characters_voice\scenario\bun\patrol_prikaz]]),

	general_ecolog_tip_1            = xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\professor_to_actor_pda_1]]),
	general_ecolog_tip_2            = xr_sound_get_safe_sound_object([[characters_voice\scenario\yantar\professor_to_actor_pda_2]]),


	tips_gar_hellcar_alarm			= xr_sound_get_safe_sound_object([[characters_voice\scenario\garbage\neutrals_commander_pda_1]]),
	gar_dolg_blokpost_warning		= xr_sound_get_safe_sound_object([[characters_voice\scenario\duty\duty_warning1]]),
	gar_dolg_monster_rush			= xr_sound_get_safe_sound_object([[characters_voice\scenario\duty\duty_request1]]),
	gar_direction_fire				= xr_sound_get_safe_sound_object([[characters_voice\scenario\garbage\junkyard_combat_ambush]]),
	gar_hellcar_victory				= xr_sound_get_safe_sound_object([[characters_voice\scenario\garbage\junkyard_combat_end]]),
	gar_actor_looser				= xr_sound_get_safe_sound_object([[characters_voice\scenario\garbage\duty_after_rush_bad]]),
	gar_actor_normal				= xr_sound_get_safe_sound_object([[characters_voice\scenario\garbage\duty_after_rush_normal]]),
	gar_actor_winner				= xr_sound_get_safe_sound_object([[characters_voice\scenario\garbage\duty_after_rush_good]]),


	esc_direction_fire				= xr_sound_get_safe_sound_object([[characters_voice\scenario\escape\lager_fanat_attack]]),
	esc_fanat_victory				= xr_sound_get_safe_sound_object([[characters_voice\scenario\escape\lager_fanat_victory]]),

	rad_barman_spam					= xr_sound_get_safe_sound_object([[characters_voice\scenario\radar\rad_barman_message]]),


	val_monolith_trader_pda1		= xr_sound_get_safe_sound_object([[characters_voice\scenario\val\trader_dialog1]]),
	o_soznanie_text					= xr_sound_get_safe_sound_object([[characters_voice\scenario\sarcofag\o_soznanie_call]]),


-- dublicate pda sounds for remark scheme
	val_rob_leader_jeer_1           = xr_sound_get_safe_sound_object([[characters_voice\scenario\val\rob_leader_jeer_1_p]]),
--	val_rob_leader_call             = xr_sound_get_safe_sound_object([[characters_voice\scenario\val\rob_call_1_p]]),

	pri_followers_leader_phrase1_1  = xr_sound_get_safe_sound_object([[characters_voice\scenario\pri\followers_leader_phrase1_1_p]]),
	pri_followers_leader_phrase1_2  = xr_sound_get_safe_sound_object([[characters_voice\scenario\pri\followers_leader_phrase1_2_p]]),
	pri_followers_leader_phrase1_3  = xr_sound_get_safe_sound_object([[characters_voice\scenario\pri\followers_leader_phrase1_3_p]]),
}

pda_news = xr_sound_get_safe_sound_object([[device\pda\pda_news]])
pda_tips = xr_sound_get_safe_sound_object([[device\pda\pda_tip]])
pda_task = xr_sound_get_safe_sound_object([[device\pda\pda_objective]])

--[[ tips_icons = {
	default  = { 0, 658},
	trader   = { 332, 893},
	dolg     = { 0, 658},
	freedom  = { 0, 658},
	ecolog   = { 498, 0},
	arena    = { 332, 141},
	stalker  = { 0, 658},
	krot     = { 332, 47},
	barman   = { 332, 235},
	wolf     = { 332, 940},
	o_soznanie = { 498, 893},
	monolith = { 0, 658},
	saharov  = { 332, 470},
	prizrak  = { 0, 658},
	killer   = { 0, 658}
}
--]]
-- nv180925 implemented MrSeyker's fix for missing sr_tip icons reported by zZz1
tips_icons = {
	kalugin  = { 249, 705},
	vasilyev = { 249, 705},
	voronin  = { 332, 423},
	wolfhound = { 332, 517},
	bes      = { 415, 0},
	blockpost = { 415, 47},
	seriy    = { 415, 141},
	cap      = { 415, 188},
	["max"]  = { 415, 282}, -- "max" is a Lua keyword
	fanatic  = { 415, 658},
	kruglov  = { 498, 0},
	military = { 0, 658},
	default  = { 0, 658},
	trader   = { 332, 893},
	dolg     = { 0, 658},
	freedom  = { 0, 658},
	ecolog   = { 498, 0},
	arena    = { 332, 141},
	stalker  = { 0, 658},
	krot     = { 332, 47},
	barman   = { 332, 235},
	wolf     = { 332, 940},
	o_soznanie = { 415, 893},
	monolith = { 0, 658},
	saharov  = { 332, 470},
	prizrak  = { 415, 705},
	killer   = { 0, 658}
}

tips_by_sender = {
	tips_esc_trader_about_anomalies = "trader",
	escape_fox_quest = "trader",
	gar_dolg_blokpost_warning = "blockpost",
	gar_dolg_monster_rush = "blockpost",
	mil_info_max_mines = "max",
	mil_actor_blockpost = "cap"
}



function send_tip(actor, news_id, timeout, sender, showtime, sender_id)
	if news_id == nil then return false end

	-- printf("try to send tips for [%s]", tostring(sender_id))
	if sender_id ~= nil then
		local sim = alife()
		if sim ~= nil then
			local npc = sim:story_object(sender_id)
			if npc ~= nil then
				if npc.online then
					--в онлайне проверяем на раненность
					if xr_wounded.is_heavy_wounded_by_id(npc.id) then
						-- printf("[PDA]Cannot send tips [%s], sender is heavy wounded", tostring(news_id))
						-- nv 080515 allow val_sos_wounded message
						if sender_id ~= 422 then return false end
					end
				end
				-- в других случаях только на смерть
				-- if npc:alive() == 1 then -- nv170620 npc:alive() returns true or false, neither of which == 1
				if not npc:alive() and sender_id ~= 903 then -- Vasiliev okay to speak from the dead
					-- printf("[PDA]Cannot send tips [%s], sender is dead", tostring(news_id))
					return false
				end
			end
		end
	end

	if timeout == nil then timeout = 0 end
	if showtime == nil then showtime = news_display_time end

	--' Играем дефолтный звук
	pda_tips:play(actor, timeout, sound_object.s2d)
	if news[news_id] ~= nil then
		--' Играем звук забитый
		news[news_id]:play(actor, timeout+1, sound_object.s2d)

		--' Необходимо поставить время показа по длине сцены
		local length = news[news_id]:length()
		if length > showtime then
			showtime = length
		end
	end

	-- if sender == nil then
		-- sender = "default"
	-- end
  -- nv180925 implemented MrSeyker's fix for missing sr_tip icons reported by zZz1
  sender = tips_by_sender[news_id] or sender or "default"
	local x = tips_icons[sender][1]
	local y = tips_icons[sender][2]

	local news_text = "%c[255,160,160,160]"..game.translate_string("st_tip").."\\n".."%c[default]"..game.translate_string(news_id)
	actor:give_game_news(news_text, "ui\\ui_iconsTotal", Frect():set(x,y,83,47), timeout*1000, showtime)
	return true
end
function send_tip_nosound(actor, news_id, timeout, sender)
	if news_id == nil then return end

	timeout = 0

	--' Играем дефолтный звук
	pda_tips:play(actor, timeout, sound_object.s2d)
	-- local news_sound = news[news_id]
	-- return news_sound
	return news[news_id]
end

local action_descr_by_type = {
	new = "general_new_task",
	update = "general_update_task",
	complete = "general_complete_task",
	fail = "general_fail_task"
}
function send_task(actor, type, task, objective)
	if actor == nil then return false end

	--' Берем координаты из текстуры таска
	local task_texture, task_rect = get_texture_info("ui_iconsTotal_"..task:get_id(), "ui_iconsTotal_locations")

	--' Играем дефолтный звук
	pda_task:play(actor, 0, sound_object.s2d)

	local news_text
	local task_title = task:get_title()
	local objective_index = objective:get_idx() + 1
	if task_title == "eliminate_lager" or
		task_title == "defend_lager" or
		task_title == "kill_stalker" or
		task_title == "artefact" or
		task_title == "monster_part" or
		task_title == "find_item"
	then
		news_text = "%c[255,160,160,160]"..game.translate_string(action_descr_by_type[type]) ..
		"  %c[default]"..game.translate_string(task_title) .."\\n%c[120,120,255,160]"..
		game.translate_string(task:get_objective(1):get_description())
	else
		news_text = "%c[255,160,160,160]"..game.translate_string(action_descr_by_type[type]) ..
		"\\n%c[default]"..game.translate_string(task_title)
		if type ~= "complete" and type ~= "fail" then
			-- workaround for incomplete objective list
			if task:get_objectives_cnt() == objective_index then
				objective_index = objective_index - 1
			end
			local obj_desc = task:get_objective(objective_index):get_description()
			if obj_desc ~= nil then
				news_text =  news_text .."\\n%c[120,127,255,255]"..game.translate_string(obj_desc)
			end
		end
	end
	if actor:is_talking() then
		actor:give_talk_message(news_text, task_texture, task_rect,"iconed_answer_item")
		-- actor:give_game_news(news_text, task_texture, task_rect, 1, 10)
	end
	-- else -- give this anyway to put it in diary
	actor:give_game_news(news_text, task_texture, task_rect, 0, news_display_time)
	-- end

	if type == "new" or
		type == "update"
	then
		--' Выдать новое подзадание
		if task:get_objectives_cnt() == objective_index then
			return
		end
		news_text = game.translate_string(task:get_objective(objective_index):get_description())
	else
		news_text = "%c[255,160,160,160]"..game.translate_string(action_descr_by_type[type]) ..
			"\\n%c[default]"..game.translate_string(task_title)
	end

	local hud = get_hud()
	hud:AddCustomStatic("main_task", true)
	hud:GetCustomStatic("main_task"):wnd():SetTextST(news_text)
	hud:GetCustomStatic("main_task").m_endTime = time_global()/1000 + banner_seconds

end
function send_encyclopedy(type, title)
	if type == "Diary" then
		pda_news:play(actor, 0, sound_object.s2d)


		--' Берем координаты из текстуры таска
		local task_texture, task_rect = get_texture_info("ui_iconsTotal_locations", "ui_iconsTotal_locations")
		local news_text = "%c[255,160,160,160]"..game.translate_string("st_found_new_pda").."\\n".."%c[default]"..game.translate_string(title)
		if actor:is_talking() then
			actor:give_talk_message(news_text, task_texture, task_rect,"iconed_answer_item")
		else
			actor:give_game_news(news_text, task_texture, task_rect, 0, news_display_time)
		end
	end
end

local secret_level = {
	esc = "l01_escape",
	gar = "l02_garbage",
	agr = "l03_agroprom",
	agr_u = "l03u_agr_underground",
	val = "l04_darkvalley",
	x18 = "l04u_labx18",
	bar = "l05_bar",
	ros = "l06_rostok",
	mil = "l07_military",
	yan = "l08_yantar",
	x16 = "l08u_brainlab",
	["rad"] = "l10_radar", -- "rad" is a Lua keyword
	pri = "l11_pripyat" --,
}
function send_treasure(name)
	pda_news:play(actor, 0, sound_object.s2d)

	local task_texture, task_rect = get_texture_info("ui_iconsTotal_found_thing")
	local news_text = "%c[255,160,160,160]"..game.translate_string("st_found_new_treasure").."\\n".."%c[120,255,120,160]"..
		game.translate_string(secret_level[string.sub(name,1,string.find(name,"_s",1,true)-1)])..": "..
		game.translate_string(name)

	actor:give_game_news(news_text, task_texture, task_rect, 0, news_display_time)
end

function get_inv_name(section)
	return system_ini():r_string(section,"inv_name")
end

function relocate_item(actor, type, item)
	if actor == nil then return false end
	--' Играем дефолтный звук
	if type == "in" then
		local task_texture, task_rect = get_texture_info("ui_iconsTotal_found_thing")
		local news_text = "%c[255,160,160,160]"..game.translate_string("general_in_item").."\\n".."%c[default]"..game.translate_string(get_inv_name(item))
		if actor:is_talking() then
			actor:give_talk_message(news_text, task_texture, task_rect,"iconed_answer_item")
		else
			actor:give_game_news(news_text, task_texture, task_rect, 0, news_display_time)
		end
	elseif type == "out" then
		local task_texture, task_rect = get_texture_info("ui_iconsTotal_lost_thing")
		local news_text = "%c[255,160,160,160]"..game.translate_string("general_out_item").."\\n".."%c[default]"..game.translate_string(get_inv_name(item))
		if actor:is_talking() then
			actor:give_talk_message(news_text, task_texture, task_rect,"iconed_answer_item")
		else
			actor:give_game_news(news_text, task_texture, task_rect, 0, news_display_time)
		end
	end

end
function relocate_money(actor, type, amount)
	if actor == nil then return false end


	--' Играем дефолтный звук
	if type == "in" then
		local task_texture, task_rect = get_texture_info("ui_iconsTotal_found_money")
		local news_text = "%c[255,160,160,160]"..game.translate_string("general_in_money").."\\n".."%c[default]"..game.translate_string(tostring(amount))

		if actor:is_talking() then
			actor:give_talk_message(news_text, task_texture, task_rect, "iconed_answer_item")
		else
			actor:give_game_news(news_text, task_texture, task_rect, 0, news_display_time)
		end
	elseif type == "out" then
		local task_texture, task_rect = get_texture_info("ui_iconsTotal_lost_money")
		local news_text = "%c[255,160,160,160]"..game.translate_string("general_out_money").."\\n".."%c[default]"..game.translate_string(tostring(amount))

		if actor:is_talking() then
			actor:give_talk_message(news_text, task_texture, task_rect, "iconed_answer_item")
		else
			actor:give_game_news(news_text, task_texture, task_rect, 0, news_display_time)
		end
	end

end
