--------------------------------------------------------------------------------

local Car		= script.Parent.Car.Value
local Values 	= script.Parent.Values
local _Tune 	= require(Car["A-Chassis Tune"])
local Handler	= Car:WaitForChild("ENGINE_PACK")
local FE		= workspace.FilteringEnabled

local ENG_PCK = nil
for _,Child in pairs(Car.Misc:GetDescendants()) do
	if Child:IsA("ObjectValue") and Child.Name == "ENGINE_PACK" then ENG_PCK = Child.Value end
end

local CG = ENG_PCK:FindFirstChild("Cam_Gears")
local ES = ENG_PCK:FindFirstChild("Engine_Shake")
local MH = ENG_PCK:FindFirstChild("Manifold_Heat")
local TB = ENG_PCK:FindFirstChild("Throttle_Body")
local TI = ENG_PCK:FindFirstChild("Timing_Belt")
local TC = ENG_PCK:FindFirstChild("Turbochargers")

local VAL = 0

local ThrottleChanged = Values.Throttle.Changed:Connect(function()
	if FE then
		Handler:FireServer("Throttle",Values.Throttle.Value,TB,THROTTLE_BODY)
	else
		
		if THROTTLE_BODY and TB ~= nil then
			for i,Body in pairs(TB:GetChildren()) do
				if string.sub(Body.Name,1,4) == "Body" then
					local Motor = Body.W:FindFirstChildOfClass("Motor")
					Motor.DesiredAngle = Values.Throttle.Value * 1.5
				end
			end
		end
		
	end
end)

local RPMChanged = Values.RPM.Changed:Connect(function()

	if FE then
		Handler:FireServer("RPM",Values.RPM.Value,CG,ES,TI,CAM_GEARS,ENGINE_SHAKE,TIMING_BELT,GEAR_SPEED,SHAKE_VALUE,BELT_SPEED)
	else
		
		if CAM_GEARS and CG ~= nil then
			for i,Gear in pairs(CG:GetChildren()) do
				if string.sub(Gear.Name,1,4) == "Gear" then
					local Motor = Gear.W:FindFirstChildOfClass("Motor")
					Motor.DesiredAngle = 9e9
					Motor.MaxVelocity = Values.RPM.Value/1000 * GEAR_SPEED
				end
			end
		end
		
		if ENGINE_SHAKE and ES ~= nil then
			local Motor = ES.W:FindFirstChildOfClass("Motor")
			Motor.MaxVelocity = Values.RPM.Value/50000 * (SHAKE_VALUE/10)
		end
		
		if TIMING_BELT and TI ~= nil then
			for i,Beam in pairs(TI.Beams:GetChildren()) do
				Beam.TextureSpeed = Values.RPM.Value/1000 * BELT_SPEED
			end
		end
		
	end
end)

local BoostChanged = Values.Boost.Changed:connect(function()
	
	if FE then
		Handler:FireServer("Boost",Values.Boost.Value,_Tune,TC,TURBO_CHARGED,TURBO_BLUR,TURBO_SPEED)
	else
		
		if TURBO_CHARGED and TC ~= nil then
			for i,Turbo in pairs(TC:GetChildren()) do
				if string.sub(Turbo.Name,1,5) == "Turbo" then
					local Motor = Turbo.W:FindFirstChildOfClass("Motor")
					Motor.DesiredAngle = 9e9
					if math.floor(Values.Boost.Value) * TURBO_SPEED == 0 then
						Motor.MaxVelocity = 0
					else
						Motor.MaxVelocity = Values.Boost.Value * TURBO_SPEED
					end
					if TURBO_BLUR and Turbo.Parts:FindFirstChild("MotionBlur") then
						Turbo.Parts.MotionBlur.Blur.Transparency = 1 - (Values.Boost.Value/_Tune.T_Boost/2*0.9)
					end
				end
			end
		end
		
	end
end)

