--[[
Copyright: © Ирбиs , Real Wolf
]]

class "upgrade_slot_menu" (CUIScriptWnd)

slot = nil

local nu = { 	wpn_pkm_tactic 	= true,
					wpn_pecheneg 	= true,
					wpn_korobka 		= true,
					wpn_rpg7 			= true,
					wpn_sks 				= true,
					wpn_ppsh 			= true,
					wpn_mosin 			= true,
					wpn_gauss 			= true,
					wpn_desert_eagle = true,
					wpn_vss_tk1 		= true,
					wpn_pkm				= true,
					wpn_toz34_hun 	= true
				   }

local tbl5 = {	wpn_ak74 = true,
		wpn_bizon_kol	= true,
		wpn_bizon = true,
		wpn_ak74u = true,
		wpn_abakan = true,
		wpn_ak47 = true,
		wpn_akms = true,
		wpn_sr3t = true,
		wpn_val = true,
		wpn_vintorez = true,
		wpn_pm = true,
		wpn_pb = true,
		wpn_fort = true,
		wpn_hpsa = true,
		wpn_beretta = true,
		wpn_usp = true,
		wpn_walther = true,
		wpn_uspm = true,
		wpn_colt1911 = true,
		wpn_sig220 = true
	     } 

local tbl3 = {	wpn_bm16 = true,
		wpn_toz34 = true,
		wpn_spas12 = true,
		wpn_wincheaster1300 = true,
		wpn_sayga = true,
		wpn_p90 = true,
		wpn_mp5 = true
	     }

local tbl2 = {	wpn_svu = true,
		wpn_svd = true,
		wpn_slon = true,
		wpn_sr25_mk11_mod0_sk1 = true,
		wpn_rpk74 = true
	     } 

local tbl6 = {	wpn_groza = true,
		wpn_sig550 = true,
		wpn_lr300 = true,
		wpn_l85 = true,
		wpn_g36 = true,
		wpn_fn2000 = true,
		wpn_famas = true,
		wpn_aug_a3 = true
	     }


function upgrade_slot_menu:__init(owner) super()
   	self.owner = owner
   	self:InitControls()
  	self:InitCallBacks()
	self.mbox_mode = 0	
	self.repair_cost = 0
	self.repaired = false
	self.upg = "nil"
	self.cost = 0	
end

function upgrade_slot_menu:__finalize() end

