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

local weapon_profit_margin = 1.6
local armor_profit_margin = 1.5

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 has_dolg_deposite(actor, npc)
	return actor:money() >= 200
end

function give_dolg_deposite(actor, npc)	
	dialogs.relocate_money(npc, 200, "out")
end

--Бармен в комнате
function give_agroprom_documents(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "quest_case_02", "out")
end

function give_agroprom_reward(first_speaker, second_speaker)
	dialogs.relocate_money(second_speaker, 650, "in")
end

function give_extra_task(actor, npc)
	if barman_darklab_documents_given(actor,npc) or
	   barman_x16_given(actor,npc) or
	   has_alife_info("bar_deactivate_radar_start")
	then
		return true
	end
	return false
end
function dont_give_extra_task(actor, npc)
	return not give_extra_task(actor, npc)
end

function barman_dont_has_room_job(actor,npc)
	return not barman_has_room_job(actor,npc)
end
function barman_has_room_job(actor,npc)	
	if barman_need_darklab_documents(actor,npc)
	then
		return true
	end
	return false
end

function barman_need_complete_job(actor,npc)
	if barman_darklab_documents_given(actor,npc) and 
	   have_darklab_documents(actor,npc)
	then
		return true
	end
	return false
end

function barman_dont_has_job(actor,npc)
	return not barman_has_job(actor,npc)
end

function barman_has_job(actor,npc)
	if has_alife_info("bar_darklab_document_done") and
	   barman_need_kill_veterans(actor,npc) and has_alife_info("bar_rescue_research_start")
	then
		return true
	end
	return false
end



-- ********************************************************
--			БАРМЕН В КОМНАТЕ
-- ********************************************************
-- Документы с дарклаба
function barman_need_darklab_documents(actor,npc)
	if not has_alife_info("bar_darklab_document_start")
	then
		return true
	end
	return false
end

function barman_darklab_documents_given(actor,npc)
	if has_alife_info("bar_darklab_document_start") and
	   not has_alife_info("bar_darklab_document_done") 
	then
		return true
	end
	return false
end

function have_darklab_documents(first_speaker, second_speaker)
  	return first_speaker:object("dar_document4") ~= nil
end

function hasnt_darklab_documents(first_speaker, second_speaker)
    return first_speaker:object("dar_document4") == nil
end

function give_darklab_documents(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "dar_document4", "out")
end
function give_darklab_reward(first_speaker, second_speaker)
	dialogs.relocate_money(second_speaker, 1000, "in")
end

-- Документы с Янтаря
function barman_x16_given(actor,npc)
	if has_alife_info("bar_x16_documents_start") and
	   not has_alife_info("bar_x16_documents_done")
	then
		return true
	end
	return false
end

-- Документы из янтаря
function have_x16_documents(first_speaker, second_speaker)
  	return first_speaker:object("lab_x16_documents") ~= nil
end
function give_x16_documents(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "lab_x16_documents", "out")
end
function give_x16_reward(first_speaker, second_speaker)
	dialogs.relocate_money(second_speaker, 1500, "in")
end

-- Выдача задания на отключение радара
function barman_need_radar(actor,npc)
	if has_alife_info("bar_deactivate_radar_start")	then
		return true
	end
	return false
end

-- ********************************************************
--			БАРМЕН ЗА ПРИЛАВКОМ
-- ********************************************************
--Долг архивные документы
function have_dolg_arhive_document(first_speaker, second_speaker)
  	return first_speaker:object("dolg_arhive_documents") ~= nil
end

function give_dolg_arhive_document(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "dolg_arhive_documents", "out")
end
-- Флешка ученных с ростка
function actor_have_research(first_speaker, second_speaker)
  	return first_speaker:object("bar_ecolog_flash") ~= nil
end
function actor_transfer_research(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "bar_ecolog_flash", "out")
	if has_alife_info("bar_rescue_research_reward") then
        dialogs.relocate_money(second_speaker, 500, "in")
    end
end


-- ********************************************************
--			Лидер Долга
-- ********************************************************
--Свобода РГ-6
function have_rg6(first_speaker, second_speaker)
  	return first_speaker:object("wpn_rg-6") ~= nil
end

function dont_have_rg6(first_speaker, second_speaker)
  	return first_speaker:object("wpn_rg-6") == nil or not has_alife_info("mil_svoboda_rg6_gain")
end


function give_rg6(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "wpn_rg-6", "out")
end

function enter_to_dolg(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "dolg_scientific_outfit", "in")
end

--Фамильное ружье охотника
function have_hunters_toz(first_speaker, second_speaker)
  	return first_speaker:object("hunters_toz") ~= nil
end
function give_hunters_toz(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "hunters_toz", "out")
end
function have_hunter_reward(first_speaker, second_speaker)
	dialogs.relocate_money(second_speaker, 400, "in")
