#include "script/common.lua"
#include "script/toolanimation.lua"

interactive = false
interactiveAlpha = 0
interactiveTransform = Transform()
lastToolTransform = Transform()
pullBack = 0
visibleLastFrame = nil

toolAnimator = ToolAnimator()

function setBodyVisibility(body, visible)
	local s = GetBodyShapes(body)

	if visible then
		for i=1, #s do
			RemoveTag(s[i], "invisible")
		end
	else
		for i=1, #s do
			SetTag(s[i], "invisible")
		end
	end
end

function moveBodyOutOfSight(body)
	SetBodyTransform(body, Transform(Vec(0, -10, 0)))
end

function removeShadow(body)
	local s = GetBodyShapes(body)
	for i=1, #s do
		SetTag(s[i], "noshadow")
	end
end

function init()
	RegisterTool("cellphone", "CELLPHONE", "MOD/prefab/cellphoneproxy.xml", 6)
	SetBool("game.tool.cellphone.enabled", true)
	SetString("game.tool.cellphone.ammo.display","")
	local entities = Spawn("MOD/prefab/cellphone.xml", Transform(Vec(0, -10, 0)), true)
	for i=1, #entities do
		if GetEntityType(entities[i])=="body" then
			phoneBody = entities[i]
		end
		if GetEntityType(entities[i])=="screen" then
			phoneScreen = entities[i]
			SetTag(phoneScreen, "noidlecursor")
			SetTag(phoneScreen, "nocam")
		end
	end

	removeShadow(phoneBody)

	toolAnimator.maxActionPoseTime = 0.3
end

function tick(dt)
	local fovScale = GetInt("options.gfx.fov")/90.0
	local selected = GetString("game.player.tool") == "cellphone"
	if selected or interactive then

		tickToolAnimator(toolAnimator, dt)
		
		if GetBool("game.player.canusetool") and InputPressed("usetool") and interactive==false and GetPlayerScreen() ~= phoneScreen then
			SetPlayerScreen(phoneScreen)
			SetValue("interactiveAlpha", 1, "cosine", 0.35)
			interactive = true
		elseif interactive and (GetPlayerScreen() ~= phoneScreen or InputPressed("rmb")) then 
			SetPlayerScreen(0)
			SetValue("interactiveAlpha", 0, "bounce", 0.6)
			interactive = false
		end
	end
	
	if selected then
		if not interactive then
			lastToolTransform = GetBodyTransform(GetToolBody())
			--Pull back phone when moving in order to hide stuttering
			local v = GetPlayerVelocity()
			local pullBackTarget = math.min(VecLength(v)*0.3, 1.0)
			pullBack = pullBack + clamp((pullBackTarget - pullBack)*0.1, -dt*5, dt*5)
			local p = TransformToParentVec(lastToolTransform, Vec(0, -0.4, 0.4))
			lastToolTransform.pos = VecAdd(lastToolTransform.pos, VecScale(p, pullBack))
		end
	end

	if selected or interactive then
		if visibleLastFrame then
			interactiveTransform = TransformToParentTransform(GetPlayerEyeTransform(), Transform(Vec(0.0, -0.28, -0.2/fovScale), QuatEuler(-15, -2, -2)))
			local t = Transform()
			t.pos = VecLerp(lastToolTransform.pos, interactiveTransform.pos, interactiveAlpha)
			t.rot = QuatSlerp(lastToolTransform.rot, interactiveTransform.rot, interactiveAlpha)
			SetBodyTransform(phoneBody, t)
		end
		visibleLastFrame = true
	else
		moveBodyOutOfSight(phoneBody)
		visibleLastFrame = false
	end

	if selected then
		if GetBool("game.thirdperson") then
			setBodyVisibility(GetToolBody(), true)
			moveBodyOutOfSight(phoneBody)
		else
			setBodyVisibility(GetToolBody(), false)
		end
	end
end