function upgrade_slot_menu:InitControls()
    	self:Init(266,138,492,491)			  
    	local xml = CScriptXmlInit()
		
    	xml:ParseFile("ui_upgrade_slot.xml")

	self.message_box = CUIMessageBoxEx()
	self:Register(self.message_box, "msg_box")

     	xml:InitStatic("background", self)
     	xml:InitStatic("slot", self)

	self:Register(xml:Init3tButton("btn_quit", self),"btn_quit")
	self:Register(xml:Init3tButton("btn_repair", self),"btn_repair")

	if nu[db.actor:item_in_slot(slot):section()] or db.actor:item_in_slot(slot):section() == "wpn_rg-6" then
		local w = system_ini():r_float(db.actor:item_in_slot(slot):section(),"inv_grid_width")*50
		local h = system_ini():r_float(db.actor:item_in_slot(slot):section(),"inv_grid_height")*50

		wnd_wi = CUIStatic()
		wnd_wi:SetAutoDelete(true)
		self:AttachChild(wnd_wi)
		wnd_wi:SetStretchTexture(true)	
		wnd_wi:SetWndRect(48,48,w,h)
		wnd_wi:InitTexture(db.actor:item_in_slot(slot):section())
		return
	end

	slot_sect = GetRealSect(db.actor:item_in_slot(slot):section())
	local upg = tostring(GetUpgrade(db.actor:item_in_slot(slot):section()))
	local tip = GetType(db.actor:item_in_slot(slot):section())

	db.actor:hide_weapon()

	local w = system_ini():r_float(slot_sect,"inv_grid_width")*50
	local h = system_ini():r_float(slot_sect,"inv_grid_height")*50

	wnd_wi = CUIStatic()
	wnd_wi:SetAutoDelete(true)
	self:AttachChild(wnd_wi)
	wnd_wi:SetStretchTexture(true)	
	wnd_wi:SetWndRect(48,48,w,h)
	wnd_wi:InitTexture(slot_sect)

	if upg ~= "nil" then
		wnd_m = CUIStatic()
		wnd_m:SetAutoDelete(true)
		self:AttachChild(wnd_m)
		wnd_m:SetStretchTexture(true)	
		wnd_m:SetWndRect(300,110,34,35)
		wnd_m:InitTexture("wpn_m")
	end

	if not tbl2[slot_sect] then
		self:Register(xml:Init3tButton("a", self),"a")
	end

	if tbl5[slot_sect] then
		self:Register(xml:Init3tButton("ba", self),"ba")
		self:Register(xml:Init3tButton("bb", self),"bb")
		self:Register(xml:Init3tButton("c", self),"cb")
	elseif tbl6[slot_sect] then
		self:Register(xml:Init3tButton("ba", self),"ba")
		self:Register(xml:Init3tButton("bb", self),"bb")
		self:Register(xml:Init3tButton("ca", self),"ca")
		self:Register(xml:Init3tButton("cb", self),"cb")
	elseif tbl2[slot_sect] then
		self:Register(xml:Init3tButton("e", self),"a")
		self:Register(xml:Init3tButton("b", self),"bb")
	elseif tbl3[slot_sect] then
		self:Register(xml:Init3tButton("ba", self),"ba")
		self:Register(xml:Init3tButton("bb", self),"bb")
	elseif slot_sect == "wpn_desert_eagle" then
		self:Register(xml:Init3tButton("ba", self),"ba")
		self:Register(xml:Init3tButton("bb", self),"bb")
		self:Register(xml:Init3tButton("d", self),"ca")
	end

	if upg == "a" then
		wnd_a = CUIStatic()
		wnd_a:SetAutoDelete(true)
		self:AttachChild(wnd_a)
		wnd_a:SetStretchTexture(true)	
		wnd_a:SetWndRect(34,183,90,44)
		wnd_a:InitTexture("seva_aa_i")
	elseif upg == "ba" and not tbl2[slot_sect]  then
		wnd_a = CUIStatic()
		wnd_a:SetAutoDelete(true)
		self:AttachChild(wnd_a)
		wnd_a:SetStretchTexture(true)	
		wnd_a:SetWndRect(34,183,90,44)
		wnd_a:InitTexture("seva_aa_i")

		wnd_ba = CUIStatic()
		wnd_ba:SetAutoDelete(true)
		self:AttachChild(wnd_ba)
		wnd_ba:SetStretchTexture(true)	
		wnd_ba:SetWndRect(145,161,90,44)
		wnd_ba:InitTexture("seva_aa_i")

		wnd_bar = CUIStatic()
		wnd_bar:SetAutoDelete(true)
		self:AttachChild(wnd_bar)
		wnd_bar:SetStretchTexture(true)	
		wnd_bar:SetWndRect(145,209,90,44)
		wnd_bar:InitTexture("base_r")
	elseif upg == "bb" then
		wnd_a = CUIStatic()
		wnd_a:SetAutoDelete(true)
		self:AttachChild(wnd_a)
		wnd_a:SetStretchTexture(true)	
		wnd_a:SetWndRect(34,183,90,44)
		wnd_a:InitTexture("seva_aa_i")

		wnd_bb = CUIStatic()
		wnd_bb:SetAutoDelete(true)
		self:AttachChild(wnd_bb)
		wnd_bb:SetStretchTexture(true)
		if tbl2[slot_sect] then	
			wnd_bb:SetWndRect(145,183,90,44)
		else
			wnd_bb:SetWndRect(145,209,90,44)

			wnd_bbr = CUIStatic()
			wnd_bbr:SetAutoDelete(true)
			self:AttachChild(wnd_bbr)
			wnd_bbr:SetStretchTexture(true)	
			wnd_bbr:SetWndRect(145,161,90,44)
			wnd_bbr:InitTexture("base_r")
		end
		wnd_bb:InitTexture("seva_aa_i")
	elseif tbl5[slot_sect] or tbl6[slot_sect] then
	if upg == "ca" or upg == "cb" then
		if string.find(db.actor:item_in_slot(slot):section(),"_ba_") then
			wnd_ba = CUIStatic()
			wnd_ba:SetAutoDelete(true)
			self:AttachChild(wnd_ba)
			wnd_ba:SetStretchTexture(true)	
			wnd_ba:SetWndRect(145,161,90,44)
			wnd_ba:InitTexture("seva_aa_i")

			wnd_bar = CUIStatic()
			wnd_bar:SetAutoDelete(true)
			self:AttachChild(wnd_bar)
			wnd_bar:SetStretchTexture(true)	
			wnd_bar:SetWndRect(145,209,90,44)
			wnd_bar:InitTexture("base_r")
		elseif string.find(db.actor:item_in_slot(slot):section(),"_bb_") then
			wnd_bb = CUIStatic()
			wnd_bb:SetAutoDelete(true)
			self:AttachChild(wnd_bb)
			wnd_bb:SetStretchTexture(true)	
			wnd_bb:SetWndRect(145,209,90,44)
			wnd_bb:InitTexture("seva_aa_i")

			wnd_bbr = CUIStatic()
			wnd_bbr:SetAutoDelete(true)
			self:AttachChild(wnd_bbr)
			wnd_bbr:SetStretchTexture(true)	
			wnd_bbr:SetWndRect(145,161,90,44)
			wnd_bbr:InitTexture("base_r")
		end
		if upg == "ca" then
			wnd_a = CUIStatic()
			wnd_a:SetAutoDelete(true)
			self:AttachChild(wnd_a)
			wnd_a:SetStretchTexture(true)	
			wnd_a:SetWndRect(34,183,90,44)
			wnd_a:InitTexture("seva_aa_i")

			wnd_ca = CUIStatic()
			wnd_ca:SetAutoDelete(true)
			self:AttachChild(wnd_ca)
			wnd_ca:SetStretchTexture(true)
			if slot_sect == "wpn_desert_eagle" then	
				wnd_ca:SetWndRect(257,183,90,44)
			else
				wnd_ca:SetWndRect(257,161,90,44)

				wnd_car = CUIStatic()
				wnd_car:SetAutoDelete(true)
				self:AttachChild(wnd_car)
				wnd_car:SetStretchTexture(true)	
				wnd_car:SetWndRect(257,209,90,44)
				wnd_car:InitTexture("base_r")
			end
			wnd_ca:InitTexture("seva_aa_i")
		elseif upg == "cb" then
			wnd_a = CUIStatic()
			wnd_a:SetAutoDelete(true)
			self:AttachChild(wnd_a)
			wnd_a:SetStretchTexture(true)	
			wnd_a:SetWndRect(34,183,90,44)
			wnd_a:InitTexture("seva_aa_i")

			wnd_cb = CUIStatic()
			wnd_cb:SetAutoDelete(true)
			self:AttachChild(wnd_cb)
			wnd_cb:SetStretchTexture(true)
			if tbl5[slot_sect] then	
				wnd_cb:SetWndRect(257,183,90,44)
			else	
				wnd_cb:SetWndRect(257,209,90,44)

				wnd_cbr = CUIStatic()
				wnd_cbr:SetAutoDelete(true)
				self:AttachChild(wnd_cbr)
				wnd_cbr:SetStretchTexture(true)	
				wnd_cbr:SetWndRect(257,161,90,44)
				wnd_cbr:InitTexture("base_r")
			end
			wnd_cb:InitTexture("seva_aa_i")
		end
		end
	end		
