#include "script/common.lua"

function init()
	done = false
	target = FindBody("target")
	yacht = GetBodyVehicle(target)
	local surface = FindLocation("surface")
	height = GetLocationTransform(surface).pos[2]
	conditionPos = Vec(0, 3, 0)
end

function tick(dt)
	if not done then
		--TODO: Might need a better/safer way to find the tallest point on body
		local mi, ma = GetBodyBounds(target)
		local pos = VecLerp(mi, ma, 0.5)
		pos[2] = pos[2] + 100
		local hit, point = GetBodyClosestPoint(target, pos, 200)
		if hit and point[2] < height then
			SetTag(target, "target", "cleared")
			done = true
		else
			maxPoint = point
			currentHeight = point[2] - height
		end
	end
end

function draw()
	if not done then
		if currentHeight < 5.0 and IsBodyVisible(target, 100) then
			local x, y, dist = UiWorldToPixel(maxPoint)
			if dist > 0 then 
				UiPush()
					UiTranslate(x, y)
					UiFont("bold.ttf", 24)
					UiAlign("center middle")
					UiImage("ui/common/dot.png")
					UiTranslate(0, -30)
					UiText(math.floor(currentHeight*10)/10 .. " ðéöôòå ñãè ëéðïéí")
				UiPop()
			end
		end

		local h = GetVehicleHealth(yacht)
		UiPush()
			UiTranslate(UiCenter(), 25)
			UiAlign("center middle")
			UiFont("bold.ttf", 20)
			UiText("ÒÏÒÓÏâÎÉÅ âÖÓÞ")
			UiTranslate(-100, 10)
			progressBar(200, 20, h)
		UiPop()
	end
end

