

local PANEL = {}

/*---------------------------------------------------------
   Name: Init
---------------------------------------------------------*/
function PANEL:Init()

	local Tree = self

	self.Tree = vgui.Create( "ToolTree_Tree", self )
	
	self.SearchTree = vgui.Create( "ToolTree_Tree", self )
	self.SearchTree:SetVisible( false )

	self.ToggleAll = vgui.Create( "ImageButton", self )
	self.ToggleAll:SetMaterial( "gui/silkicons/application_cascade" )
	self.ToggleAll:SetText( "-" )
	self.ToggleAll.DoClick = function () Tree:ToggleExpands() end
	
	self.Expanded = true
	
	self.SearchBox = vgui.Create( "TextEntry", self )
	self.SearchValue = ""
	
end

/*---------------------------------------------------------
   Name: ToggleExpands
---------------------------------------------------------*/
function PANEL:ToggleExpands()
	
	self.Expanded = !self.Expanded
	
	if ( self.Expanded ) then
		self.ToggleAll:SetText( "-" )
	else
		self.ToggleAll:SetText( "+" )
	end
	
	self.Tree:SetAllExpanded( self.Expanded )
	
end

/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function PANEL:Think()
	
	if ( !self.ToolPanel ) then return end
	if ( self.SearchValue == self.SearchBox:GetValue() ) then return end
	
	self.SearchValue = self.SearchBox:GetValue()
	self.SearchValue = self.SearchValue:lower()
	
	self.SearchTree:Clear()
	
	if ( self.SearchValue == "" ) then
	
		self.Tree:SetVisible( true )
		self.SearchTree:SetVisible( false )
		return
		
	end
	
	self.Tree:SetVisible( false )
	self.SearchTree:SetVisible( true )
	
	local iParent = self.SearchTree:AddItem( self.ToolPanel, -1, "Results", "Search Results", nil, nil )
	
	for k, v in pairs( self.Tree.Items ) do
	
		if ( v.ClassName == "ToolTree_Button" ) then
			
			local label = string.lower( v.Label:GetValue() )
			local name = string.lower( v:GetName() )
			
			if ( name:find( self.SearchValue ) || label:find( self.SearchValue ) ) then
				 
				self.SearchTree:AddItem( self.ToolPanel, iParent, v:GetName(), v.Label:GetValue(), v.Command, v.ControlPanel )
				
			end
		
		end
	
	end
		
end

/*---------------------------------------------------------
   Name: AddItem
---------------------------------------------------------*/
function PANEL:AddItem( toolpanel, iParent, name, label, command, controlpanel, full_table )

	self.ToolPanel = toolpanel
	return self.Tree:AddItem( toolpanel, iParent, name, label, command, controlpanel, full_table, true )

end


/*---------------------------------------------------------
   Name: PerformLayout
---------------------------------------------------------*/
function PANEL:PerformLayout()

	self.Tree:SetPos( 0, 0 )
	self.Tree:SetSize( self:GetWide(), self:GetTall() - 25 )
	self.Tree:PerformLayout()
	
	self.SearchTree:SetPos( 0, 0 )
	self.SearchTree:SetSize( self:GetWide(), self:GetTall() - 25 )
	self.SearchTree:PerformLayout()
	
	
	self.ToggleAll:SetPos( self:GetWide() - 20, self:GetTall() - 18 )
	self.ToggleAll:SetSize( 16, 16 )
	
	self.SearchBox:SetPos( 0, self:GetTall() - 20 )
	self.SearchBox:SetSize( self:GetWide() - 20 - 5, 20 )
	
end


/*---------------------------------------------------------
   Name: Paint
---------------------------------------------------------*/
function PANEL:Paint()

end

vgui.Register( "ToolTree", PANEL, "Panel" )