#include "script/common.lua"

RADIUS = 10.0						-- Activation radius, should include all of shape with margin
TOLERANCE = 500						-- Non-yellow voxels tolerated

done = false
messageSent = false
timer = 0

percentage = 0

noProgressTimer = 0

function init()
	
	arrow = LoadSprite("gfx/arrowdown.png")
	garageLocation = FindLocation("garageLocation")
	garageTrigger = FindTrigger("garageTrigger")

	--Find shape and all paintable voxels
	shape = FindShape("paint")
	local finishedcar = FindShape("painted")

	local sx, sy, sz = GetShapeSize(shape)
	ix = {}
	iy = {}
	iz = {}
	voxelCount = 0
	for z=0, sz-1 do
		for y=0, sy-1 do
			for x=0, sx-1 do
				local t, _, _, _ = GetShapeMaterialAtIndex(shape, x, y, z)
				if t~="" then
					voxelCount = voxelCount + 1
					ix[voxelCount] = x
					iy[voxelCount] = y
					iz[voxelCount] = z
				end
			end
		end
	end
	snd = LoadSound("valuable.ogg")

	if GetBool("savegame.mod.task.paintcar") then
		Delete(shape)
		done = true
	else
		Delete(finishedcar)		
	end
end

function tick(dt)
	if messageSent then
		return
	end
	if done and not InputDown("usetool") then
		timer = timer+dt
		if timer > 3 then
			SetString("cellphone.newMessage","cell_museum_heist")
			messageSent = true
		end
	end
	if not done and GetInt("savegame.mod.message.paintcar.read") > 0 then
		local playerPos = GetPlayerPos()
		local mi, ma = GetShapeBounds(shape)
		local carPos = VecLerp(mi, ma, 0.5)
		local isNearCar = VecLength(VecSub(playerPos, carPos)) < RADIUS

		if not isNearCar and not IsPointInTrigger(garageTrigger, playerPos) then
			SetString("hud.hintinfo", GetTranslatedStringByKey("UI_HINT_GO_TO_GARAGE"))
			drawArrow(garageLocation)
		else
			SetString("hud.hintinfo", GetTranslatedStringByKey("UI_HUD_HINT_PAINT_THE"))
			if percentage <= 0 then
				noProgressTimer = noProgressTimer + dt
				if noProgressTimer > 15 then
					SetString("hud.notification", GetTranslatedStringByKey("UI_HUD_NOTE_USE_THE"))
				end
			end
		end

		if isNearCar then
			voxelCount = 0
			local sx, sy, sz = GetShapeSize(shape)
			ix = {}
			iy = {}
			iz = {}
			voxelCount = 0
			for z=0, sz-1 do
				for y=0, sy-1 do
					for x=0, sx-1 do
						local t, _, _, _ = GetShapeMaterialAtIndex(shape, x, y, z)
						if t~="" then
							voxelCount = voxelCount + 1
							ix[voxelCount] = x
							iy[voxelCount] = y
							iz[voxelCount] = z
						end
					end
				end
			end

			local vc = GetShapeVoxelCount(shape)

			if vc < voxelCount-TOLERANCE or IsPointInWater(carPos) then
				--Don't bother with broken shape
				PlaySound(snd)
				SetString("hud.notification", GetTranslatedStringByKey("UI_HUD_NOTE_I_GUESS"))
				SetBool("savegame.mod.task.paintcar", true)
				done = true
			else
				--Count yellow voxels
				local c = 0
				for i=1, voxelCount do
					local _, r, g, b = GetShapeMaterialAtIndex(shape, ix[i], iy[i], iz[i])
					if r > 0.6 and g > 0.6 and b < 0.4 then
						c = c + 1
					end
				end
				percentage = ((c-342)/(voxelCount-TOLERANCE-342))*100
				if percentage < 0 then percentage = 0 end
				if percentage >= 100 then 
					percentage = 100
					PlaySound(snd)
					SetString("hud.notification", GetTranslatedStringByKey("UI_HUD_NOTE_I_GUESS"))
					SetBool("savegame.mod.task.paintcar", true)
					done = true
				end
				-- Check if above threshold
				if c >= voxelCount-TOLERANCE then
					--Get reward					
					PlaySound(snd)
					SetString("hud.notification", GetTranslatedStringByKey("UI_HUD_NOTE_ALL_DONE"))
					SetBool("savegame.mod.task.paintcar", true)
					done = true
				end
			end
		end
	end
end

function draw()
	if not done and GetInt("savegame.mod.message.paintcar.read") > 0 then
		local playerPos = GetPlayerPos()
		local mi, ma = GetShapeBounds(shape)
		local carPos = VecLerp(mi, ma, 0.5)
		local isNearCar = VecLength(VecSub(playerPos, carPos)) < RADIUS
		if isNearCar or IsPointInTrigger(garageTrigger, playerPos) then
			local y = 70
			if GetBool("hud.hasnotification") then
				y = y + 50
			end
			local progressBarWidth = 200
			local progressBarHeight = 20
			local margin = 10
			UiTranslate(UiCenter() - progressBarWidth/2, y + progressBarHeight/2)
			progressBar(progressBarWidth, progressBarHeight, math.max(math.min(percentage/100, 100), 0))
			UiPush()
				UiFont("bold.ttf", 22)
				UiTextOutline(0,0,0,1)
				local text = GetTranslatedStringByKey("UI_TEXT_PAINTJOB")
				local w = UiGetTextSize(text)
				UiTranslate(-(w + margin), 16)
				UiText(text)
				UiTranslate(w + margin + progressBarWidth + margin, 0)
				local floor_percent = math.floor(percentage)
				UiText(floor_percent .. "%")
			UiPop()
		end
	end
end

function drawArrow(location)
	if GetBool("game.map.enabled") then
		return
	end
	local arrowPos = GetLocationTransform(location).pos
	local c = GetCameraTransform().pos
	c[2] = arrowPos[2]
	local p = VecCopy(arrowPos)
	p[2] = p[2] + math.sin(GetTime()*5)*0.3+0.3 + 2
	local t = Transform(p, QuatLookAt(arrowPos, c))
	DrawSprite(arrow, t, 2, 2, .3, .3, .3, 3, false, true)
	DrawSprite(arrow, t, 2, 2, 1, 1, 1, 1, true, true)
end