local Disable = script.Parent.IsOn.Changed:Connect(function()
	if FE then Handler:FireServer("Disable",script.Parent.IsOn.Value,ENG_PCK,CAM_GEARS,ENGINE_SHAKE,MANIFOLD_HEAT,THROTTLE_BODY,TIMING_BELT,TURBO_CHARGED)
	else
		if script.Parent.IsOn.Value == true then
			CAM_GEARS		= true
			ENGINE_SHAKE 	= true
			MANIFOLD_HEAT	= true
			THROTTLE_BODY	= true
			TIMING_BELT		= true
			TURBO_CHARGED	= true
		else
			CAM_GEARS		= false
			ENGINE_SHAKE 	= false
			MANIFOLD_HEAT	= false
			THROTTLE_BODY	= false
			TIMING_BELT		= false
			TURBO_CHARGED	= false
			for i,Object in pairs(ENG_PCK:GetDescendants()) do
				if Object:IsA("Motor") then
					Object.MaxVelocity = 0
				end
				if Object:IsA("Beam") then
					Object.TextureSpeed = 0
				end
			end
		end
	end
end)

Car.DriveSeat.ChildRemoved:Connect(function(Child)
	if Child.Name == "SeatWeld" and LEAVE_DISABLE then
		if FE then
			Handler:FireServer("Disable",false,ENG_PCK,CAM_GEARS,ENGINE_SHAKE,MANIFOLD_HEAT,THROTTLE_BODY,TIMING_BELT,TURBO_CHARGED)
		else
			CAM_GEARS		= false
			ENGINE_SHAKE 	= false
			MANIFOLD_HEAT	= false
			THROTTLE_BODY	= false
			TIMING_BELT		= false
			TURBO_CHARGED	= false
			for i,Object in pairs(ENG_PCK:GetDescendants()) do
				if Object:IsA("Motor") then
					Object.MaxVelocity = 0
				end
				if Object:IsA("Beam") then
					Object.TextureSpeed = 0
				end
			end
		end
	end
end)

if FE then
	Handler:FireServer("Loop",ES,MH,TI,ENGINE_SHAKE,MANIFOLD_HEAT,TIMING_BELT,SHAKE_ANGLE,MH_THRESHOLD,MH_COOLDOWN,BELT_TEXTURE,BELT_WIDTH)
else
	spawn(function()

		if ENGINE_SHAKE and ES ~= nil then
			ES.W.Motor.DesiredAngle = math.rad(SHAKE_ANGLE)
		end
		
		if TIMING_BELT and TI ~= nil then
			for i,Beam in pairs(TI.Beams:GetChildren()) do
				if Beam:IsA("Beam") then
					Beam.Texture = "rbxassetid://"..BELT_TEXTURE
					Beam.Width0 = BELT_WIDTH
					Beam.Width1 = BELT_WIDTH
				end
			end
		end
		
		while wait() do

			if ENGINE_SHAKE and ES ~= nil then
				ES.W.Motor.DesiredAngle = -(ES.W.Motor.DesiredAngle)
			end

			if MANIFOLD_HEAT and MH ~= nil then
				if Values.RPM.Value > MH_THRESHOLD then
					VAL = math.max(math.min(VAL + Values.RPM.Value/900000,1),0)
					for i,ManifoldPart in pairs(MH.Parts:GetDescendants()) do
						if ManifoldPart:IsA("BasePart") then
							ManifoldPart.Color = Color3.fromRGB((110*VAL)+60,(45*VAL)+40,40-(40*VAL))
						end
					end
				elseif Values.RPM.Value < MH_THRESHOLD then
					VAL = math.max(math.min(VAL + -(MH_COOLDOWN/1000),1),0)
					for i,ManifoldPart in pairs(MH.Parts:GetDescendants()) do
						if ManifoldPart:IsA("BasePart") then
							ManifoldPart.Color = Color3.fromRGB((110*VAL)+60,(45*VAL)+40,40-(40*VAL))
						end
					end
				end
			end

		end
	end)
end