-- Realistic Vehicle Controls v1.0
-- by skyrayfox

-- CONFIGURE KEYS BELOW - defaults listen on the right:
key = {}
key.closemenu      = 8 -- "backspace"
key.openmenu       = 114 -- "F3"
key.enterveh       = 70 -- "F"
key.driveforwards  = 87 -- "W" drive forward key (used to disengage cruise)
key.drivebackwards = 83 -- "S" drive backward key (used to disengage cruise)

key.ctrl           = 17 -- "Control" modifier used with the keys below (eg. Ctrl+S for brake lights)
key.turnleft       = 65 -- "A" left turn signal
key.turnright      = 68 -- "D" right turn signal
key.brakelights    = 83 -- "S" brake lights
key.siren          = 90 -- "Z" blip police siren
key.cruise         = 87 -- "W" toggle cruise
key.engine         = 88 -- "X" toggle engine

-- KEY CODES:
------------------------------------
-- Keys.None               = 0
-- Keys.LButton            = 1
-- Keys.RButton            = 2
-- Keys.Cancel             = 3
-- Keys.MButton            = 4
-- Keys.XButton1           = 5
-- Keys.XButton2           = 6
-- Keys.Back               = 8
-- Keys.Tab                = 9
-- Keys.LineFeed           = 10
-- Keys.Clear              = 12
-- Keys.Return             = 13
-- Keys.ShiftKey           = 16
-- Keys.ControlKey         = 17
-- Keys.Menu               = 18
-- Keys.Pause              = 19
-- Keys.Capital            = 20
-- Keys.KanaMode           = 21
-- Keys.JunjaMode          = 23
-- Keys.FinalMode          = 24
-- Keys.HanjaMode          = 25
-- Keys.Escape             = 27
-- Keys.IMEConvert         = 28
-- Keys.IMENonconvert      = 29
-- Keys.IMEAceept          = 30
-- Keys.IMEModeChange      = 31
-- Keys.Space              = 32
-- Keys.PageUp             = 33
-- Keys.Next               = 34
-- Keys.End                = 35
-- Keys.Home               = 36
-- Keys.Left               = 37
-- Keys.Up                 = 38
-- Keys.Right              = 39
-- Keys.Down               = 40
-- Keys.Select             = 41
-- Keys.Print              = 42
-- Keys.Execute            = 43
-- Keys.PrintScreen        = 44
-- Keys.Insert             = 45
-- Keys.Delete             = 46
-- Keys.Help               = 47
-- Keys.D0                 = 48
-- Keys.D1                 = 49
-- Keys.D2                 = 50
-- Keys.D3                 = 51
-- Keys.D4                 = 52
-- Keys.D5                 = 53
-- Keys.D6                 = 54
-- Keys.D7                 = 55
-- Keys.D8                 = 56
-- Keys.D9                 = 57
-- Keys.A                  = 65
-- Keys.B                  = 66
-- Keys.C                  = 67
-- Keys.D                  = 68
-- Keys.E                  = 69
-- Keys.F                  = 70
-- Keys.G                  = 71
-- Keys.H                  = 72
-- Keys.I                  = 73
-- Keys.J                  = 74
-- Keys.K                  = 75
-- Keys.L                  = 76
-- Keys.M                  = 77
-- Keys.N                  = 78
-- Keys.O                  = 79
-- Keys.P                  = 80
-- Keys.Q                  = 81
-- Keys.R                  = 82
-- Keys.S                  = 83
-- Keys.T                  = 84
-- Keys.U                  = 85
-- Keys.V                  = 86
-- Keys.W                  = 87
-- Keys.X                  = 88
-- Keys.Y                  = 89
-- Keys.Z                  = 90
-- Keys.LWin               = 91
-- Keys.RWin               = 92
-- Keys.Apps               = 93
-- Keys.Sleep              = 95
-- Keys.NumPad0            = 96
-- Keys.NumPad1            = 97
-- Keys.NumPad2            = 98
-- Keys.NumPad3            = 99
-- Keys.NumPad4            = 100
-- Keys.NumPad5            = 101
-- Keys.NumPad6            = 102
-- Keys.NumPad7            = 103
-- Keys.NumPad8            = 104
-- Keys.NumPad9            = 105
-- Keys.Multiply           = 106
-- Keys.Add                = 107
-- Keys.Separator          = 108
-- Keys.Subtract           = 109
-- Keys.Decimal            = 110
-- Keys.Divide             = 111
-- Keys.F1                 = 112
-- Keys.F2                 = 113
-- Keys.F3                 = 114
-- Keys.F4                 = 115
-- Keys.F5                 = 116
-- Keys.F6                 = 117
-- Keys.F7                 = 118
-- Keys.F8                 = 119
-- Keys.F9                 = 120
-- Keys.F10                = 121
-- Keys.F11                = 122
-- Keys.F12                = 123
-- Keys.F13                = 124
-- Keys.F14                = 125
-- Keys.F15                = 126
-- Keys.F16                = 127
-- Keys.F17                = 128
-- Keys.F18                = 129
-- Keys.F19                = 130
-- Keys.F20                = 131
-- Keys.F21                = 132
-- Keys.F22                = 133
-- Keys.F23                = 134
-- Keys.F24                = 135
-- Keys.NumLock            = 144
-- Keys.Scroll             = 145
-- Keys.LShiftKey          = 160
-- Keys.RShiftKey          = 161
-- Keys.LControlKey        = 162
-- Keys.RControlKey        = 163
-- Keys.LMenu              = 164
-- Keys.RMenu              = 165
-- Keys.BrowserBack        = 166
-- Keys.BrowserForward     = 167
-- Keys.BrowserRefresh     = 168
-- Keys.BrowserStop        = 169
-- Keys.BrowserSearch      = 170
-- Keys.BrowserFavorites   = 171
-- Keys.BrowserHome        = 172
-- Keys.VolumeMute         = 173
-- Keys.VolumeDown         = 174
-- Keys.VolumeUp           = 175
-- Keys.MediaNextTrack     = 176
-- Keys.MediaPreviousTrack = 177
-- Keys.MediaStop          = 178
-- Keys.MediaPlayPause     = 179
-- Keys.LaunchMail         = 180
-- Keys.SelectMedia        = 181
-- Keys.LaunchApplication1 = 182
-- Keys.LaunchApplication2 = 183
-- Keys.Oem1               = 186
-- Keys.Oemplus            = 187
-- Keys.Oemcomma           = 188
-- Keys.OemMinus           = 189
-- Keys.OemPeriod          = 190
-- Keys.OemQuestion        = 191
-- Keys.Oemtilde           = 192
-- Keys.OemOpenBrackets    = 219
-- Keys.Oem5               = 220
-- Keys.Oem6               = 221
-- Keys.Oem7               = 222
-- Keys.Oem8               = 223
-- Keys.OemBackslash       = 226
-- Keys.ProcessKey         = 229
-- Keys.Packet             = 231
-- Keys.Attn               = 246
-- Keys.Crsel              = 247
-- Keys.Exsel              = 248
-- Keys.EraseEof           = 249
-- Keys.Play               = 250
-- Keys.Zoom               = 251
-- Keys.NoName             = 252
-- Keys.Pa1                = 253
-- Keys.OemClear           = 254
-- Keys.KeyCode            = 65535
-- Keys.Shift              = 65536
-- Keys.Control            = 131072
-- Keys.Alt                = 262144
-- Keys.Modifiers          = -65536
------------------------------------

