--UserInput.MouseBehavior = Enum.MouseBehavior.LockCenter


function CameraModule.new(Position, Rotation, Sensitivity, move)
	local Camera = {}
	
	Camera.Position = Position
	Camera.Rotation = Rotation
	Camera.CFrame = CFrame.new(Position) * CFrame.Angles(math.rad(Rotation.X), math.rad(Rotation.Y), math.rad(Rotation.Z))
	Camera.Target = Vector3.new(1,0,0)
	Camera.PressedKey = nil

	Camera.fpsController = {
		direction = 0,
		pitch = 0,
	}

	Camera.Sensitivity = Sensitivity
	
	Mouse.KeyDown:Connect(function(key)
		Camera.PressedKey = key
	end)

	Mouse.KeyUp:Connect(function()
		Camera.PressedKey = nil
	end)

	function Camera:Movement()
		local key = Camera.PressedKey

		if key == "e" then
			Camera.CFrame += Vector3.new(0,0.4,0)
		elseif key == "q" then
			Camera.CFrame -= Vector3.new(0,0.4,0)
		elseif key == "w" then
			Camera.CFrame += Vector3.new(0.4*math.cos(-Camera.Rotation.Y), 0, 0.4*math.sin(-Camera.Rotation.Y))
		elseif key == "s" then
			Camera.CFrame -= Vector3.new(0.4*math.cos(-Camera.Rotation.Y), 0, 0.4*math.sin(-Camera.Rotation.Y))
		elseif key == "a" then
			Camera.CFrame += Vector3.new(0.4*math.sin(-Camera.Rotation.Y), 0, -0.4*math.cos(-Camera.Rotation.Y))
		elseif key == "d" then
			Camera.CFrame += Vector3.new(-0.4*math.sin(-Camera.Rotation.Y), 0, 0.4*math.cos(-Camera.Rotation.Y))
		end
		
		task.synchronize()
		Camera:Pan(UserInput:GetMouseDelta().X, UserInput:GetMouseDelta().Y)
		task.desynchronize()
	end

	function Camera:Pan(dx, dy)
		Camera.fpsController.direction = math.max(math.min(Camera.fpsController.direction - dy * Camera.Sensitivity, math.pi * 0.5), math.pi * -0.5)
		Camera.fpsController.pitch = Camera.fpsController.pitch - dx * Camera.Sensitivity
		
		local rotationCFrame = CFrame.Angles(0, Camera.fpsController.pitch, 0) * CFrame.Angles(Camera.fpsController.direction, 0, 0)
		local positionCFrame = CFrame.new(Camera.CFrame.Position)

		Camera.CFrame = positionCFrame * rotationCFrame
	end
	
	function Camera:Update()
		Camera.Position = Camera.CFrame.Position
		local xRotation, yRotation, _ = Camera.CFrame:ToOrientation()
		Camera.Rotation = Vector3.new(xRotation, yRotation + math.rad(90), 0)
		
		if move == true then
			Camera:Movement()
		end
	end

	return Camera
end

return CameraModule