end


function upgrade_slot_menu:InitCallBacks()
	self:AddCallback("btn_quit", ui_events.BUTTON_CLICKED, self.on_quit, self)
	self:AddCallback("btn_repair", ui_events.BUTTON_CLICKED, self.OnButton_repair, self)
	self:AddCallback("msg_box", ui_events.MESSAGE_BOX_YES_CLICKED, self.OnMsgYes, self)
	self:AddCallback("msg_box", ui_events.MESSAGE_BOX_NO_CLICKED, self.OnMsgNo, self)

	if nu[db.actor:item_in_slot(slot):section()] then return end

	self:AddCallback("a", ui_events.BUTTON_CLICKED, self.OnButton_a, self)
	if not tbl2[slot_sect] then
		self:AddCallback("ba", ui_events.BUTTON_CLICKED, self.OnButton_ba, self)
	end
	self:AddCallback("bb", ui_events.BUTTON_CLICKED, self.OnButton_bb, self)
	if tbl5[slot_sect] then
		self:AddCallback("cb", ui_events.BUTTON_CLICKED, self.OnButton_cb, self)
	elseif slot_sect == "wpn_desert_eagle" then
		self:AddCallback("ca", ui_events.BUTTON_CLICKED, self.OnButton_ca, self)
	elseif tbl6[slot_sect] then
		self:AddCallback("ca", ui_events.BUTTON_CLICKED, self.OnButton_ca, self)
		self:AddCallback("cb", ui_events.BUTTON_CLICKED, self.OnButton_cb, self)
	end
