--------------------------------------------------------------------------------------------------------
-- Trader Repair
--------------------------------------------------------------------------------------------------------

local weapon_profit_margin = 1.8
local armor_profit_margin = 1.7

function trader_repair_precond(trader, actor)
	local item_in_slot_1 = db.actor:item_in_slot(1)
	local item_in_slot_2 = db.actor:item_in_slot(2)
	local item_in_slot_6 = db.actor:item_in_slot(6)
	if item_in_slot_1 ~= nil and 1 > item_in_slot_1:condition() + 0.01 then return true
	elseif item_in_slot_2 ~= nil and 1 > item_in_slot_2:condition() + 0.01 then return true
	elseif item_in_slot_6 ~= nil and 1 > item_in_slot_6:condition() + 0.01 then return true
	else return false
	end	 
end

function trader_check_money_s1(trader, actor)
	local item_in_slot = db.actor:item_in_slot(1)
	if item_in_slot ~= nil then 
		local item_repair_cost = math.floor( (1-item_in_slot:condition()) * item_in_slot:cost() * weapon_profit_margin )
		if item_repair_cost > 0 and db.actor:money() >= item_repair_cost and 1 > item_in_slot:condition() then
			return true
		end 
	end
	return false
end

function trader_check_money_s2(trader, actor)
	local item_in_slot = db.actor:item_in_slot(2)
	if item_in_slot ~= nil then 
		local item_repair_cost = math.floor( (1-item_in_slot:condition()) * item_in_slot:cost() * weapon_profit_margin )
		if item_repair_cost > 0 and db.actor:money() >= item_repair_cost and 1 > item_in_slot:condition() then
			return true
		end 
	end
	return false
end

function trader_check_money_s6(trader, actor)
	local item_in_slot = db.actor:item_in_slot(6)
	if item_in_slot ~= nil then 
		local item_repair_cost = math.floor( (1-item_in_slot:condition()) * item_in_slot:cost() * armor_profit_margin )
		if item_repair_cost > 0 and db.actor:money() >= item_repair_cost and 1 > item_in_slot:condition() then
			return true
		end 
	end
	return false
end

function trader_repiar_weapon_s1(trader, actor)
	local item_in_slot = db.actor:item_in_slot(1)
	if item_in_slot ~= nil then
		local item_repair_cost = math.floor( (1-item_in_slot:condition()) * item_in_slot:cost() * weapon_profit_margin )
		item_in_slot:set_condition(1)
		dialogs.relocate_money(actor, item_repair_cost, "out")
	end
end

function trader_repiar_weapon_s2(trader, actor)
	local item_in_slot = db.actor:item_in_slot(2)
	if item_in_slot ~= nil then
		local item_repair_cost = math.floor( (1-item_in_slot:condition()) * item_in_slot:cost() * weapon_profit_margin )
		item_in_slot:set_condition(1)
		dialogs.relocate_money(actor, item_repair_cost, "out")
	end
end

function trader_repiar_armor_s6(trader, actor)
	local item_in_slot = db.actor:item_in_slot(6)
	if item_in_slot ~= nil then
		local item_repair_cost = math.floor( (1-item_in_slot:condition()) * item_in_slot:cost() * armor_profit_margin )
		item_in_slot:set_condition(1)
		dialogs.relocate_money(actor, item_repair_cost, "out")
	end	
end

function repair_costs(first_speaker, second_speaker)
	local task_texture, task_rect = get_texture_info("ui_iconsTotal_lost_money")
	
	local item_name_and_price = ""
	local item_repair_cost = 0
	if db.actor ~= nil then 
		local item_in_slot_1 = db.actor:item_in_slot(1)
		local item_in_slot_2 = db.actor:item_in_slot(2)
		local item_in_slot_6 = db.actor:item_in_slot(6)
	
		if item_in_slot_1 ~= nil then 
			item_repair_cost = math.floor( (1-item_in_slot_1:condition()) * item_in_slot_1:cost() * weapon_profit_margin )
			if item_repair_cost > 0 then
				item_name_and_price = game.translate_string("list_trader_repair_0").." %c[255,238,155,23]"..item_repair_cost.."Ru%c[default]"
				db.actor:give_talk_message(item_name_and_price, task_texture, task_rect, "iconed_trade_info")
			end
		end
		if item_in_slot_2 ~= nil then
			item_repair_cost = math.floor( (1-item_in_slot_2:condition()) * item_in_slot_2:cost() * weapon_profit_margin )
			if item_repair_cost > 0 then
				item_name_and_price = game.translate_string("list_trader_repair_1").." %c[255,238,155,23]"..item_repair_cost.."Ru%c[default]"
				db.actor:give_talk_message(item_name_and_price, task_texture, task_rect, "iconed_trade_info")
			end
		end
		if item_in_slot_6 ~= nil then
			item_repair_cost = math.floor( (1-item_in_slot_6:condition()) * item_in_slot_6:cost() * armor_profit_margin )
			if item_repair_cost > 0 then
				item_name_and_price = game.translate_string("list_trader_repair_2").." %c[255,238,155,23]"..item_repair_cost.."Ru%c[default]"
				db.actor:give_talk_message(item_name_and_price, task_texture, task_rect, "iconed_trade_info")
			end
		end
	end
end


function have_kruglov_flash(first_speaker, second_speaker)
  	return first_speaker:object("kruglov_flash") ~= nil
end
function give_kruglov_flash(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "kruglov_flash", "out")
end
function give_ecolog_outfit(first_speaker, second_speaker)
	if not has_alife_info("yan_ecolog_outfit_given") then
		dialogs.relocate_item_section(second_speaker, "ecolog_outfit", "in")
		db.actor:give_info_portion("yan_ecolog_outfit_given")
	end
end

function yan_professor_talk_outfit (first_speaker, second_speaker)
	if not has_alife_info("bar_x16_documents_start") and has_alife_info("bar_heli_scene_professor_die") then
		return true
	end
	if not has_alife_info("bar_x16_documents_start") then
		if has_alife_info("bar_rescue_research_done") and  has_alife_info("yan_ecolog_outfit_given") then
			return true
		else 
			return false
		end
		return true
	end
	return false
end

function yan_professor_task_x16_precond (first_speaker, second_speaker)
	if has_alife_info("bar_rescue_research_fail") or has_alife_info("bar_heli_scene_professor_die") then
		return true
	end
	return false
end