-- Торговля неписей
local traders = {}

function get_traders()
	return traders
end

function init(npc, cfg)
	traders[npc:id()] = cfg
end

-- Торговля задается разово на первом аплейте
function first_update(npc)
	--log("TRADE INIT[%s]", npc:name())
	local cond, condlist, list
	local ini = ini_file(traders[npc:id()])
	
	-- коэфициенты покупки
	cond = utils.cfg_get_string(ini, "trader", "buy_condition", npc, true, "")
	if cond == nil then
		abort("Incorrect trader settings. Cannot find buy_condition. [%s]->[%s]", npc:name(), cfg)
	end
	condlist = xr_logic.parse_condlist(cond)
	list = xr_logic.pick_section_from_condlist(npc, condlist)
	npc:buy_condition(ini, list)
	
	-- коэфициенты продажи
	cond = utils.cfg_get_string(ini, "trader", "sell_condition", npc, true, "")
	if cond == nil then
		abort("Incorrect trader settings. Cannot find sell_condition. [%s]->[%s]", npc:name(), cfg)
	end	
	condlist = xr_logic.parse_condlist(cond)
	list = xr_logic.pick_section_from_condlist(npc, condlist)
	npc:sell_condition(ini, list)

	local se_npc = server_object(npc:id())
	local game_hours = game.hours()	
	--if se_npc.resupply_time <= game_hours then
		cond = utils.cfg_get_string(ini, "trader", "buy_supplies", npc, false, "")
		if rx_utils.IsTrader(npc) and cond ~= nil then
			condlist = xr_logic.parse_condlist(cond)
			list = xr_logic.pick_section_from_condlist(npc, condlist)
			npc:buy_supplies(ini, list)
		else
			local ammos_need={}
			npc:iterate_inventory_simple(function(item) 
				if item and npc:item_in_slot(item) then
					if isMagazinedWeapon(item) then
						ammos_need[item:section()]=item:has_grenadelauncher()
					end
				else
					if not (
						protected_items.items_keep(item) or 
						protected_items.is_protected_trade_clear(item) or
						protected_items.wpn_keep(item) or 
						protected_items.is_quest_item(item:section()) or
						protected_items.is_unique_wpn_keep(item:section())) 
					then
						--log("trade clear "..item:name())
						release(item)
					end
				end
			end)
			if table.size(ammos_need)>0 then
				for wsection,has_gl in pairs(ammos_need) do
					local wp_ammo=table.random(string.explode(config:r_string(wsection, "ammo_class"), ",", true))
					local gl_ammo
					if has_gl then
						gl_ammo=table.random(string.explode(config:r_string(wsection, "grenade_class"), ",", true))
						--log(npc:name().." snw_gl "..gl_ammo)
						ai:create_ammo(gl_ammo,npc:position(), npc:level_vertex_id(), npc:game_vertex_id(), npc:id(),3)
					end
					local num_in_box = config:r_u32(wp_ammo, "box_size")
					--log(npc:name().." snw_am "..wp_ammo)
					ai:create_ammo(wp_ammo,npc:position(), npc:level_vertex_id(), npc:game_vertex_id(), npc:id(),num_in_box)
				end
				
			end
		end
		--se_npc.resupply_time = game_hours + math.random(12,24)
	--end

	traders[npc:id()] = nil
end
--------------------------------------------------------------------------------------------
local trade_items = {}
local need_del = false

function on_info(info_id)
	if info_id == "ui_trade" then
		local second_talker = nil
		local cls
		for id, npc in pairs(db.creatures) do
			cls = npc:clsid()
			if (cls==clsid.script_stalker or cls==clsid.script_trader)
				and npc:is_talking()	-- get second talker
			then
				need_del = rx_utils.IsTrader(npc)	-- traders only
			--	log("trade: second talker is : "..npc:name().." , need_del:"..tostring(need_del))
				break
			end
		end
	elseif info_id == "ui_trade_hide" then
		if need_del then
			local obj
		--	log("trade: release items' count:"..table.size(trade_items))
			for id in pairs(trade_items) do
				obj = server_object(id)
				if obj and obj.parent_id~=0 then
					release(obj)
				end
			end
			need_del = false
			trade_items = {}
		end
	end
end

function on_trade(item, sell_bye)
	if need_del == false then return end
	if sell_bye == true then
		trade_items[item:id()] = true
	else
		trade_items[item:id()] = nil
	end
end

