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

local outf = {	proseva = true,
		dragon_exo = true,
		dolg_military = true,
		dolg_scientific_outfit = true,
		duty_exo = true,
		specops_free = true,
		freedom_exo = true,
		freedom_scientific = true,
		mercenary_exo = true,
		mercenary_scientific = true,
		military_outfit = true,
		military_gas = true,
		scientific_outfit = true,
		exo_outfit = true
	      }

local wnd_aa,wnd_ab,wnd_ac,wnd_ba,wnd_bb,wnd_bc,wnd_ca,wnd_cb,wnd_cc,wnd_i,wnd_r1,wnd_r2

class "upgrade_menu" (CUIScriptWnd)

function upgrade_menu:__init(owner) super()
   	self.owner = owner
   	self:InitControls()
  	self:InitCallBacks()
	self.mbox_mode = 0
	self.next_upg = "nil"
	self.cost = 0
	self.repair_cost = 0
	self.upgraded = false	
end

function upgrade_menu:__finalize() end

function upgrade_menu:InitControls()
    	self:Init(266,138,492,491)			  
    	local xml = CScriptXmlInit()

	file = GetRealSect(1)
	local realsect = GetRealSect(0)
	local upg = tostring(get_upgrade(db.actor:item_in_slot(6):section()))	
		
    	xml:ParseFile(file)

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

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

	self:Register(xml:Init3tButton("aa", self),"aa")
	if upg == "aa" or upg == "ab" or upg == "ac" then
       		xml:InitStatic("aa_i", self)
	end

	self:Register(xml:Init3tButton("ab", self),"ab")
	if (upg == "ab") or (upg == "ac") then
       		xml:InitStatic("ab_i", self)
	end
		
	self:Register(xml:Init3tButton("ac", self),"ac")
	if upg == "ac" then
       		xml:InitStatic("ac_i", self)
	end

	self:Register(xml:Init3tButton("ba", self),"ba")
	if upg == "ba" or upg == "bb" or upg == "bc" then
       		xml:InitStatic("ba_i", self)  
	end

	self:Register(xml:Init3tButton("bb", self),"bb")
	if upg == "bb" or upg == "bc" then
       		xml:InitStatic("bb_i", self)
	end

	self:Register(xml:Init3tButton("bc", self),"bc")
	if upg == "bc" then
		xml:InitStatic("bc_i", self)
	end

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

	if outf[realsect] then
		self.three_level = true
		self:Register(xml:Init3tButton("ca", self),"ca")
		if upg == "ca" or upg == "cb" or upg == "cc" then
       			xml:InitStatic("ca_i", self)
        		xml:InitStatic("ca_r5", self)
       			xml:InitStatic("ca_r6", self)
		end

		self:Register(xml:Init3tButton("cb", self),"cb")
		if upg == "cb" or upg == "cc" then
       			xml:InitStatic("cb_i", self)
		end

		self:Register(xml:Init3tButton("cc", self),"cc")
		if upg == "cc" then
			xml:InitStatic("cc_i", self)
		end
	end

	if upg == "aa" or upg == "ab" or upg == "ac" then
       		xml:InitStatic("aa_r1", self)
		if self.three_level == true then
       			xml:InitStatic("aa_r2", self)
		end
	end

	if upg == "ba" or upg == "bb" or upg == "bc" then
       		xml:InitStatic("ba_r3", self)
		if self.three_level == true then 
       			xml:InitStatic("ba_r4", self)
		end  
	end
		
end


function upgrade_menu:InitCallBacks()
	local upg = tostring(get_upgrade(db.actor:item_in_slot(6):section()))
	self:AddCallback("aa", ui_events.BUTTON_CLICKED, self.OnButton_aa, self)
	self:AddCallback("ab", ui_events.BUTTON_CLICKED, self.OnButton_ab, self)
	self:AddCallback("ac", ui_events.BUTTON_CLICKED, self.OnButton_ac, self)
	self:AddCallback("ba", ui_events.BUTTON_CLICKED, self.OnButton_ba, self)
	self:AddCallback("bb", ui_events.BUTTON_CLICKED, self.OnButton_bb, self)
	self:AddCallback("bc", ui_events.BUTTON_CLICKED, self.OnButton_bc, self)

	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 self.three_level == true then
		self:AddCallback("ca", ui_events.BUTTON_CLICKED, self.OnButton_ca, self)
		self:AddCallback("cb", ui_events.BUTTON_CLICKED, self.OnButton_cb, self)
		self:AddCallback("cc", ui_events.BUTTON_CLICKED, self.OnButton_cc, self)
	end
end