local rvc = {}

isDoorOpen = false
isAllLightsOn = false
isLeftIndicatorOn = false
isRightIndicatorOn = false
isInteriorLightOn = false
isBrakeLightOn = false
isExtraLightOn = false
isEngineOff = false
setCruiseSpeed = 0

function rvc.tick()
	-- variables
	playerPed = PLAYER.PLAYER_PED_ID()
	player = PLAYER.GET_PLAYER_PED(playerPed)
	playerVehicle = PED.GET_VEHICLE_PED_IS_USING(playerPed)
	playerVehicleSpeed = ENTITY.GET_ENTITY_SPEED(playerVehicle)
	isPlayerDriving	= (VEHICLE.GET_PED_IN_VEHICLE_SEAT(playerVehicle, -1) == playerPed)
	-- variables end

	-- run UI
	rvc.GUI.tick()

	-- hide UI
	if(get_key_pressed(key.closemenu)) then
		rvc.GUI.hidden = true
	end

	-- show UI
	if(get_key_pressed(key.openmenu)) then
		rvc.GUI.hidden = false
	end

	-- "F" leave veh with engine on
	if(get_key_pressed(key.enterveh)) then
		if (isPlayerDriving == true) then
			rvc.leaveVehicle()			
		end
	end

	-- "Ctrl+F" leave veh with engine off
	if(get_key_pressed(key.enterveh) and get_key_pressed(key.ctrl)) then
		if (isPlayerDriving == true) then
			rvc.leaveVehicleEngineOff()
		end
	end

	-- "Ctrl+A" toggle left indicator
	if get_key_pressed(key.turnleft) and get_key_pressed(key.ctrl) then
			rvc.toggleLeftIndicator()
	end

	-- "Ctrl+D" toggle right indicator
	if get_key_pressed(key.turnright) and get_key_pressed(key.ctrl) then
			rvc.toggleRightIndicator()
	end

	-- "Ctrl+S" toggle brake lights
	if get_key_pressed(key.brakelights) and get_key_pressed(key.ctrl) then
		isBrakeLightOn = not isBrakeLightOn
		rvc.keyDelay()
	end
	if (isBrakeLightOn == true) then
		VEHICLE.SET_VEHICLE_BRAKE_LIGHTS(playerVehicle, true)
	end

	-- blip siren "Ctrl+Z" (police cars only)
	if get_key_pressed(key.siren) and get_key_pressed(key.ctrl) then
		rvc.blipSiren()
	end

	-- cruise control
	if (setCruiseSpeed ~= 0) then
		wait(100)
		VEHICLE.SET_VEHICLE_FORWARD_SPEED(playerVehicle, setCruiseSpeed)
		-- turn off cruise if slowing down or speeding up
		if get_key_pressed(key.drivebackwards) or get_key_pressed(key.driveforwards) then
			setCruiseSpeed = 0
		end
	end
	-- "Ctrl+W" turn on cruise control 
	if get_key_pressed(key.cruise) and get_key_pressed(key.ctrl) then
		rvc.toggleCruise()
	end

	-- "Ctrl+X" toggle engine
	if get_key_pressed(key.engine) and get_key_pressed(key.ctrl) then
		rvc.toggleEngine()
		rvc.keyDelay()
	end
	if (isEngineOff == true) then
		VEHICLE.SET_VEHICLE_ENGINE_ON(playerVehicle, false, true)
	end
	-- turn engine on if moving forward or backward
	if get_key_pressed(key.drivebackwards) or get_key_pressed(key.driveforwards) then
		isEngineOff = false
	end

