#include "craneinput.lua"

local wheelJoint = 0
local hookMoveUp = 1
local hookMoveDown = 2 

function init()
	scroll = 20
	lookingDirection = Vec(0, -90, 0)
	slack = -2
	startslack = slack
	vehicle = FindVehicle()
	mainbody = FindBody("mainbody")
	hook = FindBody("hook")
	rope = FindJoint("rope")
	startloc = FindLocation("startloc")
	startpos = GetLocationTransform(startloc).pos
	holder = FindShape("cranehold")
	holderpos = GetShapeWorldTransform(holder).pos
	startLength = VecLength(VecSub(holderpos, GetBodyTransform(hook).pos))
	ropeLength = startLength
	ropeHeight = holderpos[2] - GetBodyTransform(hook).pos[2]
	maxHeight = ropeHeight
	--SetBodyDynamic(hook, false)
	SetBodyTransform(hook, Transform(startpos))
	slack = (ropeLength - startLength) / 1000
	SetRopeSlack(rope, slack)
	lookingDirection = QuatEuler(GetCameraTransform().rot)
	wheelJoint = FindJoint("wheeljoint")
end

function update(dt)
	local hookPos = GetBodyTransform(hook).pos
	ropeLength = VecLength(VecSub(holderpos, hookPos))
	ropeHeight = holderpos[2] - hookPos[2]
end

function Clamp(value, min, max)
	return math.min(math.max(value, min), max)
end

function CameraControl(tr, dt)
	scroll = Clamp(scroll - InputValue("mousewheel"), 20, 30)
	lookingDirection = VecLerp(lookingDirection, VecSub(lookingDirection, Vec(InputValue("cameray") * 100, InputValue("camerax") * 100, 0)), dt * 20)
	lookingDirection[1] = Clamp(lookingDirection[1], -90, 5)
	tr.rot = QuatEuler(lookingDirection[1], lookingDirection[2], lookingDirection[3])
	if not cameraPos then
		cameraPos = TransformToParentPoint(tr, Vec(0, 1.5, scroll))
	else
		cameraPos = VecLerp(cameraPos, TransformToParentPoint(tr, Vec(0, 1.5, scroll)), dt * 20)
	end
	SetCameraTransform(Transform(cameraPos, QuatEuler(lookingDirection[1], lookingDirection[2], 0)))
end

function tick(dt)
	--HookControl()
	if GetPlayerVehicle() == vehicle then
		local x, y, z = GetQuatEuler(GetCameraTransform().rot)
		lookingDirection = Vec(x, y, z)
		CameraControl(GetBodyTransform(mainbody), dt)
		if InputDown(input.keys.left) or InputDown(input.keys.right) then
			SetBodyDynamic(hook, true)
		elseif InputDown(input.keys.down) then
			MoveHook(dt, hookMoveDown)
		elseif InputDown(input.keys.up) and ropeHeight > maxHeight + 1 then
			MoveHook(dt, hookMoveUp)
		elseif InputReleased(input.keys.up) or InputReleased(input.keys.down) then
			SetBodyDynamic(hook, true)
		end
	end
end

function draw()
	if GetPlayerVehicle() == vehicle then
		drawHints()
	end
end

function drawHints()
	info = {}
	for key,val in ipairs(input.keys.orderrobot) do
		getBind = GetString("game.input." .. input.keys[val])
		if getBind == "" then getBind = input.keys[val] end
		local inputKey = string.upper(getBind)
		local desc = input.keys.descrobot[val]
		key = val
		info[#info + 1] = {inputKey, desc}
	end
			
	UiPush()
	UiAlign("top left")
	local w = 250
	local h = #info * 22 + 30
	UiTranslate(UiWidth() - w - 20, UiHeight() - h - 20) -- because I don't know how big the official vehicle UI will be
	UiColor(0, 0, 0, 0.5)
	UiImageBox("ui/common/box-solid-6.png", 250, h, 6, 6)
	UiTranslate(125, 32)
	UiColor(1, 1, 1)
	UiTranslate(-60, 0)
	for i = 1, #info do
		local key = info[i][1]
		local func = info[i][2]
		UiFont("bold.ttf", 22)
		UiAlign("right")
		UiText(key)
		UiTranslate(10, 0)
		UiFont("regular.ttf", 22)
		UiAlign("left")
		UiText(func)
		UiTranslate(-10, 22)
	end
	UiPop()
end

function MoveHook(dt, direction)
	SetBodyDynamic(hook, false)
	local pos = GetBodyTransform(hook).pos
	local upholderpos = GetShapeWorldTransform(holder).pos
	pos[2] = direction == hookMoveUp and pos[2] + 0.02 or pos[2] - 0.02
	SetBodyTransform(hook, Transform(VecLerp(pos, Vec(upholderpos[1], pos[2], upholderpos[3]), dt * 4)))
	SetJointMotorTarget(wheelJoint, direction == hookMoveUp and GetJointMovement(wheelJoint) - 1 or GetJointMovement(wheelJoint) + 1, 1)
	if slack < 10 or slack > startslack then 
		slack = (ropeLength - startLength) / 1000
		SetRopeSlack(rope, slack)
	end
end