--- DONT TOUCH ---


local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Values = script.Parent.Parent:WaitForChild("Values")
local car = script.Parent.Parent.Car.Value
local DriveSeat = car:WaitForChild("DriveSeat")
local Tune = require(car:WaitForChild("A-Chassis Tune"))
local IsOn = Values.Parent:WaitForChild("IsOn")
local LightEvent = car:WaitForChild("LightEvent")

local LI = script.Values.Left
local RI = script.Values.Right
local H = script.Values.Hazard
local RL = script.Values.RunningLights
local HL = script.Values.Headlights

local setup = false
local current_seq_seg = 1
local flashing = false
local parklightActive = false
local drlActive = false
local fogActive = false
local popupsActive = false

local gui_tweeninfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0)
local indgui_tweeninfo = TweenInfo.new(Indicator_Flash_Rate / 2.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0)

function input(key, gameprocessed)
	if not gameprocessed then
		if key.KeyCode == Headlights then
			if HL.Value == 0 then
				HL.Value = 1
				LightEvent:FireServer("Headlights", Low_Beam_Brightness, 0.2, 48, parklightActive, Popups_Enabled, Headlight_Type, HL.Value)
				LightEvent:FireServer("RearLights", Rear_Light_Type, Rear_Light_Brightness, 0.2, Trunk_Lights)
				if fogActive then
					LightEvent:FireServer("FogLights", Fog_Light_Brightness, 0.02, Fog_Light_Type)
					TweenService:Create(script.Parent.FogLights, gui_tweeninfo, {ImageTransparency = 0}):Play()
				end
				TweenService:Create(script.Parent.LowBeams, gui_tweeninfo, {ImageTransparency = 0}):Play()
			elseif HL.Value == 1 then
				HL.Value = 2		
				LightEvent:FireServer("Headlights", High_Beam_Brightness, 0.02, 60, parklightActive, Popups_Enabled, Headlight_Type, HL.Value)
				TweenService:Create(script.Parent.LowBeams, gui_tweeninfo, {ImageTransparency = 1}):Play()
				TweenService:Create(script.Parent.HighBeams, gui_tweeninfo, {ImageTransparency = 0}):Play()
			elseif HL.Value == 2 then	
				HL.Value = 0
				LightEvent:FireServer("Headlights", 0, 1, 60, parklightActive, Popups_Enabled, Headlight_Type, HL.Value)
				if not parklightActive then
					LightEvent:FireServer("RearLights", Rear_Light_Type, 0, 1, Trunk_Lights)
				end	
				LightEvent:FireServer("FogLights", 0, 1, Fog_Light_Type)
				TweenService:Create(script.Parent.LowBeams, gui_tweeninfo, {ImageTransparency = 1}):Play()
				TweenService:Create(script.Parent.HighBeams, gui_tweeninfo, {ImageTransparency = 1}):Play()
				TweenService:Create(script.Parent.FogLights, gui_tweeninfo, {ImageTransparency = 1}):Play()
			end	
			if Plate_Lights then
				if HL.Value >= 1 then
					LightEvent:FireServer("PlateLights", true, Plate_Light_Type)
				else	
					LightEvent:FireServer("PlateLights", false, Plate_Light_Type)
				end
			end
		elseif key.KeyCode == Popups then
			if Popups_Enabled then
				if popupsActive == false then
					LightEvent:FireServer("Popups", true, Popups_Enabled, Popup_Hinge_Angle)
					popupsActive = true
				else
					LightEvent:FireServer("Popups", false, Popups_Enabled, Popup_Hinge_Angle)
					popupsActive = false				
				end
			end	
		elseif key.KeyCode == RunningLights then
			if Running_Light_Location ~= "Custom DRL" then
				if not parklightActive then
					parklightActive = true
					TweenService:Create(script.Parent.ParkLights, gui_tweeninfo, {ImageTransparency = 0}):Play()
				else
					parklightActive = false
					TweenService:Create(script.Parent.ParkLights, gui_tweeninfo, {ImageTransparency = 1}):Play()
				end
			else
				if not drlActive then
					drlActive = true
					LightEvent:FireServer("CustomDRL", drlActive, CustomDRL_Type)
					TweenService:Create(script.Parent.ParkLights, gui_tweeninfo, {ImageTransparency = 0}):Play()
				else
					drlActive = false
					LightEvent:FireServer("CustomDRL", drlActive, CustomDRL_Type)
					TweenService:Create(script.Parent.ParkLights, gui_tweeninfo, {ImageTransparency = 1}):Play()
				end
			end	
			if HL.Value == 0 then
				LightEvent:FireServer("Headlights", 0, 1, 60, parklightActive, Popups_Enabled, Headlight_Type, HL.Value)
				if parklightActive or drlActive then
					LightEvent:FireServer("RearLights", Rear_Light_Type, Rear_Light_Brightness, 0.2, Trunk_Lights)
				elseif not parklightActive or drlActive then
					LightEvent:FireServer("RearLights", Rear_Light_Type, 0, 1, Trunk_Lights)
				end	
			end
		elseif key.KeyCode == FogLights then
			if HL.Value > 0 then
				if not fogActive then
					LightEvent:FireServer("FogLights", Fog_Light_Brightness, 0.02, Fog_Light_Type)
					TweenService:Create(script.Parent.FogLights, gui_tweeninfo, {ImageTransparency = 0}):Play()
					fogActive = true
				else
					LightEvent:FireServer("FogLights", 0, 1, Fog_Light_Type)
					TweenService:Create(script.Parent.FogLights, gui_tweeninfo, {ImageTransparency = 1}):Play()
					fogActive = false					
				end
			else
				if not fogActive then
					fogActive = true
				else
					fogActive = false
				end
			end
		elseif key.KeyCode == Flash then
			if not flashing then

			end
		elseif key.KeyCode == SignalLeft then
			if not LI.Value then
				LI.Value = true
				script.StackOn:Play()
			else
				LI.Value = false
				script.StackOff:Play()
			end	
		elseif key.KeyCode == SignalRight then
			if not RI.Value then
				RI.Value = true
				script.StackOn:Play()
			else
				RI.Value = false
				script.StackOff:Play()
			end
		elseif key.KeyCode == Hazards then
			if not H.Value then
				H.Value = true
			else
				H.Value = false	
			end
		end
	end