end

function upgrade_slot_menu:OnKeyboard(dik, keyboard_action)
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
	if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
		if dik == DIK_keys.DIK_ESCAPE then
			self:on_quit()
		end
	end
	return true
end

function upgrade_slot_menu:on_quit()
	local console = get_console()
	self.three_level = false
	self:GetHolder():start_stop_menu (self.owner, true)
	self:GetHolder():start_stop_menu (self,true)
	self.owner:Show	(true)
	db.actor:restore_weapon()
	if level.present() and (db.actor ~= nil) and db.actor:alive() then
		console:execute("main_menu off")
	end
end

function upgrade_slot_menu:OnMsgYes()
	local wpn = db.actor:item_in_slot(slot)
	local realsect = GetRealSect(wpn:section())
	local tip = GetType(wpn:section())

	if self.repaired == true then
		if db.actor:money() >= self.repair_cost then
			wpn:set_condition(1)
			db.actor:give_money(-self.repair_cost)
			if slot == 1 then
				lwc.PlaySound([[inv_sounds\tiski_pistol_3]])
			else
				lwc.PlaySound([[inv_sounds\tiski_avto_3]])
			end
			news_manager.send_tip(db.actor, "Оружие отремонтировано", nil, nil, 5000)
			self:ReleaseAddons(wpn,1)
		else
			news_manager.send_tip(db.actor, "Недостаточно денег", nil, nil, 5000)			
		end
		self.message_box:DetachChild(wnd_i)
		self.repaired = false
		return
	end
		
	if self.upg and self.upg ~= "nil" then
		if db.actor:money() >= self.cost then
			wnd_co:Show(false)
			db.actor:give_money(-self.cost)
			self:ReleaseAddons(wpn,0)
			news_manager.send_tip(db.actor, "Модификация установлена", nil, nil, 5000)
			if slot == 1 then
				lwc.PlaySound([[inv_sounds\tiski_pistol_2]])
			else
				lwc.PlaySound([[inv_sounds\tiski_avto_2]])
			end
			if not wnd_m then
				wnd_m = CUIStatic()
				wnd_m:SetAutoDelete(true)
				self:AttachChild(wnd_m)
				wnd_m:SetStretchTexture(true)	
				wnd_m:SetWndRect(300,110,34,35)
				wnd_m:InitTexture("wpn_m")
			end
			if self.upg == "a" then
				wnd_a = CUIStatic()
				wnd_a:SetAutoDelete(true)
				self:AttachChild(wnd_a)
				wnd_a:SetStretchTexture(true)	
				wnd_a:SetWndRect(34,183,90,44)
				wnd_a:InitTexture("seva_aa_i")
			elseif self.upg == "ba" then
				wnd_ba = CUIStatic()
				wnd_ba:SetAutoDelete(true)
				self:AttachChild(wnd_ba)
				wnd_ba:SetStretchTexture(true)	
				wnd_ba:SetWndRect(145,161,90,44)
				wnd_ba:InitTexture("seva_aa_i")

				wnd_bar = CUIStatic()
				wnd_bar:SetAutoDelete(true)
				self:AttachChild(wnd_bar)
				wnd_bar:SetStretchTexture(true)	
				wnd_bar:SetWndRect(145,209,90,44)
				wnd_bar:InitTexture("base_r")
			elseif self.upg == "bb" then
				wnd_bb = CUIStatic()
				wnd_bb:SetAutoDelete(true)
				self:AttachChild(wnd_bb)
				wnd_bb:SetStretchTexture(true)
				if tbl2[slot_sect] then	
					wnd_bb:SetWndRect(145,183,90,44)
				else	
					wnd_bb:SetWndRect(145,209,90,44)

					wnd_bbr = CUIStatic()
					wnd_bbr:SetAutoDelete(true)
					self:AttachChild(wnd_bbr)
					wnd_bbr:SetStretchTexture(true)	
					wnd_bbr:SetWndRect(145,161,90,44)
					wnd_bbr:InitTexture("base_r")
				end
				wnd_bb:InitTexture("seva_aa_i")
			elseif self.upg == "ca" then
				self.message_box:DetachChild(wnd_t)
				wnd_ca = CUIStatic()
				wnd_ca:SetAutoDelete(true)
				self:AttachChild(wnd_ca)
				wnd_ca:SetStretchTexture(true)
				if slot_sect == "wpn_desert_eagle" then	
					wnd_ca:SetWndRect(257,183,90,44)
				else
					wnd_ca:SetWndRect(257,161,90,44)

					wnd_car = CUIStatic()
					wnd_car:SetAutoDelete(true)
					self:AttachChild(wnd_car)
					wnd_car:SetStretchTexture(true)	
					wnd_car:SetWndRect(257,209,90,44)
					wnd_car:InitTexture("base_r")
				end
				wnd_ca:InitTexture("seva_aa_i")
			elseif self.upg == "cb" then
				wnd_cb = CUIStatic()
				wnd_cb:SetAutoDelete(true)
				self:AttachChild(wnd_cb)
				wnd_cb:SetStretchTexture(true)
				if tbl5[slot_sect] then	
					wnd_cb:SetWndRect(257,183,90,44)
				else
					wnd_cb:SetWndRect(257,209,90,44)

					wnd_cbr = CUIStatic()
					wnd_cbr:SetAutoDelete(true)
					self:AttachChild(wnd_cbr)
					wnd_cbr:SetStretchTexture(true)	
					wnd_cbr:SetWndRect(257,161,90,44)
					wnd_cbr:InitTexture("base_r")
				end
				wnd_cb:InitTexture("seva_aa_i")
			end
		else
			news_manager.send_tip(db.actor, "Недостаточно денег", nil, nil, 5000)
		end						
	end
