

local PANEL = {}
local SelectingToolTreeButton = false

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

	self.Label = vgui.Create( "Label", self )
		self.Label:SetText( "Unnammed Group" )
		self.Label:SetMouseInputEnabled( false )

	self.HighlightColor = NamedColor( "Highlight" )
	self.LastHover = 0
	self.LastSelect = 0

end

/*---------------------------------------------------------
   Name: Setup
---------------------------------------------------------*/
function PANEL:Setup( ToolPanel, iparent, name, label, command, controlpanel, ToolTree, full_table, bMakeConvar )

	self:SetName( name )
	self.Label:SetText( label )
	
	self.ControlPanel = controlpanel
	self.Command = command
	self.ToolPanel = ToolPanel
	
	if ( bMakeConvar ) then
		concommand.Add( "tool_"..name, function() self:ActivateButton() end )
	end
	
	// This button includes a convar to switch on and off (add a checkbox at the right of the button)
	if ( full_table && full_table.SwitchConVar ) then
	
		// This could probably suck a bit less.
		self.Switch = vgui.Create( "ImageCheckBox", self )
		self.Switch:SetMaterial( "gui/silkicons/check_on_s" )
		self.Switch.DoClick = function( self ) 
								local val = GetConVarNumber( full_table.SwitchConVar ) 
								if ( val != 0 ) then val = ' 0\n' else val = ' 1\n' end
								LocalPlayer():ConCommand( full_table.SwitchConVar .. val )
							  end
		self.Switch.Think =  function( self ) self:Set( GetConVarNumber( full_table.SwitchConVar ) != 0 ) end
	
	end
	
	self:PerformLayout()

end

/*---------------------------------------------------------
   Name: OnMousePressed
---------------------------------------------------------*/
function PANEL:OnMousePressed( mcode )

	if ( mcode == MOUSE_LEFT ) then
		SelectingToolTreeButton = true
		self:ActivateButton( true )
	end
	
	// Right mouse button means DRAG
	// This is a kind of lousy way to do this.. but it works.
	if ( mcode == MOUSE_RIGHT ) then
		local Parent = self
		while ( true ) do
		
			Parent = Parent:GetParent()
			if (!Parent) then return end
			
			if ( Parent.ClassName == "DPanelList" && Parent.VBar ) then
				Parent.VBar:Grip()
				return
			end
		
		end
	end
	
end

/*---------------------------------------------------------
   Name: OnMouseReleased
---------------------------------------------------------*/
function PANEL:OnMouseReleased( mcode )

	if ( mcode == MOUSE_LEFT ) then
		SelectingToolTreeButton = false
	end

end

/*---------------------------------------------------------
   Name: OnCursorMoved
---------------------------------------------------------*/
function PANEL:OnCursorMoved( x, y )

	if ( SelectingToolTreeButton ) then
		self:ActivateButton()
	end

end

/*---------------------------------------------------------
   Name: ActivateButton
---------------------------------------------------------*/
function PANEL:ActivateButton( bPlaySound )

	if ( self.ControlPanel ) then
		self.ToolPanel:SelectControlPanel( self.ControlPanel )
	end
	
	if ( self.Command && self.Command != "" ) then
		LocalPlayer():ConCommand( self.Command )
	end
	
	// Don't keep playing the sound if we're already selected
	if ( !bPlaySounds && self.ToolPanel.Selected && self.ToolPanel.Selected == self ) then return end

	surface.PlaySound( "ui/buttonclick.wav" )
	self.ToolPanel.Selected = self
	
	self:RequestFocus()
	
end

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

	self.Label:SetPos( 4, 1 )
	self.Label:SetSize( self:GetWide(), 15 )
	self:SetSize( self:GetWide(), 17 )
	
	if ( self.Switch ) then
		self.Switch:SetPos( self:GetWide() - 17, 3 )
		self.Switch:SetSize( 12, 12 )
	end

end

/*---------------------------------------------------------
   Name: Paint
---------------------------------------------------------*/
function PANEL:Paint()
	
	if ( self.Num %2 == 1 ) then
		surface.SetDrawColor( 0, 0, 0, 50 )		
	else
		surface.SetDrawColor( 0, 0, 0, 70 )
	end
	
	local w = self:GetWide()
	local h = self:GetTall()
	
	surface.DrawRect( 0, 0, w, h )
	
	if ( self.ToolPanel.Selected == self ) then
	
		local strobe = math.sin( RealTime() * 5 ) * 20
		
		surface.SetDrawColor( math.Clamp( self.HighlightColor.r+strobe, 0, 255 ), 
							  math.Clamp( self.HighlightColor.g+strobe, 0, 255 ), 
							  math.Clamp( self.HighlightColor.b+strobe, 0, 255 ), 
							  255 )
							  
		surface.DrawRect( 0, 0, w, h )
		self.LastSelect = RealTime()
		
	else
	
		// Fade out selection
		local Delta = 1 - math.Clamp( (RealTime() - self.LastSelect) / 5, 0, 1 )
		if ( Delta > 0 ) then
			surface.SetDrawColor( self.HighlightColor.r, self.HighlightColor.g, self.HighlightColor.b, 100 * Delta )
			surface.DrawRect( 0, 0, w, h )
		end
		
		if ( self.Hovered ) then
			surface.SetDrawColor( 255, 200, 50, 200 )
			surface.DrawRect( 0, 0, w, h )
		end
	
	end
	
	return true
	
end

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

	self.Label:SetFGColor( 255, 255, 255, 255 )

end

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

