--[[
Copyright: © Èðáès , Real Wolf
]]

local wnd_s1,wnd_s2,wnd_st1,wnd_st2,slot

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

class "upg_main_menu" (CUIScriptWnd)

function upg_main_menu:__init(owner) super()
   	self.owner = owner
   	self:InitControls()
  	self:InitCallBacks()	
end

function upg_main_menu:__finalize() end

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

     	xml:InitStatic("background", self)

	wnd_s1 = CUIStatic()
	wnd_s1:SetAutoDelete(true)
	self:AttachChild(wnd_s1)
	wnd_s1:SetStretchTexture(true)	
	wnd_s1:SetWndRect(80,60,326,128)
	wnd_s1:InitTexture("ui_slot_texture")

	if db.actor:item_in_slot(1) ~= nil then
		if string.find(db.actor:item_in_slot(1):section(),"wpn_") and not string.find(db.actor:item_in_slot(1):section(),"_m") then
		self:Register(xml:Init3tButton("slot1", self),"1slot")

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

			wnd_st1 = CUIStatic()
			wnd_st1:SetAutoDelete(true)
			self:AttachChild(wnd_st1)
			wnd_st1:SetStretchTexture(true)	
			wnd_st1:SetWndRect(100,85,w,h)
			wnd_st1:InitTexture(db.actor:item_in_slot(1):section())
		else

		local slot1_sect = lwc_ui_slot_upgrade.GetRealSect(db.actor:item_in_slot(1):section())
		local upg = tostring(lwc_ui_slot_upgrade.GetUpgrade(db.actor:item_in_slot(1):section()))

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

		wnd_st1 = CUIStatic()
		wnd_st1:SetAutoDelete(true)
		self:AttachChild(wnd_st1)
		wnd_st1:SetStretchTexture(true)	
		wnd_st1:SetWndRect(100,85,w,h)
		wnd_st1:InitTexture(slot1_sect)

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

	end

	wnd_s2 = CUIStatic()
	wnd_s2:SetAutoDelete(true)
	self:AttachChild(wnd_s2)
	wnd_s2:SetStretchTexture(true)	
	wnd_s2:SetWndRect(80,235,326,128)
	wnd_s2:InitTexture("ui_slot_texture")

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

	if db.actor:item_in_slot(2) ~= nil then
		if string.find(db.actor:item_in_slot(2):section(),"wpn_") and not string.find(db.actor:item_in_slot(2):section(),"_m") then
		self:Register(xml:Init3tButton("slot2", self),"2slot")

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

			wnd_st2 = CUIStatic()
			wnd_st2:SetAutoDelete(true)
			self:AttachChild(wnd_st2)
			wnd_st2:SetStretchTexture(true)	
			wnd_st2:SetWndRect(100,238,w,h)
			wnd_st2:InitTexture(db.actor:item_in_slot(2):section())
		else

		local slot2_sect = lwc_ui_slot_upgrade.GetRealSect(db.actor:item_in_slot(2):section())
		local upg = tostring(lwc_ui_slot_upgrade.GetUpgrade(db.actor:item_in_slot(2):section()))
		local w = system_ini():r_float(slot2_sect,"inv_grid_width")*50
		local h = system_ini():r_float(slot2_sect,"inv_grid_height")*50

		wnd_st2 = CUIStatic()
		wnd_st2:SetAutoDelete(true)
		self:AttachChild(wnd_st2)
		wnd_st2:SetStretchTexture(true)	
		wnd_st2:SetWndRect(100,238,w,h)
		wnd_st2:InitTexture(slot2_sect)

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


function upg_main_menu:InitCallBacks()
	self:AddCallback("btn_quit", ui_events.BUTTON_CLICKED, self.on_quit, self)
	self:AddCallback("1slot", ui_events.BUTTON_CLICKED, self.OnButton_1slot, self)
	self:AddCallback("2slot", ui_events.BUTTON_CLICKED, self.OnButton_2slot, self)
end

function upg_main_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 main_menu:OnMsgOk()
	self.mbox_mode = 0
end

function upg_main_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 upg_main_menu:OnButton_1slot()
	lwc_ui_slot_upgrade.slot = 1
	self:GetHolder():start_stop_menu (self,true)
	local spwn = lwc_ui_slot_upgrade.upgrade_slot_menu(get_hud())
	level.start_stop_menu(spwn,true)	
end

function upg_main_menu:OnButton_2slot()
	lwc_ui_slot_upgrade.slot = 2
	self:GetHolder():start_stop_menu (self,true)
	local spwn = lwc_ui_slot_upgrade.upgrade_slot_menu(get_hud())
	level.start_stop_menu(spwn,true)	
end

function upg_main_menu:on_quit()
	self:GetHolder():start_stop_menu (self,true)
end