-- ui_itm_repair
-- Alundaio
-- ponney68
--[[
Copyright (C) 2012 Alundaio
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License-]]
--]]
-------------------------------------------------------------------
class "load_item" (CUIListBoxItem)
function load_item:__init(height) super(height)
	self.file_name		= "filename"

	self:SetTextColor(GetARGB(255, 170, 170, 170))

	self.fn = self:GetTextItem()
	self.fn:SetFont(GetFontLetterica18Russian())
	self.fn:SetEllipsis(true)
end

function load_item:__finalize()
end

-------------------------------------------------------------------
class "repair_ui" (CUIScriptWnd)

function repair_ui:__init(owner,section) super()
	self.owner = owner
	self.section = section

	self.min_condition = alun_utils.read_from_ini(nil,section,"repair_min_condition","float",0)
	self.max_condition = alun_utils.read_from_ini(nil,section,"repair_max_condition","float",0)
	self.add_condition = alun_utils.read_from_ini(nil,section,"repair_add_condition","float",0)
	self.part_bonus = alun_utils.read_from_ini(nil,section,"repair_part_bonus","float",0)
	self.use_parts = alun_utils.read_from_ini(nil,section,"repair_use_parts","bool",false)
	self.repair_type = alun_utils.read_from_ini(nil,section,"repair_type","string","weapon")

	self.repair_only = alun_utils.parse_list(nil,section,"repair_only",true)
	self.repair_refuse = alun_utils.parse_list(nil,section,"repair_refuse",true)

	self.parts_include = alun_utils.parse_list(nil,section,"repair_parts_include",true)
	self.parts_exclude = alun_utils.parse_list(nil,section,"repair_parts_exclude",true)

	self.use_actor_effects = alun_utils.read_from_ini(nil,section,"repair_use_actor_effects","bool",false)

	self:InitControls()
	self:InitCallBacks()
end

function repair_ui:__finalize()
end

function repair_ui:FillList()
	self.list_box:RemoveAll()

	local function fill_list(actor,obj)
		if (obj) and (self.repair_only and self.repair_only[obj:section()]) or (self.repair_refuse == nil) or (self.repair_refuse[obj:section()] == nil) then
			if (self.repair_type == "weapon" and alun_utils.item_is_fa(obj) or ( (self.repair_type == "outfit") and (alun_utils.item_is_outfit(obj) or obj:clsid() == 69))) then
				local con = obj:condition()
				if (con and con >= self.min_condition and con <= self.max_condition) then
					self:AddItemToList(obj,self.list_box,con)
				end
			end
		end
	end

	db.actor:iterate_inventory(fill_list,db.actor)
end

function repair_ui:FillPartsList()
	self.list_box_parts:RemoveAll()

	if self.list_box:GetSize()==0 then return end

	local item = self.list_box:GetSelectedItem()
	if not (item) then
		return
	end

	local obj = level.object_by_id(item.item_id)
	local function fill_list(actor,itm)
		if (itm and itm:id() ~= item.item_id) then
			if (self.parts_include and self.parts_include[itm:section()]) or (self.parts_exclude == nil and string.find(obj:section(),itm:section())) or (self.parts_exclude and self.parts_exclude[itm:section()] == nil and string.find(obj:section(),itm:section())) then
				self:AddItemToList(itm,self.list_box_parts,alun_utils.read_from_ini(nil,itm:section(),"repair_part_bonus","float",nil) or self.part_bonus)
			end
		end
	end

	db.actor:iterate_inventory(fill_list,db.actor)
end