end

function upgrade_slot_menu:OnMsgNo()
	self.mbox_mode = 0

	local drop, err_msg = pcall(protected_detach, self.message_box, wnd_i)
	if (not drop) and (err_msg) then
		qqq("ERROR: lwc_ui_slot_upgrade, fail to detach static, reason: "..tostring(err_msg))
	end
	wnd_co:Show(false)
end

function protected_detach(wnd,child)
	wnd:DetachChild(child)
end

function upgrade_slot_menu:OnButton_a()
	local wpn = db.actor:item_in_slot(slot)
	local uPg = tostring(GetUpgrade(wpn:section()))	
	if uPg == "nil" then
		self.upg = "a"
		self.cost = wpn:cost() + wpn:cost()/2
		if tbl2[slot_sect] then
			self:msg_init("message_wpn_ba")
		else
			self:msg_init("message_wpn_a")
		end
		wnd_co:SetText(tostring(self.cost))		 
	else
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	end
end

function upgrade_slot_menu:OnButton_ba()
	local wpn = db.actor:item_in_slot(slot)
	local uPg = tostring(GetUpgrade(wpn:section()))	
	if uPg == "a" then
		self.upg = "ba"
		self.cost = wpn:cost() + wpn:cost()/2 + 1000
		self:msg_init("message_wpn_ba")		 
	elseif uPg == "ba" or string.find(wpn:section(),"_ba_") then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif uPg == "bb" or string.find(wpn:section(),"_bb_") then
		news_manager.send_tip(db.actor, "Уже установлена другая модификация", nil, nil, 5000)
	end