end

function booltoint(bool)
	if bool then
		return 1
	else
		return 0
	end
end

function rvc.keyDelay()
	wait(500)
end

function rvc.fixTyre()
	VEHICLE.SET_VEHICLE_TYRE_FIXED(playerVehicle, 0)
	VEHICLE.SET_VEHICLE_TYRE_FIXED(playerVehicle, 1)
	VEHICLE.SET_VEHICLE_TYRE_FIXED(playerVehicle, 2)
	VEHICLE.SET_VEHICLE_TYRE_FIXED(playerVehicle, 3)
	VEHICLE.SET_VEHICLE_TYRE_FIXED(playerVehicle, 4)
	VEHICLE.SET_VEHICLE_TYRE_FIXED(playerVehicle, 5)
	rvc.keyDelay()
end

function rvc.blipSiren()
	AUDIO.BLIP_SIREN(playerVehicle)
	rvc.keyDelay()
end

function rvc.toggleEngine()
	isEngineOff = not isEngineOff
	rvc.keyDelay()
end

function rvc.cleanVehicle()
	VEHICLE.SET_VEHICLE_DIRT_LEVEL(playerVehicle, 0)
end

function rvc.fixVehicle()
	VEHICLE.SET_VEHICLE_FIXED(playerVehicle)
end

function rvc.toggleCruise()
	setCruiseSpeed = playerVehicleSpeed
	setCruiseSpeed = math.floor(setCruiseSpeed)
	rvc.GUI.hidden = true
end

function rvc.toggleAllLights()
	isAllLightsOn = not isAllLightsOn
	VEHICLE.SET_VEHICLE_LIGHTS(playerVehicle, booltoint(isAllLightsOn))
	rvc.keyDelay()
