-- OBJECT DIRECTORY


local machine = script.Parent
local jackpotValues = machine.JackpotValues
local lights = machine.Lights
local lights2 = machine.Lights2
local ticketValues = machine.TicketValues
local arm = machine.Arm
local button = machine.StopButton
local isSpinning = false
local zone = {5,4,3,1,2,3,4,5,5,4,3,1,2,3,4,5}
local lzone = {6,5,5,4,4,3,3,1,1,2,2,3,3,4,4,5,5,6,5,5,4,4,3,3,1,1,2,2,3,3,4,4,5,5}
local active = false
local index = 1

wait()
machine.CreditsDisp.Value.Value = 0
for _,a in pairs(jackpotValues:GetChildren()) do a.SurfaceGui.TextLabel.Text = values[6] end
for i = 1, 16 do ticketValues[tostring(i)].SurfaceGui.TextLabel.Text = values[zone[i]] end

function start()
	machine.CreditsDisp.Value.Value = machine.Credits.Value
	if not active and machine.Credits.Value > 0 then
		active = true
		isSpinning = false
		arm.ClickDetector.MaxActivationDistance = 10
		machine.Timer.Value.Value = 20
		machine.Timer.Disabled = false
	end
end

function clickArm(plr)
	if plr == 1 or plr.Name == machine.CardReader.GameInfo.Player.Value then
		isSpinning = true
		button.LeverPull.TimePosition = 1
		button.LeverPull:Play()
		machine.Timer.Disabled = true
		machine.Timer.Value.Value = 0
		arm.ClickDetector.MaxActivationDistance = 0
		arm.HingeConstraint.TargetAngle = 50
		wait(0.5)
		arm.HingeConstraint.TargetAngle = 0
		wait(0.5)
		for _,a in pairs(machine.Lights3:GetChildren()) do a.Material = Enum.Material.Neon end
		button.Click:Play()
		button.Material = Enum.Material.Neon
		machine.Timer.Value.Value = 20
		machine.Timer.Disabled = false
		index = 0
		repeat
			index = index + 1
			if index == 35 then index = 1 end
			lights[tostring(index)].Material = Enum.Material.Neon
			wait(wheelSpeed)
			if button.Material == Enum.Material.Neon then lights[tostring(index)].Material = Enum.Material.SmoothPlastic end
		until button.Material == Enum.Material.SmoothPlastic
	end
end

function clickButton(plr)
	if button.Material == Enum.Material.Neon and (plr == 1 or plr.Name == machine.CardReader.GameInfo.Player.Value) then
		button.Click:Stop()
		machine.Timer.Disabled = true
		machine.Timer.Value.Value = 0
		button.Material = Enum.Material.SmoothPlastic
		for _,a in pairs(machine.Lights3:GetChildren()) do a.Material = Enum.Material.SmoothPlastic end
		if lzone[index] == 6 then
			local r = math.random(1, jpChance)
			if r ~= 1 then
				lights[tostring(index)].Material = Enum.Material.SmoothPlastic
				index = index + 1
			end
		end
		ticketOut(values[lzone[index]])
		if lzone[index] == 6 then
			button.Bell:Play()
			button.JPMusic.TimePosition = 0.5
			button.JPMusic:Play()
			for i = 1, 12 do
				lights[tostring(index)].Material = Enum.Material.Neon
				wait(.5)
				for _,a in pairs(lights:GetChildren()) do a.Material = Enum.Material.Neon end
				for _,a in pairs(lights2:GetChildren()) do a.Material = Enum.Material.SmoothPlastic end
				wait(.5)
				for _,a in pairs(lights:GetChildren()) do a.Material = Enum.Material.SmoothPlastic end
				for _,a in pairs(lights2:GetChildren()) do a.Material = Enum.Material.Neon end
			end
			for _,a in pairs(lights2:GetChildren()) do a.Material = Enum.Material.SmoothPlastic end
			button.JPMusic:Stop()
		else
			button.Win:Play()
			for i = 1, 8 do
				lights[tostring(index)].Material = Enum.Material.Neon
				wait(0.5)
				lights[tostring(index)].Material = Enum.Material.SmoothPlastic
				wait(0.5)
			end
		end
		active = false
		machine.Credits.Value = machine.Credits.Value - 1
	end
end

machine.Timer.Changed:connect(function()
	if machine.Timer.Value.Value == 0 and not machine.Timer.Disabled then
		if isSpinning then clickButton(1) else clickArm(1) end
	end
end)

arm.ClickDetector.MouseClick:connect(clickArm)
button.ClickDetector.MouseClick:connect(clickButton)
machine.Credits.Changed:connect(start)
function ticketOut(tix)
	machine.CardReader.GameInfo.TicketsWon.Value = machine.CardReader.GameInfo.TicketsWon.Value + tix
end