#include "script/common.lua"

function init()
	done = false
	target = FindBody("target")
	yacht = GetBodyVehicle(target)
	local surface = FindLocation("surface")
	height = GetLocationTransform(surface).pos[2]
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)
		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)
					local text = GetTranslatedStringByKey("UI_TEXT_HEIGHT_ABOVE_SURFACE," .. math.floor(currentHeight*10)/10)
					UiText(text)
				UiPop()
			end
		end

		local h = GetVehicleHealth(yacht)
		UiPush()
			UiTranslate(UiCenter(), 25)
			UiAlign("center middle")
			UiFont("bold.ttf", 20)
			UiText("loc@UI_TEXT_YACHT_CONDITION")
			UiTranslate(-100, 10)
			progressBar(200, 20, h)
		UiPop()
	end
end