end

function upgrade_slot_menu:OnButton_bb()
	local wpn = db.actor:item_in_slot(slot)
	local uPg = tostring(GetUpgrade(wpn:section()))	
	if uPg == "a" then
		self.upg = "bb"
		self.cost = wpn:cost() + wpn:cost()/2 + 1000
		self:msg_init("message_wpn_bb")		 
	elseif uPg == "bb" or string.find(wpn:section(),"_bb_") then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif uPg == "ba" or string.find(wpn:section(),"_ba_") then
		news_manager.send_tip(db.actor, "Уже установлена другая модификация", nil, nil, 5000)
	end
end

function upgrade_slot_menu:OnButton_ca()
	local wpn = db.actor:item_in_slot(slot)
	local uPg = tostring(GetUpgrade(wpn:section()))		
	if uPg == "ba" or uPg == "bb" then
		self.upg = "ca"
		self.cost = wpn:cost() + wpn:cost()/2 + 2000
		self:msg_init("message_wpn_ca")	 
	elseif uPg == "ca" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif uPg == "cb" then
		news_manager.send_tip(db.actor, "Уже установлена другая модификация", nil, nil, 5000)
	end
end

function upgrade_slot_menu:OnButton_cb()
	local wpn = db.actor:item_in_slot(slot)
	local uPg = tostring(GetUpgrade(wpn:section()))	
	if uPg == "ba" or uPg == "bb" then
		self.upg = "cb"
		self.cost = wpn:cost() + wpn:cost()/2 + 2000
		self:msg_init("message_wpn_cb")			 
	elseif uPg == "cb" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif uPg == "ca" then
		news_manager.send_tip(db.actor, "Уже установлена другая модификация", nil, nil, 5000)
	end
end

function upgrade_slot_menu:OnButton_repair()
	local wpn = db.actor:item_in_slot(slot)
	if wpn:condition() > 0.96 then
		news_manager.send_tip(db.actor, "Оружие не нуждается в ремонте", nil, nil, 5000)
		return
	elseif wpn:condition() < 0.2 then
		news_manager.send_tip(db.actor, "Оружие не подлежит ремонту", nil, nil, 5000)
		return
	end

	self.repaired = true
	self:msg_init("message_repair_1")
	
	self.repair_cost = math.floor( (1-wpn:condition()) * wpn:cost())

	wnd_i = CUIStatic()
	wnd_i:SetAutoDelete(true)
	self.message_box:AttachChild(wnd_i)
	wnd_i:SetWndRect(330, 450, 383,120)          
	wnd_i:SetFont(GetFontLetterica18Russian())
	wnd_i:SetTextColor(255,160,160,160)
	wnd_i:SetText("Стоимость ремонта: "..self.repair_cost.." рублей. Произвести ремонт?")	
end

function upgrade_slot_menu:msg_init(text)
	self.mbox_mode = 1
	self.message_box:Init(text)
	self:GetHolder():start_stop_menu(self.message_box, true)
		wnd_co = CUIStatic()
		wnd_co:SetAutoDelete(true)
		self.message_box:AttachChild(wnd_co)
		wnd_co:SetWndRect(385, 425, 20, 20)          
		wnd_co:SetFont(GetFontLetterica18Russian())
		wnd_co:SetTextColor(255,255,255,255)
	wnd_co:SetText(tostring(self.cost))
end

function GetUpgrade(section)
	local param = system_ini():r_string(section,"upg")
	return param	
end

function GetLastUpgrade(section)
	local param = system_ini():r_string(section,"upg_last")
	return param	
end