end

function rvc.toggleLeftIndicator()
	isLeftIndicatorOn = not isLeftIndicatorOn
	VEHICLE.SET_VEHICLE_INDICATOR_LIGHTS(playerVehicle, true, isLeftIndicatorOn)
	rvc.keyDelay()
end

function rvc.toggleRightIndicator()
	isRightIndicatorOn = not isRightIndicatorOn
	VEHICLE.SET_VEHICLE_INDICATOR_LIGHTS(playerVehicle, false, isRightIndicatorOn)
	rvc.keyDelay()
end

function rvc.toggleBrakeLights()
	isBrakeLightOn = not isBrakeLightOn
	rvc.keyDelay()
end

function rvc.openDoors(doorIndex)
	VEHICLE.SET_VEHICLE_DOOR_OPEN(playerVehicle, doorIndex, true, false)
end

function rvc.closeDoors()
	VEHICLE.SET_VEHICLE_DOORS_SHUT(playerVehicle, true)
end

function rvc.openWindows()
	VEHICLE.ROLL_DOWN_WINDOWS(playerVehicle)
end

function rvc.closeWindows()
	VEHICLE.ROLL_UP_WINDOW(playerVehicle,0)
	VEHICLE.ROLL_UP_WINDOW(playerVehicle,1)
	VEHICLE.ROLL_UP_WINDOW(playerVehicle,2)
	VEHICLE.ROLL_UP_WINDOW(playerVehicle,3)
end

function rvc.leaveVehicle()
	AI.TASK_LEAVE_VEHICLE(playerPed, playerVehicle, 8)
	rvc.keyDelay()
	VEHICLE.SET_VEHICLE_ENGINE_ON(playerVehicle, true, true)
end

function rvc.leaveVehicleEngineOff()
	AI.TASK_LEAVE_VEHICLE(playerPed, playerVehicle, 8)
	rvc.keyDelay()
	VEHICLE.SET_VEHICLE_ENGINE_ON(playerVehicle, false, true)
end

function rvc.toggleInteriorLight()
	isInteriorLightOn = not isInteriorLightOn
	VEHICLE.SET_VEHICLE_INTERIORLIGHT(playerVehicle, isInteriorLightOn)
	rvc.keyDelay()
end

function rvc.toggleExtraLight()
	isExtraLightOn = not isExtraLightOn
	VEHICLE.SET_TAXI_LIGHTS(playerVehicle, isExtraLightOn)
	rvc.keyDelay()
end

function rvc.skipSong()
	AUDIO.SKIP_RADIO_FORWARD()
	rvc.keyDelay()
end


function rvc.init()
	rvc.GUI = Libs["GUI"]
	-- add all buttons to UI
    rvc.GUI.addButton("Toggle Engine",rvc.toggleEngine,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Toggle Cruise",rvc.toggleCruise,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Toggle Left Indicator",rvc.toggleLeftIndicator,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Toggle Right Indicator",rvc.toggleRightIndicator,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Toggle Brake Lights",rvc.toggleBrakeLights,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Toggle Interior Light",rvc.toggleInteriorLight,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Toggle Extra Lights",rvc.toggleExtraLight,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Toggle All Lights",rvc.toggleAllLights,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Open Front Left Door",rvc.openDoors,0,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Open Front Right Door",rvc.openDoors,1,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Open Back Left Door",rvc.openDoors,2,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Open Back Right Door",rvc.openDoors,3,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Open Hood",rvc.openDoors,4,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Open Trunk",rvc.openDoors,5,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Open Alt Trunk",rvc.openDoors,1,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Close All Doors",rvc.closeDoors,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Open Windows",rvc.openWindows,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Close Windows",rvc.closeWindows,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Blip Police Siren",rvc.blipSiren,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Radio Skip Song",rvc.skipSong,"",0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Clean Vehicle",rvc.cleanVehicle,0,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Fix Tyres",rvc.fixTyre,0,0.02,0.25,0.03,0.03)
    rvc.GUI.addButton("Fix Vehicle",rvc.fixVehicle,0,0.02,0.25,0.03,0.03)

	-- hide UI at the start
	rvc.GUI.hidden = true
end

function rvc.unload()

end

return rvc