#include "script/common.lua"

done = false
messageSent = false
timer = 0

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

    car1 = FindVehicle("car1")
    car2 = FindVehicle("car2")

    parked1 = FindVehicle("parked1")
    parked2 = FindVehicle("parked2")

    mishap1=false
    mishap2=false

	--Find shape and all paintable voxels
	shape = FindShape("paint")
	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")

    taskDone = GetBool("savegame.mod.task.parkcars")
    if taskDone then
		Delete(car1)
		Delete(car2)
		done = true
	else
		Delete(parked1)
		Delete(parked2)
	end
end

function tick(dt)
	if messageSent then
		return
	end
	if done then
		timer = timer+dt
		if timer > 7 then
			SetString("cellphone.newMessage","cell_cam_heist")
			messageSent = true
		end
	end
	if not done and GetInt("savegame.mod.message.parkcars.read") > 0 then
        if IsVehicleInTrigger(garageTrigger, car1) and IsVehicleInTrigger(garageTrigger, car2) then
            done = true
            PlaySound(snd)
            SetString("hud.notification", "Âõé õèéïãñò.")
            SetBool("savegame.mod.task.parkcars", true)
        end

        local vehicle = GetPlayerVehicle()
        local body = GetVehicleBody(vehicle)

        local t1 = GetVehicleTransform(car1)
        local t2 = GetVehicleTransform(car2)
        car1inWater, depth1 = IsPointInWater(t1.pos)
        car2inWater, depth2 = IsPointInWater(t2.pos)
        
        if vehicle == car1 or vehicle == car2 then
            if not IsVehicleInTrigger(garageTrigger, car1) and vehicle == car1 then
                drawArrow(garageLocation) 
                SetString("hud.hintinfo", "Ðôìóãôîùíöé ãåöòðòäìï¼ å Ocean Garage")
            end
            if not IsVehicleInTrigger(garageTrigger, car2) and vehicle == car2 then
                drawArrow(garageLocation)
                SetString("hud.hintinfo", "Ðôìóãôîùíöé ãåöòðòäìï¼ å Ocean Garage")
            end
		else
            --not in any vehicle
            if not IsVehicleInTrigger(garageTrigger, car1) and  not IsVehicleInTrigger(garageTrigger, car2) then
                if car1inWater then
                    SetString("hud.hintinfo", "Éèìöé î êéïöòí çòñòýñòí ðãþìñé")
                    drawArrowVehicle(car2)
                elseif car2inWater then
                    SetString("hud.hintinfo", "Éèìöé î îôãõñòí çòñòýñòí ðãþìñé")
                    drawArrowVehicle(car1)
                else
                    SetString("hud.hintinfo", "Ðòèòíèìöé î òèñòí ìë çòñòýñºû ðãþìñ")
                    drawArrowVehicle(car1)
                    drawArrowVehicle(car2)
                end                
            elseif not IsVehicleInTrigger(garageTrigger, car1) and not car1inWater then
                drawArrowVehicle(car1)
                SetString("hud.hintinfo", "Éèìöé î îôãõñòí çòñòýñòí ðãþìñé")
            elseif not IsVehicleInTrigger(garageTrigger, car2) and not car2inWater then
                drawArrowVehicle(car2)
                SetString("hud.hintinfo", "Éèìöé î êéïöòí çòñòýñòí ðãþìñé")
            end
        end

        if (car1inWater and depth1 > 1) and (car2inWater and depth2 > 1) then
            done = true
            PlaySound(snd)
            SetString("hud.notification", "Äùðã¾, ñùêñò ôãõõîãëãö¼ Ãòôèòñù òä ½öòð ðãïéñ¼îòð îãëùõé óòëêé.")
            SetBool("savegame.mod.task.parkcars", true)
            SetBool("savegame.mod.task.parkcars.water", true)
        end

        if (car1inWater and depth1 > 1) and  IsVehicleInTrigger(garageTrigger, car2) then
            done = true
            PlaySound(snd)
            SetString("hud.notification", "Äùðã¾, ñùêñò ôãõõîãëãö¼ Ãòôèòñù òä ½öòð ðãïéñ¼îòð îãëùõé óòëêé..")
            SetBool("savegame.mod.task.parkcars", true)
            SetBool("savegame.mod.task.parkcars.water", true)
        end
        
        if (car2inWater and depth2 > 1) and  IsVehicleInTrigger(garageTrigger, car1) then
            done = true
            PlaySound(snd)
            SetString("hud.notification", "Äùðã¾, ñùêñò ôãõõîãëãö¼ Ãòôèòñù òä ½öòð ðãïéñ¼îòð îãëùõé óòëêé..")
            SetBool("savegame.mod.task.parkcars", true)
            SetBool("savegame.mod.task.parkcars.water", true)
        end

        if (car1inWater and depth1 > 1) and not mishap1 and not done then
            mishap1 = true
            SetString("hud.notification", "Ôóõ...")
        end
        if (car2inWater and depth2 > 1) and not mishap2 and not done then
            mishap2 = true
            SetString("hud.notification", "Úöò ê...")
        end

	end
end


function drawArrowVehicle(vehicle)
	local arrowPos = GetVehicleTransform(vehicle).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 + 3
	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
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