-- Copyright Declaration
-- This file is part of the "Faction Commander Mod", a personal production of rgggclp.
-- You could copy, modify and distribute this file only if you keep this header intact as the originality of this file.
-- If you have questions, feel free to contact me at rggg_clp@163.com
-- 版权声明
-- 这个程序是“阵营指挥官”的一部分，它是Chen Lin Peng的个人作品
-- 你可以拷贝、修改和再次发布这个文件，不过你必须保留这个版权声明，它表示这个文件的来源，谢谢

RegisterdTab = { 	{ class = faction_commander_ui.UIFactionCommander, 	id = "faction_commander" },
					{ class = faction_commander_ui.UIFactionChanger, 	id = "faction_changer" },
					{ class = faction_commander_ui.UISummoner, 			id = "summoner" }	}



class "TabController" ( CUIScriptWnd )
function TabController:__init( ingame ) super()
	self.ingame = ingame
	self:SetFont( GetFontMedium() )
    self:InitControls()
    self:InitCallBacks()
end
function TabController:__finalize()
end

function TabController:InitControls()
	--1
	self:SetWndRect( Frect():set( 0, 0, 1024, 768 ) )
	self:Enable(true)
	
	--2
	local xml= CScriptXmlInit()
	xml:ParseFile( "ui_tab_controller.xml" )
	
	--3
	local background = xml:InitStatic( "background", self )
	
	--4
	local workArea = CUIWindow()
	xml:InitWindow( "wrk_area", 0, workArea )
	workArea:SetAutoDelete( true )
	self:AttachChild( workArea )
	
	--5
	local dialog = xml:InitStatic( "main_dialog:dialog", workArea )
	xml:InitStatic( "main_dialog:cap_dialog", dialog )
	
	--6
	xml:InitAnimStatic( "main_dialog:anim", dialog )
	
	--7
	self.tabs = {}
	for i, reg in pairs( RegisterdTab ) do
		local tab = reg.class()
		tab:SetWndPos( vector2():set( 0, 0 ) )
		tab:SetWndSize( vector2():set( 738, 416 ) )
		tab:SetAutoDelete( true )
		xml:InitWindow( "main_dialog:tab_size", 0, tab )

		tab:Show( false )
		dialog:AttachChild( tab )
		tab:SetWindowName( reg.id )
		table.insert( self.tabs, tab )
	end

	--8
	self.tabBar = xml:InitTab( "main_dialog:tab", dialog )
	self:Register( self.tabBar, "tabBar" )
	if #RegisterdTab > 0 then
		self.tabBar:SetActiveTab( RegisterdTab[ 1 ].id )
		self.tabs[ 1 ]:Show( true )
	end
	
	--9
	local btn = xml:Init3tButton( "main_dialog:btn_esc", dialog )
	self:Register( btn, "btn_esc" )
end

function TabController:InitCallBacks()
	self:AddCallback( "tabBar",		ui_events.TAB_CHANGED,		self.OnTabChange,	self )
	self:AddCallback( "btn_esc",	ui_events.BUTTON_CLICKED,	self.OnBtnESC,		self )
end

function TabController:OnTabChange()
	local id = self.tabBar:GetActiveId()
	for i, tab in pairs( self.tabs ) do
		if id == tab:WindowName() then
			tab:Show( true )
		else
			tab:Show( false )
		end
	end
end

function TabController:OnBtnESC()
	if self.ingame then
		self:GetHolder():start_stop_menu( self, true )
	else
		self:GetHolder():start_stop_menu( self.owner, true )
		self:GetHolder():start_stop_menu( self, true )
		self.owner:Show( true )
	end
end

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

function TabController:Update()
	CUIScriptWnd.Update(self)
end