function upgrade_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_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)
	if level.present() and (db.actor ~= nil) and db.actor:alive() then
		console:execute("main_menu off")
	end
end

function upgrade_menu:OnMsgYes()
	local outf = db.actor:item_in_slot(6)
	local realsect = GetRealSect(0)
	if self.upgraded == true then
		if db.actor:money() >= self.repair_cost then
			outf:set_condition(0.98)
			db.actor:give_money(-self.repair_cost)
			lwc.PlaySound([[inv_sounds\tiski_bronik_3]])
			news_manager.send_tip(db.actor, "Костюм отремонтирован", nil, nil, 5000)
		else
			news_manager.send_tip(db.actor, "Недостаточно денег", nil, nil, 5000)			
		end
		self.message_box:DetachChild(wnd_i)
		self.upgraded = false
		return
	end
		
	if next_upg and next_upg ~= "nil" then
		if db.actor:money() >= self.cost then
			db.actor:give_money(-self.cost)
			alife():release(alife():object(db.actor:item_in_slot(6):id()))		
			amk.spawn_item_in_inv(realsect.."_"..next_upg)
			news_manager.send_tip(db.actor, "Модификация установлена", nil, nil, 5000)
			if next_upg == "aa" then
				wnd_aa = CUIStatic()
				wnd_aa:SetAutoDelete(true)
				self:AttachChild(wnd_aa)
				wnd_aa:SetStretchTexture(true)	
				wnd_aa:SetWndRect(172,56,90,44)
				wnd_aa:InitTexture("seva_aa_i")

				wnd_r1 = CUIStatic()
				wnd_r1:SetAutoDelete(true)
				self:AttachChild(wnd_r1)
				wnd_r1:SetStretchTexture(true)	
				wnd_r1:SetWndRect(172,108,289,44)
				wnd_r1:InitTexture("upgrade_red_i")
				if self.three_level == true then
					wnd_r2 = CUIStatic()
					wnd_r2:SetAutoDelete(true)
					self:AttachChild(wnd_r2)
					wnd_r2:SetStretchTexture(true)	
					wnd_r2:SetWndRect(172,161,289,44)
					wnd_r2:InitTexture("upgrade_red_i")
				end
			elseif next_upg == "ab" then
				wnd_ab = CUIStatic()
				wnd_ab:SetAutoDelete(true)
				self:AttachChild(wnd_ab)
				wnd_ab:SetStretchTexture(true)	
				wnd_ab:SetWndRect(272,56,90,44)
				wnd_ab:InitTexture("seva_aa_i")
			elseif next_upg == "ac" then
				wnd_ac = CUIStatic()
				wnd_ac:SetAutoDelete(true)
				self:AttachChild(wnd_ac)
				wnd_ac:SetStretchTexture(true)	
				wnd_ac:SetWndRect(372,56,90,44)
				wnd_ac:InitTexture("seva_aa_i")
			elseif next_upg == "ba" then
				wnd_ba = CUIStatic()
				wnd_ba:SetAutoDelete(true)
				self:AttachChild(wnd_ba)
				wnd_ba:SetStretchTexture(true)	
				wnd_ba:SetWndRect(172,108,90,44)
				wnd_ba:InitTexture("seva_aa_i")

				wnd_r1 = CUIStatic()
				wnd_r1:SetAutoDelete(true)
				self:AttachChild(wnd_r1)
				wnd_r1:SetStretchTexture(true)	
				wnd_r1:SetWndRect(172,56,289,44)
				wnd_r1:InitTexture("upgrade_red_i")
				if self.three_level == true then
					wnd_r2 = CUIStatic()
					wnd_r2:SetAutoDelete(true)
					self:AttachChild(wnd_r2)
					wnd_r2:SetStretchTexture(true)	
					wnd_r2:SetWndRect(172,161,289,44)
					wnd_r2:InitTexture("upgrade_red_i")
				end
			elseif next_upg == "bb" then
				wnd_bb = CUIStatic()
				wnd_bb:SetAutoDelete(true)
				self:AttachChild(wnd_bb)
				wnd_bb:SetStretchTexture(true)	
				wnd_bb:SetWndRect(272,108,90,44)
				wnd_bb:InitTexture("seva_aa_i")
			elseif next_upg == "bc" then
				wnd_bc = CUIStatic()
				wnd_bc:SetAutoDelete(true)
				self:AttachChild(wnd_bc)
				wnd_bc:SetStretchTexture(true)	
				wnd_bc:SetWndRect(372,108,90,44)
				wnd_bc:InitTexture("seva_aa_i")
			elseif next_upg == "ca" then
				wnd_ca = CUIStatic()
				wnd_ca:SetAutoDelete(true)
				self:AttachChild(wnd_ca)
				wnd_ca:SetStretchTexture(true)	
				wnd_ca:SetWndRect(172,161,90,44)
				wnd_ca:InitTexture("seva_aa_i")

				wnd_r1 = CUIStatic()
				wnd_r1:SetAutoDelete(true)
				self:AttachChild(wnd_r1)
				wnd_r1:SetStretchTexture(true)	
				wnd_r1:SetWndRect(172,56,289,44)
				wnd_r1:InitTexture("upgrade_red_i")

				wnd_r2 = CUIStatic()
				wnd_r2:SetAutoDelete(true)
				self:AttachChild(wnd_r2)
				wnd_r2:SetStretchTexture(true)	
				wnd_r2:SetWndRect(172,108,289,44)
				wnd_r2:InitTexture("upgrade_red_i")
			elseif next_upg == "cb" then
				wnd_cb = CUIStatic()
				wnd_cb:SetAutoDelete(true)
				self:AttachChild(wnd_cb)
				wnd_cb:SetStretchTexture(true)	
				wnd_cb:SetWndRect(272,161,90,44)
				wnd_cb:InitTexture("seva_aa_i")
			elseif next_upg == "cc" then
				wnd_cc = CUIStatic()
				wnd_cc:SetAutoDelete(true)
				self:AttachChild(wnd_cc)
				wnd_cc:SetStretchTexture(true)	
				wnd_cc:SetWndRect(372,161,90,44)
				wnd_cc:InitTexture("seva_aa_i")
			end
			if next_upg == "aa" or next_upg == "ba" or next_upg == "ca" then
				lwc.PlaySound([[inv_sounds\tiski_bronik_1]])
			elseif next_upg == "ab" or next_upg == "bb" or next_upg == "cb" then
				lwc.PlaySound([[inv_sounds\tiski_bronik_2]])
			elseif next_upg == "ac" or next_upg == "bc" or next_upg == "cc" then
				lwc.PlaySound([[inv_sounds\tiski_bronik_3]])
			end
		else
			news_manager.send_tip(db.actor, "Недостаточно денег", nil, nil, 5000)
		end						
	end
