local dataLink = script:WaitForChild("Data Link").Value

local mouseDown = false
local parts = {}
local centers = {}
function rotateMod(mod, rotation)
	for _, part in pairs(parts) do
		part.CFrame = (centers[part.Identifier.Value] * rotation * (centers[part.Identifier.Value]:inverse() * part.CFrame))
	end
end




local Clockwork = script.Parent.Parent
local IdentifierValue = 0
local function scan(parent)
	for i, object in pairs(parent:GetChildren()) do
		if object.Name == "ClockworkDecor" or object.Name == "ClockworkDial" then
		elseif (object:IsA("BasePart")) then
			local _Identifier = Instance.new("IntValue")
			_Identifier.Name = "Identifier"
			_Identifier.Value = IdentifierValue
			_Identifier.Parent = object
			IdentifierValue = IdentifierValue + 1
			table.insert(parts, object)
			table.insert(centers, object.Identifier.Value, object.Parent:GetModelCFrame())
		end
		scan(object)
	end
end

scan(Clockwork)



function goForward()
	while mouseDown do
		local step = 1
		for i = 0, 5, step do
			rotateMod(Clockwork, CFrame.Angles(0, -0.2, 0))
			dataLink.temporalLocatorTime.Value = dataLink.temporalLocatorTime.Value + (1 * dataLink.temporalLocatorTimeMultiplier.Value)
			wait()
		end
	end
end



function goBack()
	while mouseDown do
		local step = 1
		for i = 0, 5, step do
			rotateMod(Clockwork, CFrame.Angles(0, 0.2, 0))
			dataLink.temporalLocatorTime.Value = dataLink.temporalLocatorTime.Value - (1 * dataLink.temporalLocatorTimeMultiplier.Value)
			wait()
		end
	end
end


function leftClick()
	if mouseDown then
		return
	end
	mouseDown = true
	script.Parent.Main.Sound.Volume = 1.2
	script.Parent.Main.Sound.PlaybackSpeed = 1
	goBack()
end

function rightClick()
	if mouseDown then
		return
	end
	mouseDown = true
	script.Parent.Main.Sound.Volume = 1.3
	script.Parent.Main.Sound.PlaybackSpeed = 0.8
	goForward()
end


function Leave()
	mouseDown = false
	script.Parent.Main.Sound.Volume = 0
end


script.Parent.Use.MouseClick:connect(leftClick)
script.Parent.Use.RightMouseClick:Connect(rightClick)
script.Parent.Use.MouseHoverLeave:connect(Leave)