function GetRealSect(sect)
	local par = GetUpgrade(sect)
	local result
	if par == "a" then
		local result = string.sub(sect,1,(string.len(sect)-2))
		return result
	elseif par == "ba" or par == "bb" then
		local result = string.sub(sect,1,(string.len(sect)-3))
		return result
	elseif par == "ca" or par == "cb" then
		local result = string.sub(sect,1,(string.len(sect)-6))
		return result
	else
		return sect 
	end
end

function GetType(sect)
	local param = system_ini():r_string(sect,"class")
	local sect1 = GetRealSect(sect)
	if class == "WP_BM16" or class == "WP_SHOTG" or sect1 == "wpn_sayga" then
		return "shotg"
	elseif class == "WP_PM" or class == "WP_HPSA" or class == "WP_WALTH" or sect1 == "wpn_oc33" or sect1 == "wpn_usp" then
		return "pistol"
	else
		return "auto"
	end
end

function upgrade_slot_menu:ReleaseAddons(obj,mode)
	local sobj = alife():object(obj:id())
	local wpn = obj:section()
	if sobj ~= nil then
		local t = amk.get_weapon_data(sobj)
		local h_scope = read_if_exist(system_ini(),wpn,"scope_status",nil)
		local h_sil = read_if_exist(system_ini(),wpn,"silencer_status",nil)
		local h_gren = read_if_exist(system_ini(),wpn,"grenade_launcher_status",nil)
		if h_scope == 2 then
			h_scope = read_string_if_exist(system_ini(),wpn,"scope_name",nil)
			if h_scope ~= nil and (t.addon_flags == 1 or t.addon_flags == 3 or t.addon_flags == 5 or t.addon_flags == 7) then
				h_scope = amk.spawn_item_in_inv(h_scope)
			end
		end
		if h_sil == 2 then
			h_sil = read_string_if_exist(system_ini(),wpn,"silencer_name",nil)
			if h_sil ~= nil and t.addon_flags >= 4 then
				h_sil = amk.spawn_item_in_inv(h_sil)
			end
		end
		if h_gren == 2 then
			h_gren = read_string_if_exist(system_ini(),wpn,"grenade_launcher_name",nil)
			if h_gren ~= nil and (t.addon_flags == 2 or t.addon_flags == 3 or t.addon_flags == 6 or t.addon_flags == 7) then
				h_gren = amk.spawn_item_in_inv(h_gren)
			end
		end
		local ammo = obj:get_ammo_in_magazine()
		local ammoT = t.ammo_type
		local section_ammo = weapons_ammo_sections(wpn,ammoT)
		if section_ammo ~= nil and ammo >= 1 then
			amk.spawn_ammo_in_inv(section_ammo, ammo, db.actor)
		end
		alife():release(sobj)
		if mode == 1 then
			amk.spawn_item_in_inv(wpn)
		else
			local realsect = GetRealSect(wpn)
			local upg = GetUpgrade(wpn)
			if self.upg == "ca" or self.upg == "cb" then 		
				amk.spawn_item_in_inv(wpn.."_"..self.upg)
			else
				amk.spawn_item_in_inv(realsect.."_"..self.upg)
			end
		end
		amk.start_timer("upg_set_cond",1)
	end
end

function read_if_exist(ltx, section, value, default)
	if ltx:line_exist(section,value) then
		return ltx:r_float(section,value)
	else
		return default
	end
end

function read_string_if_exist(ltx, section, value, default)
	if ltx:line_exist(section,value) then
		return ltx:r_string(section,value)
	else
		return default
	end
end

--' Раньше было через таблицу, :( Не очень хотелось прописывать кучу стволов, сделал без таблиц. Real Wolf
function weapons_ammo_sections(section, ammoT)
	local ammo_table	
	local ammo = utils.cfg_get_string(system_ini(), section, "ammo_class", false, false, false, "")
	if ammo ~= "" then
		ammo_table = xrs_utils.str_explode(",",ammo)
	end										 							
	return ammo_table[ammoT+1]
end

function upgrade_slot_menu:on_quit()
	db.actor:restore_weapon()
	self:GetHolder():start_stop_menu (self,true)
end