TOOL.ClientConVar[ "locked" ] 	= "0"
TOOL.ClientConVar[ "key" ] 	= "0"
TOOL.ClientConVar[ "toggle" ] 	= "1"

cleanup.Register( "cameras" )

GAMEMODE.CameraList = GAMEMODE.CameraList or {}

function TOOL:LeftClick( trace )

	local key	= tonumber( self:GetClientInfo( "key" ) )
	if (key == -1) then return false end

	if (CLIENT) then return true end

	local ply 	= self:GetOwner()
	local locked	= tonumber( self:GetClientInfo( "locked" ) )
	local toggle	= tonumber( self:GetClientInfo( "toggle" ) )
	local pid	= ply:UniqueID()

	GAMEMODE.CameraList[ pid ] 	= GAMEMODE.CameraList[ pid ] or {}
	local CameraList = GAMEMODE.CameraList[ pid ]

	local Pos = trace.StartPos// + trace.Normal * 16

	// If the camera already exists then remove it
	if (CameraList[ key ] && CameraList[ key ] != NULL ) then

		local ent = CameraList[ key ]
		ent:Remove()
	end

	local camera = ents.Create( "gmod_cameraprop" )
	if (!camera:IsValid()) then return false end

		camera:SetAngles( ply:EyeAngles() )
		camera:SetPos( Pos )
		camera:Spawn()
		
		camera:GetTable():SetKey( key )
		camera:GetTable():SetPlayer( ply )
		camera:GetTable():SetLocked( locked )
		camera:GetTable().toggle = toggle

		camera:GetTable():SetTracking( NULL, Vector(0) )
		
		if toggle == 1 then
			numpad.OnDown(	ply, key, "Camera_Toggle",  camera )
		else
			numpad.OnDown(	ply, key, "Camera_On",  camera )
			numpad.OnUp(	ply, key, "Camera_Off", camera )
		end

	undo.Create("Camera")
		undo.AddEntity( camera )
		undo.SetPlayer( ply )
	undo.Finish()

	ply:AddCleanup( "camera", camera )

	
	CameraList[ key ] = camera
	
	return true, camera

end

function TOOL:RightClick( trace )

	_, camera = self:LeftClick( trace, true )
	
	if (CLIENT) then return true end

	if ( !camera || !camera:IsValid() ) then return end
	
	if ( trace.Entity:IsWorld() ) then
	
		trace.Entity = self:GetOwner()
		trace.HitPos = self:GetOwner():GetPos()
	
	end

	camera:GetTable():SetTracking( trace.Entity, trace.Entity:WorldToLocal( trace.HitPos ))
	
	return true
	
end
