#include "common.lua"

pMessage = GetStringParam("message", "loc@PMESSAGE_HACKING")
pDuration = GetFloatParam("duration", 2.0)
pSound = GetStringParam("sound", "transmission.ogg")

JOINTED_STATIC = 1
JOINTED_DYNAMIC = 2
FIXED_STATIC = 3
FIXED_DYNAMIC = 4

function init()
	body = FindBody("target")
	hackpanel = FindShape("hack")
	light = FindLight("light")

	local joints = GetShapeJoints(hackpanel)
	joint = joints[1]

	if joint then
		local s = GetJointOtherShape(joint, hackpanel)
		if IsBodyDynamic(GetShapeBody(s)) then
			bodyType = JOINTED_DYNAMIC
			initialMass = GetBodyMass(GetShapeBody(s))
		else
			bodyType = JOINTED_STATIC
		end
	else
		if IsBodyDynamic(GetShapeBody(hackpanel)) then
			bodyType = FIXED_DYNAMIC
			initialMass = GetBodyMass(GetShapeBody(hackpanel))
		else
			bodyType = FIXED_STATIC
		end
	end

	SetTag(body, "interact", "loc@HACK_TARGET")
	progress = 0
	snd = LoadLoop(pSound)

	tampered = false
	hacked = false
	dispatched = false
end


function tick(dt)
	if not hacked then
		if GetPlayerInteractBody() == body and InputDown("interact") then
			progress = progress + dt
			local t = clamp(progress / pDuration, 0, 1)
			if t == 1 then
				SetTag(body, "target", "cleared")
				RemoveTag(body, "interact")
				hacked = true
			else
				SetBool("level.hacking", true)
				PlayLoop(snd, GetBodyTransform(body).pos)
			end
		else
			progress = 0
		end
	end

	if not tampered then
		if bodyType == FIXED_STATIC then
			if IsBodyDynamic(GetShapeBody(hackpanel)) then
				tampered = true
			end
		elseif bodyType == FIXED_DYNAMIC then
			local currentMass = GetBodyMass(GetShapeBody(hackpanel))
			if currentMass < 0.5 * initialMass then
				tampered = true
			end
		elseif bodyType == JOINTED_STATIC then
			if IsJointBroken(joint) then
				tampered = true
			else
				local s = GetJointOtherShape(joint, hackpanel)
				if IsBodyDynamic(GetShapeBody(s)) then
					tampered = true
				end
			end
		elseif bodyType == JOINTED_DYNAMIC then
			if IsJointBroken(joint) then
				tampered = true
			else
				local s = GetJointOtherShape(joint, hackpanel)
				local currentMass = GetBodyMass(GetShapeBody(s))
				if currentMass < 0.5 * initialMass then
					tampered = true
				end
			end
		end
	end

	if not dispatched and (tampered or hacked) then
		SetBool("level.dispatch", true)
		dispatched = true
	end

	local period = 0.8
	if tampered then
		period = 0.4
		SetLightColor(light, 1, .1, .1)
	end
	if hacked then
		SetShapeEmissiveScale(hackpanel, 0)
	else
		if math.fmod(GetTime(), period) < 0.1 then
			SetShapeEmissiveScale(hackpanel, 1)
		else
			SetShapeEmissiveScale(hackpanel, 0)
		end
	end
end


function draw()
	if not hacked and progress > 0 then
		UiPush()
			UiTranslate(UiCenter()-100, UiHeight()-220)
			progressBar(200, 20, progress/pDuration)
			UiTranslate(100, -10)
			UiAlign("center")
			UiFont("bold.ttf", 24)
			UiText(pMessage)
		UiPop()
	end
end