end


-- Оплата информации осведомителю.
function has_money_for_informer_1(actor, npc)
	return actor:money() >= 400
end

function give_money_for_informer_1(actor, npc)
	dialogs.relocate_money(npc, 400, "out")
end

function has_money_for_informer_2(actor, npc)
	return actor:money() >= 200
end

function give_money_for_informer_2(actor, npc)	
	dialogs.relocate_money(npc, 200, "out")
end

function has_money_for_informer_3(actor, npc)
	return actor:money() >= 350
end

function give_money_for_informer_3(actor, npc)	
	dialogs.relocate_money(npc, 350, "out")
end

function barman_give_outfit(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "stalker_outfit", "in")
end


function professor_for_resque(npc, actor)
	dialogs.relocate_item_section(npc, "bar_ecolog_flash", "in")
end

-- ********************************************************
--			ARENA
-- ********************************************************

function arena_give_reward(actor, npc)
	local m = 0
	if has_alife_info("bar_arena_fight_1_reward") then
		m = 100
	elseif has_alife_info("bar_arena_fight_2_reward") then
		m = 200
	elseif has_alife_info("bar_arena_fight_3_reward") then
		m = 300
	elseif has_alife_info("bar_arena_fight_4_reward") then
		m = 400
	elseif has_alife_info("bar_arena_fight_5_reward") then
		m = 600
	elseif has_alife_info("bar_arena_fight_6_reward") then
		m = 800
	elseif has_alife_info("bar_arena_fight_7_reward") then
		m = 1000   
	elseif has_alife_info("bar_arena_fight_8_reward") then
		m = 2000
	end
	dialogs.relocate_money(npc, m, "in")
	db.actor:give_info_portion("bar_arena_reset")	
	xr_zones.purge_arena_items("bar_arena")
end


function actor_has_1000(actor,npc)
	return actor:money() > 100
end
function give_money_1000(actor, npc)	
	dialogs.relocate_money(npc, 100, "out")
end
function actor_has_2000(actor,npc)
	return actor:money() > 200
end
function give_money_2000(actor, npc)	
	dialogs.relocate_money(npc, 200, "out")
end
function actor_has_3000(actor,npc)
	return actor:money() > 300
end
function give_money_3000(actor, npc)	
	dialogs.relocate_money(npc, 300, "out")
end
function actor_has_5000(actor,npc)
	return actor:money() > 500
end
function give_money_5000(actor, npc)	
	dialogs.relocate_money(npc, 500, "out")
end

-- new arena

function arena_has_reward ()
	return has_alife_info("bar_arena_fight_1_reward") or has_alife_info("bar_arena_fight_2_reward") or has_alife_info("bar_arena_fight_3_reward") or has_alife_info("bar_arena_fight_4_reward") or has_alife_info("bar_arena_fight_5_reward") or has_alife_info("bar_arena_fight_6_reward") or has_alife_info("bar_arena_fight_7_reward") or has_alife_info("bar_arena_fight_8_reward")
end

function arena_hasnt_reward ()
	return not( has_alife_info("bar_arena_fight_1_reward") or has_alife_info("bar_arena_fight_2_reward") or has_alife_info("bar_arena_fight_3_reward") or has_alife_info("bar_arena_fight_4_reward") or has_alife_info("bar_arena_fight_5_reward") or has_alife_info("bar_arena_fight_6_reward") or has_alife_info("bar_arena_fight_7_reward") or has_alife_info("bar_arena_fight_8_reward"))
end

function arena_rank_check_true ()
	if has_alife_info("bar_arena_fight_6_done") or has_alife_info("bar_arena_fight_3_done") then
		if has_alife_info("bar_arena_fight_3_done") and (db.actor:character_rank() > 300) and not(has_alife_info("bar_arena_fight_6_done")) then
			return true
		end
	elseif has_alife_info("bar_arena_fight_6_done") and (db.actor:character_rank() > 600) then
		return true 	
	else 
		return false
	end
end

function arena_rank_check_false ()
	return not arena_rank_check_true ()
end

-- Отметка интересных мест на карте
function locate_bar_bar(actor,npc)
	local sim = alife()
	if sim == nil then
		return
	end
	local obj = sim:story_object(570)
	if obj then
		level.map_add_object_spot(obj.id, "crlc_small", "bar_bar_locator")
	end
end
function locate_bar_arena(actor,npc)
	local sim = alife()
	if sim == nil then
		return
	end
	local obj = sim:story_object(571)
	if obj then
		level.map_add_object_spot(obj.id, "crlc_small", "bar_arena_locator")
	end
end
function locate_bar_dolg(actor,npc)
	local sim = alife()
	if sim == nil then
		return
	end
	local obj = sim:story_object(507)
	if obj then
		level.map_add_object_spot(obj.id, "green_location", "bar_voronin_name")
	end
end