function repair_ui:InitControls()
	self:SetWndRect			(Frect():set(0,0,1024,768))

	self:SetAutoDelete(true)

	self.xml				= CScriptXmlInit()
	local ctrl
	self.xml:ParseFile			("ui_itm_main.xml")

	ctrl					= CUIWindow()
	self.xml:InitWindow			("itm_repair:file_item:main",0,ctrl)

	self.file_item_main_sz	= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())

	self.xml:InitWindow			("itm_repair:file_item:fn",0,ctrl)
	self.file_item_fn_sz	= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())

	self.xml:InitWindow			("itm_repair:file_item:fd",0,ctrl)
	self.file_item_fd_sz	= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())

	self.form				= self.xml:InitStatic("itm_repair:form",self)
	--self.form:SetWndPos(vector2():set(device().width/2-(self.form:GetWidth()+70), device().height/2 - self.form:GetHeight()))
	self.form:SetWndPos(vector2():set(0, 0))

	-- Background for forms
	--self.xml:InitStatic("itm_repair:form:list_background",self.form)

	if (self.use_parts) then
		--self.xml:InitStatic("itm_repair:form:list_parts_background",self.form)
	end

	-- Item picture
	self.picture			= self.xml:InitStatic("itm_repair:form:icon",self.form)
	self.picture_parts		= self.xml:InitStatic("itm_repair:form:icon_parts",self.form)

	-- Repair tool picture
	self.pic = self.xml:InitStatic("itm_repair:form:icon_tool",self.form)

	local inv_grid_width = alun_utils.read_from_ini(ini,self.section,"inv_grid_width","float",0)
	local inv_grid_height = alun_utils.read_from_ini(ini,self.section,"inv_grid_height","float",0)
	local inv_grid_x = alun_utils.read_from_ini(ini,self.section,"inv_grid_x","float",0)
	local inv_grid_y = alun_utils.read_from_ini(ini,self.section,"inv_grid_y","float",0)

	local x1 = inv_grid_x*50
	local y1 = inv_grid_y*50

	local w = inv_grid_width*50
	local h = inv_grid_height*50

	local x2 = x1 + w
	local y2 = y1 + h

	local w,h = w,h
	if (utils.is_widescreen()) then
	w,h = w/1.5,h/1.2
	else
	w,h = w/1.3,h/1.3
	end
	self.pic:InitTexture("ui\\ui_icon_equipment")
	self.pic:SetTextureRect(Frect():set(x1,y1,x2,y2))
	self.pic:SetWndSize(vector2():set(w,h))

	if not (self.pic.x) then
		local pos = self.pic:GetWndPos()
		local posform = self.form:GetWndPos()
		self.pic.x = pos.x + posform.x
		self.pic.y = pos.y + posform.y
	end

	self.pic:SetWndPos(vector2():set(self.pic.x-w/2, self.pic.y-h/2))

	-- Caption
	self.caption_parts 		= self.xml:InitTextWnd("itm_repair:form:caption_parts",self.form)
	self.caption_repair		= self.xml:InitTextWnd("itm_repair:form:caption_repair",self.form)

	-- List Box
	self.xml:InitFrame			("itm_repair:form:list_frame",self.form)

	self.list_box			= self.xml:InitListBox("itm_repair:form:list",self.form)

	self.list_box:ShowSelectedItem	(true)
	self:Register			(self.list_box, "list_window")

	if (self.use_parts) then
		-- Parts List Box
		self.list_pos = self.list_box:GetWndPos()

		self.list_box_parts			= self.xml:InitListBox("itm_repair:form:list_parts",self.form)
		--self.list_box_parts:SetWndPos(vector2():set(self.list_pos.x+self.list_box:GetWidth()+5, self.list_pos.y))

		local frame = self.xml:InitFrame("itm_repair:form:list_frame_parts",self.form)
		--frame:SetWndPos(vector2():set(self.list_pos.x+self.list_box:GetWidth()+5, self.list_pos.y))

		self.list_box_parts:ShowSelectedItem(true)
		self:Register(self.list_box_parts, "list_window_parts")
	else
		--self.form:SetWndSize(vector2():set(self.list_box:GetWidth()+35, self.form:GetHeight()))
	end
	-- Button Repair
	ctrl					= self.xml:Init3tButton("itm_repair:form:btn_repair",	self.form)
	self:Register			(ctrl, "button_repair")

	-- Button Cancel
	ctrl = self.xml:Init3tButton	("itm_repair:form:btn_cancel",	self.form)
	self:Register			(ctrl, "button_back")
end

