
TOOL.Category		= "Construction"
TOOL.Name			= "#Light"
TOOL.Command		= nil
TOOL.ConfigName		= ""

TOOL.ClientConVar[ "ropelength" ] = "64"
TOOL.ClientConVar[ "ropematerial" ] = "cable/rope"
TOOL.ClientConVar[ "r" ] = "255"
TOOL.ClientConVar[ "g" ] = "255"
TOOL.ClientConVar[ "b" ] = "255"
TOOL.ClientConVar[ "brightness" ] = "2"
TOOL.ClientConVar[ "size" ] = "256"

cleanup.Register( "lamps" )

// Add Default Language translation (saves adding it to the txt files)
if ( CLIENT ) then

	language.Add( "Tool_light_name", "Light Bulbs" )
	language.Add( "Tool_light_desc", "Create light bulbs" )
	language.Add( "Tool_light_0", "Left click to create with rope, right click to create without" )

end

function TOOL:LeftClick( trace, attach )

	if trace.Entity && trace.Entity:IsPlayer() then return false end
	if (CLIENT) then return true end
	if (attach == nil) then attach = true end
	
	// If there's no physics object then we can't constraint it!
	if ( SERVER && attach && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
	
	local ply = self:GetOwner()
	
	local pos, ang = trace.HitPos + trace.HitNormal * 10, trace.HitNormal:Angle() - Angle( 90, 0, 0 )

	local r 	= math.Clamp( self:GetClientNumber( "r" ), 0, 255 )
	local g 	= math.Clamp( self:GetClientNumber( "g" ), 0, 255 )
	local b 	= math.Clamp( self:GetClientNumber( "b" ), 0, 255 )
	local brght	= math.Clamp( self:GetClientNumber( "brightness" ), 0, 255 )
	local size 	= self:GetClientNumber( "size" )
	
	// Clamp for multiplayer
	if ( !SinglePlayer() ) then
		size = math.Clamp( size, 0, 512 )
		brght = math.Clamp( brght, 0, 20 )
	end
	
	if	trace.Entity:IsValid() && 
		trace.Entity:GetClass() == "gmod_light" &&
		trace.Entity:GetTable():GetPlayer() == ply
	then
		trace.Entity:GetTable():SetLightColor( r, g, b )
		trace.Entity:GetTable().r = r
		trace.Entity:GetTable().g = g
		trace.Entity:GetTable().b = b
		return true
	end
	
	if ( !self:GetSWEP():CheckLimit( "lamps" ) ) then return false end
	lamp = MakeLight( ply, r, g, b, brght, size, { Pos = pos, Angle = ang } )
	
	if (!attach) then 
	
		undo.Create("Lamp")
			undo.AddEntity( lamp )
			undo.SetPlayer( self:GetOwner() )
		undo.Finish()
		
		return true
		
	end

	local length 	= math.Clamp( self:GetClientNumber( "ropelength" ), 4, 1024 )
	local material 	= self:GetClientInfo( "ropematerial" )
	
	local LPos1 = Vector( 0, 0, 5 )
	local LPos2 = trace.Entity:WorldToLocal( trace.HitPos )
	
	if (trace.Entity:IsValid()) then
		
		local phys = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone )
		if (phys:IsValid()) then
			LPos2 = phys:WorldToLocal( trace.HitPos )
		end
	
	end
	
	local constraint, rope = constraint.Rope( lamp, trace.Entity, 
											  0, trace.PhysicsBone, 
											  LPos1, LPos2, 
											  0, length,
											  0, 
											  1.5, 
											  material, 
											  nil )
	
	undo.Create("Lamp")
		undo.AddEntity( lamp )
		undo.AddEntity( rope )
		undo.AddEntity( constraint )
		undo.SetPlayer( ply )
	undo.Finish()

	return true

end

function TOOL:RightClick( trace )

	return self:LeftClick( trace, false )

end

function TOOL.BuildCPanel( CPanel )

	// HEADER
	CPanel:AddControl( "Header", { Text = "#Tool_light_name", Description	= "#Tool_light_desc" }  )
	
	// Presets
	local params = { Label = "#Presets", MenuButton = 1, Folder = "light", Options = {}, CVars = {} }
		
		params.Options.default = {
			light_ropelength	= 		64,
			light_ropematerial	=		"cable/rope",
			light_r				=		255,
			light_g				=		255,
			light_b				=		255,
			light_brightness	=		2,
			light_size			=		256
		}
			
		table.insert( params.CVars, "light_ropelength" )
		table.insert( params.CVars, "light_ropematerial" )
		table.insert( params.CVars, "light_r" )
		table.insert( params.CVars, "light_g" )
		table.insert( params.CVars, "light_b" )
		table.insert( params.CVars, "light_brightness" )
		table.insert( params.CVars, "light_size" )
		
	CPanel:AddControl( "ComboBox", params )
	
	CPanel:AddControl( "Slider",  { Label	= "#Rope Length",
									Type	= "Float",
									Min		= 0,
									Max		= 256,
									Command = "light_ropelength" }	 )
	
	
	CPanel:AddControl( "Color",  { Label	= "#Color",
									Red			= "light_r",
									Green		= "light_g",
									Blue		= "light_b",
									ShowAlpha	= 0,
									ShowHSV		= 1,
									ShowRGB 	= 1,
									Multiplier	= 255 } )	
									
	CPanel:AddControl( "Slider",  { Label	= "#Brightness",
									Type	= "Float",
									Min		= 0,
									Max		= 10,
									Command = "light_brightness" }	 )
									
	CPanel:AddControl( "Slider",  { Label	= "#Size",
									Type	= "Float",
									Min		= 0,
									Max		= 1024,
									Command = "light_size" }	 )

									
end

function MakeLight( pl, r, g, b, brght, size, Data )

	if ( !pl:CheckLimit( "lamps" ) ) then return false end

	local lamp = ents.Create( "gmod_light" )
	
		if (!lamp:IsValid()) then return end
	
		duplicator.DoGeneric( lamp, Data )
		lamp:SetLightColor( r, g, b )
		lamp:SetBrightness( brght )
		lamp:SetLightSize( size )
		
	lamp:Spawn()
	
	duplicator.DoGenericPhysics( lamp, pl, Data )
	
	lamp:SetPlayer( pl )

	pl:AddCount( "lamps", lamp )
	pl:AddCleanup( "lamps", lamp )
	
	return lamp
	
end

duplicator.RegisterEntityClass( "gmod_lamp", MakeLamp, "lightr", "lightg", "lightb", "Brightness", "Size", "Data" )