end

function upgrade_menu:OnMsgNo()
	self.mbox_mode = 0
	self.message_box:DetachChild(wnd_i)
	self.upgraded = false
end

function upgrade_menu:OnButton_aa()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))
	if upg == "nil" then
		next_upg = "aa"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_aa")
			self.cost = 35000
		elseif string.find(outf:section(), "exo") then
			self:msg_init("message_exo_aa")
			self.cost = 75000
		elseif string.find(outf:section(), "stalker_outfit") or string.find(outf:section(), "dolg_outfit") or string.find(outf:section(), "killer_outfit")
		or string.find(outf:section(), "svoboda_light_outfit") or string.find(outf:section(), "specops_free") or string.find(outf:section(), "svoboda_heavy_outfit") then
			self:msg_init("message_stalker_aa")
			self.cost = 20000
		elseif string.find(outf:section(), "sky_heavy") then
			self:msg_init("message_cs_aa")
			self.cost = 30000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_aa")
			self.cost = 35000
		elseif string.find(outf:section(), "specops_outfit") then
			self:msg_init("message_spec_aa")
			self.cost = 35000
		elseif string.find(outf:section(), "leather_raincoat") then
			self:msg_init("message_plash_aa")
			self.cost = 30000
		end			 
	elseif upg == "aa" or upg == "ab" or upg == "ac" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "aa" and upg ~= "ab" and upg ~= "ac" then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_ab()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))	
	if upg == "aa" then
		next_upg = "ab"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_ab")
			self.cost = 70000
		elseif string.find(outf:section(), "exo") then
			self:msg_init("message_exo_ab")
			self.cost = 100000
		elseif string.find(outf:section(), "stalker_outfit") or string.find(outf:section(), "dolg_outfit") or string.find(outf:section(), "killer_outfit")
		or string.find(outf:section(), "svoboda_light_outfit") or string.find(outf:section(), "specops_free") or string.find(outf:section(), "svoboda_heavy_outfit") then
			self:msg_init("message_stalker_ab")
			self.cost = 35000
		elseif string.find(outf:section(), "sky_heavy") then
			self:msg_init("message_cs_ab")
			self.cost = 50000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_ab")
			self.cost = 70000
		elseif string.find(outf:section(), "specops_outfit") then
			self:msg_init("message_spec_ab")
			self.cost = 70000
		elseif string.find(outf:section(), "leather_raincoat") then
			self:msg_init("message_plash_ab")
			self.cost = 50000
		end
	elseif upg == "ab" or upg == "ac" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "ab" or upg ~= "ac" then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_ac()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))	
	if upg == "ab" then
		next_upg = "ac"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_ac")
			self.cost = 125000
		elseif string.find(outf:section(), "exo") then
			self:msg_init("message_exo_ac")
			self.cost = 125000
		elseif string.find(outf:section(), "stalker_outfit") or string.find(outf:section(), "dolg_outfit") or string.find(outf:section(), "killer_outfit")
		or string.find(outf:section(), "svoboda_light_outfit") or string.find(outf:section(), "specops_free") or string.find(outf:section(), "svoboda_heavy_outfit") then
			self:msg_init("message_stalker_ac")
			self.cost = 50000
		elseif string.find(outf:section(), "sky_heavy") then
			self:msg_init("message_cs_ac")
			self.cost = 75000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_ac")
			self.cost = 125000
		elseif string.find(outf:section(), "specops_outfit") then
			self:msg_init("message_spec_ac")
			self.cost = 125000
		elseif string.find(outf:section(), "leather_raincoat") then
			self:msg_init("message_plash_ac")
			self.cost = 75000
		end
	elseif upg == "ac" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "ac" then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_ba()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))	
	if upg == "nil" then
		next_upg = "ba"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_ba")
			self.cost = 40000
		elseif string.find(outf:section(), "exo") then
			self:msg_init("message_exo_ba")
			self.cost = 90000
		elseif string.find(outf:section(), "stalker_outfit") or string.find(outf:section(), "dolg_outfit") or string.find(outf:section(), "killer_outfit")
		or string.find(outf:section(), "svoboda_light_outfit") or string.find(outf:section(), "specops_free") or string.find(outf:section(), "svoboda_heavy_outfit") then
			self:msg_init("message_stalker_ba")
			self.cost = 25000
		elseif string.find(outf:section(), "sky_heavy") then
			self:msg_init("message_cs_ba")
			self.cost = 35000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_ba")
			self.cost = 40000
		elseif string.find(outf:section(), "specops_outfit") then
			self:msg_init("message_spec_ba")
			self.cost = 40000
		elseif string.find(outf:section(), "leather_raincoat") then
			self:msg_init("message_plash_ba")
			self.cost = 35000
		end
	elseif upg == "ba" or upg == "bb" or upg == "bc" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "ba" and upg ~= "bb" and upg ~= "bc" then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_bb()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))	
	if upg == "ba" then
		next_upg = "bb"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_bb")
			self.cost = 80000
		elseif string.find(outf:section(), "exo") then
			self:msg_init("message_exo_bb")
			self.cost = 125000
		elseif string.find(outf:section(), "stalker_outfit") or string.find(outf:section(), "dolg_outfit") or string.find(outf:section(), "killer_outfit")
		or string.find(outf:section(), "svoboda_light_outfit") or string.find(outf:section(), "specops_free") or string.find(outf:section(), "svoboda_heavy_outfit") then
			self:msg_init("message_stalker_bb")
			self.cost = 50000
		elseif string.find(outf:section(), "sky_heavy") then
			self:msg_init("message_cs_bb")
			self.cost = 60000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_bb")
			self.cost = 80000
		elseif string.find(outf:section(), "specops_outfit") then
			self:msg_init("message_spec_bb")
			self.cost = 80000
		elseif string.find(outf:section(), "leather_raincoat") then
			self:msg_init("message_plash_bb")
			self.cost = 60000
		end
	elseif upg == "bb" or upg == "bc" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "bb" and upg ~= "bc" then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_bc()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))	
	if upg == "bb" then
		next_upg = "bc"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_bc")
			self.cost = 120000
		elseif string.find(outf:section(), "exo") then
			self:msg_init("message_exo_bc")
			self.cost = 150000
		elseif string.find(outf:section(), "stalker_outfit") or string.find(outf:section(), "dolg_outfit") or string.find(outf:section(), "killer_outfit")
		or string.find(outf:section(), "svoboda_light_outfit") or string.find(outf:section(), "specops_free") or string.find(outf:section(), "svoboda_heavy_outfit") then
			self:msg_init("message_stalker_bc")
			self.cost = 75000
		elseif string.find(outf:section(), "sky_heavy") then
			self:msg_init("message_cs_bc")
			self.cost = 80000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_bc")
			self.cost = 120000
		elseif string.find(outf:section(), "specops_outfit") then
			self:msg_init("message_spec_bc")
			self.cost = 120000
		elseif string.find(outf:section(), "leather_raincoat") then
			self:msg_init("message_plash_bc")
			self.cost = 80000
		end
	elseif upg == "bc" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "bc" then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_ca()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))	
	if upg == "nil" then
		next_upg = "ca"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_ca")
			self.cost = 50000
		elseif string.find(outf:section(), "exo") and not string.find(outf:section(), "mercenary_exo")  then
			self:msg_init("message_exo_ca")
			self.cost = 100000
		elseif string.find(outf:section(), "mercenary_exo") then
			self:msg_init("message_merc_ca")
			self.cost = 100000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_ca")
			self.cost = 50000
		elseif string.find(outf:section(), "specops_outfit") or string.find(outf:section(), "specops_free") then
			self:msg_init("message_spec_ca")
			self.cost = 50000			
		end
	elseif upg == "ca" or upg == "cb" or upg == "cc" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "ca" and upg ~= "cb" and upg ~= "cc" then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_cb()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))	
	if upg == "ca" then
		next_upg = "cb"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_cb")
			self.cost = 100000
		elseif string.find(outf:section(), "exo") and not string.find(outf:section(), "mercenary_exo") then
			self:msg_init("message_exo_cb")
			self.cost = 135000
		elseif string.find(outf:section(), "mercenary_exo") then
			self:msg_init("message_merc_cb")
			self.cost = 135000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_cb")
			self.cost = 100000
		elseif string.find(outf:section(), "specops_outfit") or string.find(outf:section(), "specops_free") then
			self:msg_init("message_spec_cb")
			self.cost = 100000				
		end
	elseif upg == "cb" or upg == "cc" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "cb" and upg ~= "cc"  then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_cc()
	local outf = db.actor:item_in_slot(6)
	local upg = tostring(get_upgrade(outf:section()))	
	if upg == "cb" then
		next_upg = "cc"
		if string.find(outf:section(), "scientific") or string.find(outf:section(), "proseva") then
			self:msg_init("message_seva_cc")
			self.cost = 150000
		elseif string.find(outf:section(), "exo") and not string.find(outf:section(), "mercenary_exo") then
			self:msg_init("message_exo_cc")
			self.cost = 170000
		elseif string.find(outf:section(), "mercenary_exo") then
			self:msg_init("message_merc_cc")
			self.cost = 170000
		elseif string.find(outf:section(), "military") then
			self:msg_init("message_mil_cc")
			self.cost = 150000
		elseif string.find(outf:section(), "specops_outfit") or string.find(outf:section(), "specops_free") then
			self:msg_init("message_spec_cc")
			self.cost = 150000
		end
	elseif upg == "cc" then
		news_manager.send_tip(db.actor, "Эта часть уже модифицирована", nil, nil, 5000)
	elseif upg ~= "cc" then
		news_manager.send_tip(db.actor, "Невозможно установить данную модификацию", nil, nil, 5000)
	end