function repair_ui:InitCallBacks()
	self:AddCallback("button_repair",		ui_events.BUTTON_CLICKED,         self.OnButton_repair,			self)
	self:AddCallback("button_back",		ui_events.BUTTON_CLICKED,             self.OnButton_back_clicked,	self)

	self:AddCallback("list_window", ui_events.LIST_ITEM_CLICKED, 			  self.OnListItemClicked,		self)
	self:AddCallback("list_window", ui_events.WINDOW_LBUTTON_DB_CLICK,		  self.OnListItemDbClicked,		self)

	self:AddCallback("list_window_parts", ui_events.LIST_ITEM_CLICKED, 		  self.OnPartsListItemClicked,		self)
	self:AddCallback("list_window_parts", ui_events.WINDOW_LBUTTON_DB_CLICK,  self.OnPartsListItemDbClicked,	self)
end

function repair_ui:OnPartsListItemClicked()
	if self.list_box_parts:GetSize()==0 then return end

	local item = self.list_box_parts:GetSelectedItem()
	if not (item) then
		self.picture_parts:SetTextureRect(Frect():set(0,0,0,0))
		self.caption_parts:SetText("")
		return
	end

	local se_item = item.item_id and alife():object(item.item_id)
	if (se_item == nil or not db.actor:object(se_item:section_name())) then
		self.list_box_parts:RemoveItem(item)
		return
	end

	local sec = se_item:section_name()
	local part_bonus = alun_utils.read_from_ini(nil,sec,"repair_part_bonus","float",nil) or self.part_bonus
	local con = math.floor((self.add_condition + part_bonus)*100)
	self.caption_repair:SetText("+"..con.."%")

	local w,h = item.width,item.height
	if (utils.is_widescreen()) then
	w,h = item.width/1.5,item.height/1.2
	else
	w,h = item.width/1.2,item.height/1.2
	end
	self.picture_parts:InitTexture("ui\\ui_icon_equipment")
	self.picture_parts:SetTextureRect(Frect():set(item.x1,item.y1,item.x2,item.y2))
	self.picture_parts:SetWndSize(vector2():set(w,h))

	if not (self.picture_parts.x) then
		local pos = self.picture_parts:GetWndPos()
		self.picture_parts.x = pos.x
		self.picture_parts.y = pos.y
	end

	self.picture_parts:SetWndPos(vector2():set(self.picture_parts.x-w/2, self.picture_parts.y-h/2))

	self.caption_parts:SetText("Утилизация")
end

function repair_ui:OnPartsListItemDbClicked()
	self:OnButton_repair()
end

function repair_ui:OnListItemClicked()
	if self.list_box:GetSize()==0 then return end

	local item = self.list_box:GetSelectedItem()

	if not (item) then
		self.picture:SetTextureRect(Frect():set(0,0,0,0))
		return
	end

	local se_item = alife():object(item.item_id)
	if (se_item == nil or not db.actor:object(se_item:section_name())) then
		self.list_box:RemoveItem(item)
		return
	end

	local w,h = item.width,item.height
	if (utils.is_widescreen()) then
	w,h = item.width/1.5,item.height/1.2
	else
	w,h = item.width/1.2,item.height/1.2
	end
	self.picture:InitTexture("ui\\ui_icon_equipment")
	self.picture:SetTextureRect(Frect():set(item.x1,item.y1,item.x2,item.y2))
	self.picture:SetWndSize(vector2():set(w,h))

	if not (self.picture.x) then
		local pos = self.picture:GetWndPos()
		self.picture.x = pos.x
		self.picture.y = pos.y
	end

	self.picture:SetWndPos(vector2():set(self.picture.x-w/2, self.picture.y-h/2))


	local con = self.add_condition*100
	self.caption_repair:SetText("+"..con.."%")

	if (self.use_parts) then
		self.picture_parts:SetTextureRect(Frect():set(0,0,0,0))
		self.caption_parts:SetText("")
		self:FillPartsList()
	end
end

function repair_ui:OnListItemDbClicked()
	self:OnButton_repair()
end

function repair_ui:OnButton_back_clicked()
	alife():create(self.section,db.actor:position(),db.actor:level_vertex_id(),db.actor:game_vertex_id(),db.actor:id())
	self:HideDialog()
end

