
local PANEL = {}

/*---------------------------------------------------------
   Name: This function is used as the paint function for 
		   selected buttons.
---------------------------------------------------------*/
local function HighlightedButtonPaint( self )

	surface.SetDrawColor( 255, 200, 0, 255 )
	
	for i=2, 3 do
		surface.DrawOutlinedRect( i, i, self:GetWide()-i*2, self:GetTall()-i*2 )
	end

end

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

	// A panellist is a panel that you shove other panels
	// into and it makes a nice organised frame.
	self.List = vgui.Create( "PanelList", self )
		self.List:EnableHorizontal( true )
		self.List:EnableVerticalScrollbar()
		self.List:SetSpacing( 1 )
		self.List:SetPadding( 3 )
	
	self.Controls 	= {}
	self.Height		= 2
	
	self.ItemWidth = 128
	self.ItemHeight = 128

end

/*---------------------------------------------------------
   Name: ControlValues
---------------------------------------------------------*/
function PANEL:AddMaterial( label, value )

	// Creeate a spawnicon and set the model
	local Mat = vgui.Create( "Material", self )
	Mat:SetMaterial( value )
	Mat.AutoSize = false
	Mat.ConVar = self:ConVar()
	Mat.Value = value
	Mat:SetSize( self.ItemWidth, self.ItemHeight )
	
	// Run a console command when the Icon is clicked
	Mat.DoClick = 	function ( self ) 
	
						LocalPlayer():ConCommand( Format( "%s \"%s\"\n", self.ConVar, self.Value ) ) 

					end

	
	// Add the Icon us
	self.List:AddItem( Mat )
	table.insert( self.Controls, Mat )

end

/*---------------------------------------------------------
   Name: ControlValues
---------------------------------------------------------*/
function PANEL:ControlValues( kv )

	self.BaseClass.ControlValues( self, kv )
	
	self.Height = kv.height or 2
	
	// Load the list of models from our keyvalues file
	if (kv.options) then
	
		for k, v in pairs( kv.options ) do
			self:AddMaterial( k, v )
		end
		
	end
	
	// If we have a category add them from there too
	// This allows people to add new props to us without
	// editing the kv file(s).
	if ( kv.category && GAMEMODE.GetPropCategory ) then

		local Props = {}
		GAMEMODE:GetPropCategory( kv.category, Props )
	
		for k, v in pairs( Props ) do
			self:AddModel( k, v )
		end
	
	end
	
	self.ItemWidth = kv.itemwidth or 128
	self.ItemHeight = kv.itemheight or 128
	
	for k, v in pairs( self.Controls ) do
	
		v:SetSize( self.ItemWidth, self.ItemHeight )
	
	end
	
	
	
	self:InvalidateLayout( true )

end

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

	local y = self.BaseClass.PerformLayout( self )
	
	local Height = self.ItemHeight * self.Height + 6
	
	self.List:SetPos( 10, y )
	self.List:SetSize( self:GetWide() - 20, Height )
	
	y = y + Height
	y = y + 5
	
	self:SetTall( y )

end



/*---------------------------------------------------------
   Name: FindAndSelectMaterial
---------------------------------------------------------*/
function PANEL:FindAndSelectMaterial( Value )

	self.CurrentValue = Value

	for k, Mat in pairs( self.Controls ) do
	
		if ( Mat.Value == Value ) then
		
			// Remove the old overlay
			if ( self.SelectedMaterial ) then
				self.SelectedMaterial.PaintOver = nil
			end
			
			// Add the overlay to this button
			Mat.PaintOver = HighlightedButtonPaint;
			self.SelectedMaterial = Mat

		end
	
	end

end

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

	local Value = GetConVarString( self:ConVar() )
	
	if ( Value == self.CurrentValue ) then return end
	
	self:FindAndSelectMaterial( Value )

end


vgui.Register( "MatSelect", PANEL, "ContextBase" )