--If both DriverControls are enabled and touch controls are enabled then the accelerate and brake buttons will appear.


function VehicleGui:EnableDriverControls()
	self.driverControlsEnabled = true

	if self.touchEnabled then
		self:DisplayTouchDriveControls()
	end
end

function VehicleGui:EnableSpeedo()
	self.speedoFrame.Visible = true
end

function VehicleGui:DisableSpeedo()
	self.speedoFrame.Visible = false
end

function VehicleGui:DisplayTouchDriveControls()
	self.brakeButton.Visible = true
	self.accelButton.Visible = true

	--Read button inputs
	local ADown = false
	local BDown = false
	local HDown = false
	local function ProcessTouchThrottle()
		if (ADown and BDown) or (not ADown and not BDown) then
			self.throttleInput = 0
		elseif ADown then
			self.throttleInput = 1
		elseif BDown then
			self.throttleInput = -1
		end
		if HDown then
			self.handBrakeInput = 1
		else
			self.handBrakeInput = 0
		end
	end
	--Accel
	self.accelButton.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch then
			ADown = true
			self.accelButton.ImageTransparency = 1
			self.accelButton.Pressed.ImageTransparency = 0
			ProcessTouchThrottle()
		end
	end)
	self.accelButton.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch then
			ADown = false
			self.accelButton.ImageTransparency = 0
			self.accelButton.Pressed.ImageTransparency = 1
			ProcessTouchThrottle()
		end
	end)
	--Brake
	local lastBrakeTapTime = 0
	local brakeLastInputBegan = 0
	self.brakeButton.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch then
			if tick() - lastBrakeTapTime <= DOUBLE_TAP_THRESHOLD then
				HDown = true
			else
				brakeLastInputBegan = tick()
				BDown = true
			end
			self.brakeButton.ImageTransparency = 1
			self.brakeButton.Pressed.ImageTransparency = 0
			ProcessTouchThrottle()
		end
	end)
	self.brakeButton.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch then
			if not HDown then
				if tick() - brakeLastInputBegan <= SINGLE_TAP_THRESHOLD then
					lastBrakeTapTime = tick()
				end
			end
			brakeLastInputBegan = 0
			HDown = false
			BDown = false
			self.brakeButton.ImageTransparency = 0
			self.brakeButton.Pressed.ImageTransparency = 1
			ProcessTouchThrottle()
		end
	end)
end

function VehicleGui:EnableTouchControls()
	if self.touchEnabled then return end
	self.touchEnabled = true

	disableJumpButton()

	self:ConfigureButtons()

	if self.driverControlsEnabled then
		self:DisplayTouchDriveControls()
	end
end

function VehicleGui:DisableTouchControls()
	if not self.touchEnabled  then return end
	self.touchEnabled = false

	enableJumpButton()

	self.brakeButton.Visible = false
	self.accelButton.Visible = false
end

function VehicleGui:EnableKeyboardUI()
	self.speedoFrame.Size = UDim2.new(0, 400, 0, 90)
	self.speedoOn.Size = self.speedoFrame.Size
	self.speedoOff.Size = self.speedoFrame.Size
	self.speedoFrame.Position = UDim2.new(0.5, 0, 1, -40)
	self.speedoOn.Image = "rbxassetid://2848312414"
	self.speedoOff.Image = "rbxassetid://2848312878"
	self.speedText.TextSize = 40
	self.speedText.Size = UDim2.new(0, 60, 0, 40)
	self.speedText.Position = UDim2.new(0, 215, 0, 94)
	self.unitText.TextSize = 20
	self.unitText.Size = UDim2.new(0, 30, 0, 20)
	self.unitText.Position = UDim2.new(0, 225, 0, 90)

	self:DisableTouchControls()
end

function VehicleGui:EnableTouchUI()
	self.speedoFrame.Size = UDim2.new(0, 240, 0, 54)
	self.speedoOn.Size = self.speedoFrame.Size
	self.speedoOff.Size = self.speedoFrame.Size
	self.speedoFrame.Position = UDim2.new(0.5, 0, 1, -20)
	self.speedoOn.Image = "rbxassetid://2847843718"
	self.speedoOff.Image = "rbxassetid://2847843839"
	self.speedText.TextSize = 24
	self.speedText.Size = UDim2.new(0, 24*3*0.5, 0, 24)
	self.speedText.Position = UDim2.new(0, 120, 0, 54)
	self.unitText.TextSize = 12
	self.unitText.Size = UDim2.new(0, 12*3*0.5, 0, 12)
	self.unitText.Position = UDim2.new(0, 125, 0, 54)

	self:EnableTouchControls()
end

function VehicleGui:EnableGamepadUI()
	self.speedoFrame.Size = UDim2.new(0, 600, 0, 135)
	self.speedoOn.Size = self.speedoFrame.Size
	self.speedoOff.Size = self.speedoFrame.Size
	self.speedoFrame.Position = UDim2.new(0.5, 0, 1, -40)
	self.speedoOn.Image = "rbxassetid://2836208803"
	self.speedoOff.Image = "rbxassetid://2836208488"
	self.speedText.TextSize = 60
	self.speedText.Size = UDim2.new(0, 90, 0, 60)
	self.speedText.Position = UDim2.new(0, 315, 0, 140)
	self.unitText.TextSize = 30
	self.unitText.Size = UDim2.new(0, 45, 0, 30)
	self.unitText.Position = UDim2.new(0, 325, 0, 135)

	self:DisableTouchControls()
end

return VehicleGui