end


function autoLight()
	local dusk = {16, 18}
	local night = {18, 6.2}
	local timenow = game.Lighting:GetMinutesAfterMidnight() / 60
	
	if timenow >= dusk[1] and timenow <= dusk[2] then
		HL.Value = 0
		parklightActive = true
		LightEvent:FireServer("Headlights", Low_Beam_Brightness, 0.2, 48, parklightActive, Popups_Enabled, Headlight_Type, HL.Value)		
		LightEvent:FireServer("RearLights", Rear_Light_Type, Rear_Light_Brightness, 0.2, Trunk_Lights)
		TweenService:Create(script.Parent.ParkLights, gui_tweeninfo, {ImageTransparency = 0}):Play()
	elseif timenow > night[1] or (timenow >= 0 and timenow <= night[2]) then
		HL.Value = 1
		LightEvent:FireServer("Headlights", Low_Beam_Brightness, 0.02, 48, parklightActive, Popups_Enabled, Headlight_Type, HL.Value)
		LightEvent:FireServer("RearLights", Rear_Light_Type, Rear_Light_Brightness, 0.2, Trunk_Lights)
		TweenService:Create(script.Parent.LowBeams, gui_tweeninfo, {ImageTransparency = 0}):Play()
	else
		HL.Value = 0
		LightEvent:FireServer("Headlights", 0, 1, 0, parklightActive, Popups_Enabled, Headlight_Type, HL.Value)
		LightEvent:FireServer("RearLights", Rear_Light_Type, 0, 1, Trunk_Lights)
	end
end



function brakes()
	if Values.Brake.Value > 0.01 then 
		LightEvent:FireServer("BrakeLights", Rear_Light_Type, Brake_Light_Brightness, 0.02, Trunk_Lights)
	else
		LightEvent:FireServer("BrakeLights", Rear_Light_Type, 0, 1, Trunk_Lights)
	end
end

function reverse()
	if Values.Gear.Value == -1 then
		LightEvent:FireServer("Reverse", Reverse_Light_Type, Reverse_Light_Brightness, 0.02, Trunk_Lights)
	else
		LightEvent:FireServer("Reverse", Reverse_Light_Type, 0, 1, Trunk_Lights)
	end
end

function leavedriveseat()
	LightEvent:FireServer("IndsOnLeave", LI.Value, RI.Value, H.Value, Sequential_Indicators, Sequential_Segments, Indicator_Flash_Rate, Indicator_Type)
end