end

function upgrade_menu:OnButton_repair()
	local outf = db.actor:item_in_slot(6)
	if outf:condition() > 0.97 then
		news_manager.send_tip(db.actor, "Костюм не нуждается в ремонте", nil, nil, 5000)
		return
	elseif outf:condition() < 0.3 then
		news_manager.send_tip(db.actor, "Костюм не подлежит ремонту", nil, nil, 5000)
		return
	end

	self:msg_init("message_repair")
	
	self.repair_cost = math.floor( (1-outf:condition()) * outf: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.." рублей. Произвести ремонт?")

	self.upgraded = true	
end

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

function upgrade_menu:msg_init(text)
	self.mbox_mode = 1
	self.message_box:Init(text)
	self:GetHolder():start_stop_menu(self.message_box, true)
end

function GetRealSect(mode)
	local par = system_ini():r_string(db.actor:item_in_slot(6):section(),"upg")
	if mode == 1 then
		if par == "nil" then
			local result = "upg_"..db.actor:item_in_slot(6):section()
			return result
		else
			local real_sect = string.sub(db.actor:item_in_slot(6):section(),1,(string.len(db.actor:item_in_slot(6):section())-3))
			local result = "upg_"..real_sect
			return result
		end
	else
		if par == "nil" then
			local result = db.actor:item_in_slot(6):section()
			return result
		else
			local real_sect = string.sub(db.actor:item_in_slot(6):section(),1,(string.len(db.actor:item_in_slot(6):section())-3))
			local result = real_sect
			return result
		end
	end
end

function GetClass(sect)
	local res = system_ini():r_string(sect,"class")
	if res ~= nil and res ~= 0 then
		return res
	end	
end

function upgrade_menu:on_quit()
	self.three_level = false
	self:GetHolder():start_stop_menu (self,true)
end