function repair_ui:OnKeyboard(dik, keyboard_action)
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
	if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then
		if (dik == DIK_keys.DIK_RETURN) then

		elseif (dik == DIK_keys.DIK_ESCAPE) then
			self:OnButton_back_clicked()
		end
	end
	return true
end

function repair_ui:OnButton_repair()

	local index = self.list_box:GetSelectedIndex()
 	if index == -1 then return end
	local item  = self.list_box:GetItemByIndex(index)

	local obj = item and level.object_by_id(item.item_id)
	if not (obj) then
		return
	end

	local bonus = 0
	if (self.list_box_parts) then

		-- ponney68 repair item multi usage
		-- WEAPON REPAIR ITEMS
		-- sharpening_stones x4
		if self.section =="sharpening_stones" then
		alife():create("sharpening_stones2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="sharpening_stones2" then
		alife():create("sharpening_stones3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="sharpening_stones3" then
		alife():create("sharpening_stones4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end

		-- gun_oil x2
		if self.section =="gun_oil" then
		alife():create("gun_oil2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- gun_oil_ru_d x2
		if self.section =="gun_oil_ru_d" then
		alife():create("gun_oil_ru_d2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- gun_oil_ru x2
		if self.section =="gun_oil_ru" then
		alife():create("gun_oil_ru2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- solvent x2
		if self.section =="solvent" then
		alife():create("solvent2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- steel_wool x2
		if self.section =="steel_wool" then
		alife():create("steel_wool2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- grease x2
		if self.section =="grease" then
		alife():create("grease2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end

		-- cleaning_kit_p x3
		if self.section =="cleaning_kit_p" then
		alife():create("cleaning_kit_p2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="cleaning_kit_p2" then
		alife():create("cleaning_kit_p3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- cleaning_kit_s x3
		if self.section =="cleaning_kit_s" then
		alife():create("cleaning_kit_s2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="cleaning_kit_s2" then
		alife():create("cleaning_kit_s3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- cleaning_kit_r5 x3
		if self.section =="cleaning_kit_r5" then
		alife():create("cleaning_kit_r5_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="cleaning_kit_r5_2" then
		alife():create("cleaning_kit_r5_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- cleaning_kit_r7 x3
		if self.section =="cleaning_kit_r7" then
		alife():create("cleaning_kit_r7_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="cleaning_kit_r7_2" then
		alife():create("cleaning_kit_r7_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- cleaning_kit_u x3
		if self.section =="cleaning_kit_u" then
		alife():create("cleaning_kit_u2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="cleaning_kit_u2" then
		alife():create("cleaning_kit_u3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end

		-- toolkit_p x4
		if self.section =="toolkit_p" then
		alife():create("toolkit_p2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_p2" then
		alife():create("toolkit_p3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_p3" then
		alife():create("toolkit_p4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- toolkit_s x4
		if self.section =="toolkit_s" then
		alife():create("toolkit_s2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_s2" then
		alife():create("toolkit_s3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_s3" then
		alife():create("toolkit_s4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- toolkit_r5 x4
		if self.section =="toolkit_r5" then
		alife():create("toolkit_r5_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_r5_2" then
		alife():create("toolkit_r5_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_r5_3" then
		alife():create("toolkit_r5_4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- toolkit_r7 x4
		if self.section =="toolkit_r7" then
		alife():create("toolkit_r7_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_r7_2" then
		alife():create("toolkit_r7_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_r7_3" then
		alife():create("toolkit_r7_4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end

		-- ARMOR REPAIR ITEMS
		-- sewing_kit_b x4
		if self.section =="sewing_kit_b" then
		alife():create("sewing_kit_b2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="sewing_kit_b2" then
		alife():create("sewing_kit_b3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="sewing_kit_b3" then
		alife():create("sewing_kit_b4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- sewing_kit_a x4
		if self.section =="sewing_kit_a" then
		alife():create("sewing_kit_a2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="sewing_kit_a2" then
		alife():create("sewing_kit_a3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="sewing_kit_a3" then
		alife():create("sewing_kit_a4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- sewing_kit_h x4
		if self.section =="sewing_kit_h" then
		alife():create("sewing_kit_h2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="sewing_kit_h2" then
		alife():create("sewing_kit_h3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="sewing_kit_h3" then
		alife():create("sewing_kit_h4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end

		-- glue_b x2
		if self.section =="glue_b" then
		alife():create("glue_b2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- glue_a x2
		if self.section =="glue_a" then
		alife():create("glue_a2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- glue_e x2
		if self.section =="glue_e" then
		alife():create("glue_e2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end

		-- armor_repair_fa x2
		if self.section =="armor_repair_fa" then
		alife():create("armor_repair_fa2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- helmet_repair x3
		if self.section =="helmet_repair_kit" then
		alife():create("helmet_repair_kit2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="helmet_repair_kit2" then
		alife():create("helmet_repair_kit3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- armor_repair_pro x4
		if self.section =="armor_repair_pro" then
		alife():create("armor_repair_pro2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="armor_repair_pro2" then
		alife():create("armor_repair_pro3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="armor_repair_pro3" then
		alife():create("armor_repair_pro4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- toolkit_u x4
		if self.section =="toolkit_u" then
		alife():create("toolkit_u2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_u2" then
		alife():create("toolkit_u3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		if self.section =="toolkit_u3" then
		alife():create("toolkit_u4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		end
		-- End ponney68

		index = self.list_box_parts:GetSelectedIndex()
		if (index ~= -1) then
			item = self.list_box_parts:GetItemByIndex(index)
			local se_parts = item and item.item_id and alife():object(item.item_id)
			if (se_parts) then
				local sec = se_parts:section_name()
				bonus = alun_utils.read_from_ini(nil,sec,"repair_part_bonus","float",nil) or self.part_bonus

				-- ponny68 repair optional parts multi usage
				-- IMPORTED DRINK
				-- vodka x3
				if(se_parts)and(se_parts:section_name()=="vodka")then
		      		alife():create("vodka_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				if(se_parts)and(se_parts:section_name()=="vodka_2")then
		      		alife():create("vodka_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				-- vodka2 x3
				if(se_parts)and(se_parts:section_name()=="vodka2")then
		      		alife():create("vodka2_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				if(se_parts)and(se_parts:section_name()=="vodka2_2")then
		      		alife():create("vodka2_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				-- vodka_quality x3
				if(se_parts)and(se_parts:section_name()=="vodka_quality")then
		      		alife():create("vodka_quality2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				if(se_parts)and(se_parts:section_name()=="vodka_quality2")then
		      		alife():create("vodka_quality3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				-- mineral_water x3
				if(se_parts)and(se_parts:section_name()=="mineral_water")then
		      		alife():create("mineral_water2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				if(se_parts)and(se_parts:section_name()=="mineral_water2")then
		      		alife():create("mineral_water3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end

				-- ZONE-PRODUCED DRINK
				-- bottle_metal x3
				if(se_parts)and(se_parts:section_name()=="bottle_metal")then
		      		alife():create("bottle_metal2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				if(se_parts)and(se_parts:section_name()=="bottle_metal2")then
		      		alife():create("bottle_metal3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				-- flask x3
				if(se_parts)and(se_parts:section_name()=="flask")then
		      		alife():create("flask2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end
				if(se_parts)and(se_parts:section_name()=="flask2")then
		      		alife():create("flask3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		    		end

				-- WEAPON REPAIR ITEMS
				-- swiss knife
				if(se_parts)and(se_parts:section_name()=="swiss")then
				alife():create("swiss", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- hammer
				if(se_parts)and(se_parts:section_name()=="hammer_inv")then
				alife():create("hammer_inv", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- crowbar
				if(se_parts)and(se_parts:section_name()=="crowbar")then
				alife():create("crowbar", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- gun_oil x2
				if(se_parts)and(se_parts:section_name()=="gun_oil")then
				alife():create("gun_oil2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- gun_oil_ru_d x2
				if(se_parts)and(se_parts:section_name()=="gun_oil_ru_d")then
				alife():create("gun_oil_ru_d2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- gun_oil_ru x2
				if(se_parts)and(se_parts:section_name()=="gun_oil_ru")then
				alife():create("gun_oil_ru2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- solvent x2
				if(se_parts)and(se_parts:section_name()=="solvent")then
				alife():create("solvent2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- steel_wool x2
				if(se_parts)and(se_parts:section_name()=="steel_wool")then
				alife():create("steel_wool2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- grease x2
				if(se_parts)and(se_parts:section_name()=="grease")then
				alife():create("grease2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- cleaning_kit_p x3
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_p")then
				alife():create("cleaning_kit_p2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_p2")then
				alife():create("cleaning_kit_p3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- cleaning_kit_s x3
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_s")then
				alife():create("cleaning_kit_s2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_s2")then
				alife():create("cleaning_kit_s3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- cleaning_kit_r5 x3
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_r5")then
				alife():create("cleaning_kit_r5_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_r5_2")then
				alife():create("cleaning_kit_r5_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- cleaning_kit_r7 x3
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_r7")then
				alife():create("cleaning_kit_r7_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_r7_2")then
				alife():create("cleaning_kit_r7_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- cleaning_kit_u x3
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_u")then
				alife():create("cleaning_kit_u2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())						end
				if(se_parts)and(se_parts:section_name()=="cleaning_kit_u2")then
				alife():create("cleaning_kit_u3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- toolkit_p x4
				if(se_parts)and(se_parts:section_name()=="toolkit_p")then
				alife():create("toolkit_p2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_p2")then
				alife():create("toolkit_p3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_p3")then
				alife():create("toolkit_p4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- toolkit_s x4
				if(se_parts)and(se_parts:section_name()=="toolkit_s")then
				alife():create("toolkit_s2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_s2")then
				alife():create("toolkit_s3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_s3")then
				alife():create("toolkit_s4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- toolkit_r5 x4
				if(se_parts)and(se_parts:section_name()=="toolkit_r5")then
				alife():create("toolkit_r5_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_r5_2")then
				alife():create("toolkit_r5_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_r5_3")then
				alife():create("toolkit_r5_4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- toolkit_r7 x4
				if(se_parts)and(se_parts:section_name()=="toolkit_r7")then
				alife():create("toolkit_r7_2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_r7_2")then
				alife():create("toolkit_r7_3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_r7_3")then
				alife():create("toolkit_r7_4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end

				-- ARMOR REPAIR ITEMS
				-- sewing_kit_b x4
				if(se_parts)and(se_parts:section_name()=="sewing_kit_b")then
				alife():create("sewing_kit_b2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="sewing_kit_b2")then
				alife():create("sewing_kit_b3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="sewing_kit_b3")then
				alife():create("sewing_kit_b4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- sewing_kit_a x4
				if(se_parts)and(se_parts:section_name()=="sewing_kit_a")then
				alife():create("sewing_kit_a2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="sewing_kit_a2")then
				alife():create("sewing_kit_a3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="sewing_kit_a3")then
				alife():create("sewing_kit_a4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end

				-- sewing_kit_h x4
				if(se_parts)and(se_parts:section_name()=="sewing_kit_h")then
				alife():create("sewing_kit_h2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="sewing_kit_h2")then
				alife():create("sewing_kit_h3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="sewing_kit_h3")then
				alife():create("sewing_kit_h4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end

				-- glue_b x2
				if(se_parts)and(se_parts:section_name()=="glue_b")then
				alife():create("glue_b2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- glue_a x2
				if(se_parts)and(se_parts:section_name()=="glue_a")then
				alife():create("glue_a2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- glue_e x2
				if(se_parts)and(se_parts:section_name()=="glue_e")then
				alife():create("glue_e2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end

				-- armor_repair_fa x2
				if(se_parts)and(se_parts:section_name()=="armor_repair_fa")then
				alife():create("armor_repair_fa2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- helmet_repair x3
				if(se_parts)and(se_parts:section_name()=="helmet_repair_kit")then
				alife():create("helmet_repair_kit2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="helmet_repair_kit2")then
				alife():create("helmet_repair_kit3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- armor_repair_pro x4
				if(se_parts)and(se_parts:section_name()=="armor_repair_pro")then
				alife():create("armor_repair_pro2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="armor_repair_pro2")then
				alife():create("armor_repair_pro3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="armor_repair_pro3")then
				alife():create("armor_repair_pro4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- toolkit_u x4
				if(se_parts)and(se_parts:section_name()=="toolkit_u")then
				alife():create("toolkit_u2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_u2")then
				alife():create("toolkit_u3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="toolkit_u3")then
				alife():create("toolkit_u4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end

				-- FUEL
				-- kerosene x5
				if(se_parts)and(se_parts:section_name()=="kerosene")then
				alife():create("kerosene2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="kerosene2")then
				alife():create("kerosene3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="kerosene3")then
				alife():create("kerosene4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="kerosene4")then
				alife():create("kerosene5", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- explo_jerrycan_fuel x8
				if(se_parts)and(se_parts:section_name()=="explo_jerrycan_fuel")then
				alife():create("explo_jerrycan_fuel2", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="explo_jerrycan_fuel2")then
				alife():create("explo_jerrycan_fuel3", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="explo_jerrycan_fuel3")then
				alife():create("explo_jerrycan_fuel4", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="explo_jerrycan_fuel4")then
				alife():create("explo_jerrycan_fuel5", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="explo_jerrycan_fuel5")then
				alife():create("explo_jerrycan_fuel6", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="explo_jerrycan_fuel6")then
				alife():create("explo_jerrycan_fuel7", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				if(se_parts)and(se_parts:section_name()=="explo_jerrycan_fuel7")then
				alife():create("explo_jerrycan_fuel8", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
				end
				-- End ponney68

				alife():release(se_parts,true)
			end
		end
	end

	local con = obj:condition()
	con = con + self.add_condition + bonus
	con = con <= 1 and con or 1
	obj:set_condition(con)

	self:HideDialog()

	if (self.use_actor_effects and actor_effects) then
		actor_effects.use_item(self.section.."_dummy")
		level_weathers.get_weather_manager():forced_weather_change()
	end
end

function repair_ui:AddItemToList(item,listbox,condition)

	local _itm			= load_item(self.file_item_main_sz.y)
	local sec = item and item:section()
	local inv_name 		= item and game.translate_string(alun_utils.read_from_ini(nil,sec,"inv_name","string","error")) or "none"

	_itm:SetWndSize		(self.file_item_main_sz)

	_itm.fn:SetWndPos(vector2():set(0,0))
	_itm.fn:SetWndSize	(self.file_item_fn_sz)
	_itm.fn:SetText		(inv_name)

	condition = math.ceil(condition*100)
	if (item) then
		_itm.fage     = _itm:AddTextField("+"..condition.."%", self.file_item_fd_sz.x)
		_itm.fage:SetFont	(GetFontLetterica16Russian())
		_itm.fage:SetWndPos	(vector2():set(self.file_item_fn_sz.x+4, 0))
		_itm.fage:SetWndSize(self.file_item_fd_sz)

		_itm.item_id = item:id()

		local inv_grid_width = alun_utils.read_from_ini(ini,sec,"inv_grid_width","float",0)
		local inv_grid_height = alun_utils.read_from_ini(ini,sec,"inv_grid_height","float",0)
		local inv_grid_x = alun_utils.read_from_ini(ini,sec,"inv_grid_x","float",0)
		local inv_grid_y = alun_utils.read_from_ini(ini,sec,"inv_grid_y","float",0)

		_itm.x1 = inv_grid_x*50
		_itm.y1 = inv_grid_y*50

		_itm.width = inv_grid_width*50
		_itm.height = inv_grid_height*50

		_itm.x2 = _itm.x1 + _itm.width
		_itm.y2 = _itm.y1 + _itm.height
	end

	--[[
	_itm.picture = self.xml:InitStatic("itm_repair:form:picture",self.form)
	_itm.picture:InitTexture("ui\\ui_icon_equipment")
	_itm.picture:SetTextureRect(Frect():set(_itm.x1,_itm.y1,_itm.x2,_itm.y2))
	_itm.picture:SetWndSize(vector2():set(inv_grid_width*50,inv_grid_height*50))
	--]]

	listbox:AddExistingItem(_itm)
end
