

local trade_manager = {
}





function trade_init(npc, cfg)
--'	printf("TRADE INIT[%s]", npc:name())
--'	if trade_manager[npc:id()] == nil then
		trade_manager[npc:id()] = {}
--'	end

	trade_manager[npc:id()].cfg_ltx = cfg
	trade_manager[npc:id()].config = ini_file(cfg)

	-- коэфициенты покупки
	local str = utils.cfg_get_string(trade_manager[npc:id()].config, "trader", "buy_condition", npc, true, "")
	if str == nil then
		abort("Incorrect trader settings. Cannot find buy_condition. [%s]->[%s]", npc:name(), cfg)
	end
	trade_manager[npc:id()].buy_condition = xr_logic.parse_condlist(npc, "trade_manager", "buy_condition", str)

	-- коэфициенты продажи
	str = utils.cfg_get_string(trade_manager[npc:id()].config, "trader", "sell_condition", npc, true, "")
	if str == nil then
		abort("Incorrect trader settings. Cannot find sell_condition. [%s]->[%s]", npc:name(), cfg)
	end
	trade_manager[npc:id()].sell_condition = xr_logic.parse_condlist(npc, "trade_manager", "sell_condition", str)

	-- список закупки
	str = utils.cfg_get_string(trade_manager[npc:id()].config, "trader", "buy_supplies", npc, false, "")
	if str ~= nil then
		trade_manager[npc:id()].buy_supplies = xr_logic.parse_condlist(npc, "trade_manager", "buy_supplies", str)
	end
end


function update(npc)
	local tt = trade_manager[npc:id()]

	if tt == nil then
		return
	end

	if tt.update_time ~= nil and tt.update_time < time_global() then
		return
	end
	tt.update_time = time_global() + 3600000

	local str = xr_logic.pick_section_from_condlist(db.actor, npc, tt.buy_condition)
	if tt.current_buy_condition ~= str then
		--'printf("TRADE [%s]: buy condition = %s", npc:name(), str)
		npc:buy_condition(tt.config, str)
		tt.current_buy_condition = str
	end

	str = xr_logic.pick_section_from_condlist(db.actor, npc, tt.sell_condition)
	if tt.current_sell_condition ~= str then
		--'printf("TRADE [%s]: sell condition = %s", npc:name(), str)
		npc:sell_condition(tt.config, str)
		tt.current_sell_condition = str
	end

	if tt.buy_supplies == nil then
		return
	end

	str = xr_logic.pick_section_from_condlist(db.actor, npc, tt.buy_supplies)
	if tt.current_buy_supplies ~= str then
		if tt.resuply_time ~= nil and tt.resuply_time < time_global() then
			return
		end
		--'printf("TRADE [%s]: buy_supplies = %s", npc:name(), str)
		npc:buy_supplies(tt.config, str)
		tt.current_buy_supplies = str
		tt.resuply_time = time_global() + 24*3600000
	end
end


function save(obj, packet)
	local tt = trade_manager[obj:id()]
	set_save_marker(packet, "save", false, "trade_manager")
	--' Сохраняем присутствует ли инициализированная торговля в принципе.
	if tt == nil then
		printf("TRADE SAVE [%s]: ignored", obj:name())
		packet:w_bool(false)
		return
	else
		packet:w_bool(true)
	end

	packet:w_stringZ(tt.cfg_ltx)

	printf("TRADE SAVE [%s]: current_buy_condition = %s", obj:name(), tostring(tt.current_buy_condition))
	if tt.current_buy_condition == nil then
		packet:w_stringZ("")
	else
		packet:w_stringZ(tt.current_buy_condition)
	end

	printf("TRADE SAVE [%s]: current_sell_condition = %s", obj:name(), tostring(tt.current_sell_condition))
	if tt.current_sell_condition == nil then
		packet:w_stringZ("")
	else
		packet:w_stringZ(tt.current_sell_condition)
	end

	printf("TRADE SAVE [%s]: current_buy_supplies = %s", obj:name(), tostring(tt.current_buy_supplies))
	if tt.current_buy_supplies == nil then
		packet:w_stringZ("")
	else
		packet:w_stringZ(tt.current_buy_supplies)
	end

	local cur_tm = time_global()

	if tt.update_time == nil then
		packet:w_s32(-1)
	else
	 	packet:w_s32(tt.update_time - cur_tm)
	end

	if tt.resuply_time == nil then
		packet:w_s32(-1)
	else
	 	packet:w_s32(tt.resuply_time - cur_tm)
	end
	set_save_marker(packet, "save", true, "trade_manager")
end

function load(obj, packet)
	set_save_marker(packet, "load", false, "trade_manager")
	local a = packet:r_bool()
	if a == false then
		printf("TRADE LOAD [%s]: ignored", obj:name())
		return
	end

	trade_manager[obj:id()] = {}

	local tt = trade_manager[obj:id()]

	tt.cfg_ltx = packet:r_stringZ()
	printf("TRADE LOAD [%s]: cfg_ltx = %s", obj:name(), tostring(tt.cfg_ltx))

	tt.config = ini_file(tt.cfg_ltx)

	a = packet:r_stringZ()
	printf("TRADE LOAD [%s]: current_buy_condition = %s", obj:name(), tostring(a))
	if a ~= "" then
		tt.current_buy_condition = a
		obj:buy_condition(tt.config, a)
	end

	a = packet:r_stringZ()
	printf("TRADE LOAD [%s]: current_sell_condition = %s", obj:name(), tostring(a))
	if a ~= "" then
		tt.current_sell_condition = a
		obj:sell_condition(tt.config, a)
	end

	a = packet:r_stringZ()
	printf("TRADE LOAD [%s]: current_buy_supplies = %s", obj:name(), tostring(a))
	if a ~= "" then
		tt.current_buy_supplies = a
	end

	local cur_tm = time_global()

	a = packet:r_s32()
	if a ~= -1 then
		tt.update_time = cur_tm + a
	end

	a = packet:r_s32()
	if a ~= -1 then
		tt.resuply_time = cur_tm + a
	end
	set_save_marker(packet, "load", true, "trade_manager")
end