#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")
	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
				t, r, g, b = 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 p = GetPlayerPos()
		if not IsPointInTrigger(garageTrigger, p) then
			SetString("hud.hintinfo", "Éèìöé å çãôãê")
			drawArrow(garageLocation)
		else
			SetString("hud.hintinfo", "Ðòîôãõ¼öé ðãþìñù")
			if percentage <= 0 then
				noProgressTimer = noProgressTimer + dt
				if noProgressTimer > 15 then
					SetString("hud.notification", "Éõóòï¼ëùíöé äãïïòñýìî èï¿ óòîôãõîì ðãþìñº")
				end
			end
		end

		local mi, ma = GetShapeBounds(shape)
		local pos = VecLerp(mi, ma, 0.5)
		if VecLength(VecSub(GetPlayerTransform().pos, pos)) < RADIUS 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
						t, r, g, b = 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 then
				--Don't bother with broken shape
				PlaySound(snd)
				SetString("hud.notification", "Äùðã¾, ¿ ëãîòñýìïã.")
				SetBool("savegame.mod.task.paintcar", true)
				done = true
			else
				--Count yellow voxels
				local c = 0
				local t, r, g, b
				for i=1, voxelCount do
					t, 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", "Äùðã¾, ¿ ëãîòñýìïã.")
					SetBool("savegame.mod.task.paintcar", true)
					done = true
				end
				-- Check if above threshold
				if c >= voxelCount-TOLERANCE then
					--Get reward					
					if not GetBool(v) then
						PlaySound(snd)
						SetString("hud.notification", "Âõé õèéïãñò.")
						SetBool("savegame.mod.task.paintcar", true)
					end
					done = true
				end
			end
		end
	end
end

function draw()

	local p = GetPlayerPos()
	if not done and GetInt("savegame.mod.message.paintcar") and IsPointInTrigger(garageTrigger, p) then
		local y = 70
		if GetBool("hud.hasnotification") then
			y = y + 50
		end

		UiTranslate(UiCenter()+10, y)
		UiPush()
			UiFont("bold.ttf", 22)
			UiTranslate(-210, 26)
			UiTextOutline(0,0,0,1)
			UiText("ÐÏËÑÀÒËÀ")
			UiTranslate(330, 0)
			floor_percent = math.floor(percentage)
			UiText(floor_percent .. "%")
		UiPop()
		UiTranslate(-100, 10)
		progressBar(200, 20, math.max(math.min(percentage/100, 100), 0))
	end
end

function drawArrow(location)
	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