ON = 1
OFF = 0

ROTATE_SPEED = 18
SWAP_INTERVAL = 8
FLASH_FREQ = 5

function init()
	targetVehicle = FindVehicle("emergency")
	rotateLights = FindShapes("rotate")
	flashLights_A = FindShapes("flash_A")
	flashLights_B = FindShapes("flash_B")
	
	local judge = math.random(0, 100)
	if judge < 50 then
		toggled = false
	else
		toggled = true
	end
	
	count = 0
	timer = 0
	
	uiCaption = {'Lightbar Commands'}
	uiHotkeys = {{'G', 'Turn On/Off'}}
end

function draw()
	nowDriven = GetPlayerVehicle()
	if nowDriven == targetVehicle then
		local kw = 0
		local vw = 0
		for i=1, #uiHotkeys do
			UiFont("bold.ttf", 22)
			local w0 = UiGetTextSize(uiHotkeys[i][1])
			UiFont("regular.ttf", 22)
			local w1 = UiGetTextSize(uiHotkeys[i][2])
			kw = math.max(kw,w0)
			vw = math.max(vw,w1)
		end
	
		local uiwidth = kw + vw + 120
		local uiheight = #uiHotkeys*26 + 55
		if uiwidth <= 270 then uiwidth = 270 end
		
		UiAlign("left")
		UiTranslate(UiWidth()-uiwidth-20, 20)
		UiColor(0,0,0,0.4)
		UiImageBox("ui/common/box-solid-6.png", uiwidth, uiheight, 6, 6)
		UiTranslate(uiwidth*0.5, 24)
		UiAlign("center middle")
		UiColor(1, 1, 1)
		UiFont("bold.ttf", 26)
		UiText(uiCaption[1])
		UiTranslate(-45-(uiwidth-270)*0.25, 36)
		for i=1, #uiHotkeys do
			UiFont("bold.ttf", 22)
			UiAlign("right")
			UiText(uiHotkeys[i][1])
			UiTranslate(20, 0)
			UiFont("regular.ttf", 22)
			UiAlign("left")
			UiText(uiHotkeys[i][2])
			UiTranslate(-20, 26)
		end
	end
end

function toggleLightbar()
	if toggled then
		
		--rotating lights
		for i=1, #rotateLights do
			SetShapeEmissiveScale(rotateLights[i], ON)
			local rotateJoints = GetShapeJoints(rotateLights[i])
			for j=1, #rotateJoints do
				SetJointMotor(rotateJoints[j], ROTATE_SPEED)
			end
		end
		
		--flashing lights
		timer = timer + 1
		freq = timer % FLASH_FREQ
		
		if count % SWAP_INTERVAL < (SWAP_INTERVAL / 2) then	--to count the times lights have flashed, if reached threshold, swap side
			if freq == 0 then	--flash frequency control
				for i=1, #flashLights_A do
					SetShapeEmissiveScale(flashLights_A[i], ON)
				end
				for i=1, #flashLights_B do
					SetShapeEmissiveScale(flashLights_B[i], OFF)
				end
				count = count + 1
			elseif freq == 3 then
				for i=1, #flashLights_A do
					SetShapeEmissiveScale(flashLights_A[i], OFF)
				end
				for i=1, #flashLights_B do
					SetShapeEmissiveScale(flashLights_B[i], OFF)
				end
			end
		else
			if freq == 0 then
				for i=1, #flashLights_A do
					SetShapeEmissiveScale(flashLights_A[i], OFF)
				end
				for i=1, #flashLights_B do
					SetShapeEmissiveScale(flashLights_B[i], ON)
				end
				count = count + 1
			elseif freq == 3 then
				for i=1, #flashLights_A do
					SetShapeEmissiveScale(flashLights_A[i], OFF)
				end
				for i=1, #flashLights_B do
					SetShapeEmissiveScale(flashLights_B[i], OFF)
				end
			end
		end
	else
		lightbarInit()
	end
		
end

function lightbarInit()
	for i=1, #rotateLights do
		SetShapeEmissiveScale(rotateLights[i], OFF)
		local rotateJoints = GetShapeJoints(rotateLights[i])
		for j=1, #rotateJoints do
			SetJointMotor(rotateJoints[j], 0)
		end
	end
	for i=1, #flashLights_A do
		SetShapeEmissiveScale(flashLights_A[i], OFF)
	end
	for i=1, #flashLights_B do
		SetShapeEmissiveScale(flashLights_B[i], OFF)
	end
end

function tick(dt)
	local nowDriven = GetPlayerVehicle()
	if nowDriven == targetVehicle then
		if InputPressed('g') then
			if toggled == false then
				toggled = true
			else
				toggled = false
			end
		end
	end
	toggleLightbar()
end