#include "script/common.lua"

function init()
	target = FindBody("target")
	shining =  FindLight("shining")
	target_AA, target_BB = GetBodyBounds(target)	
	startshapedcount = #QueryAabbShapes(target_AA, target_BB)
	instasis = true
	flashstart = 0
	lightintensity = math.random(1,5)
	glowing = true
	glowing_timer = GetTime()
	released = false
	hitplace = nil
end

function handleCommand(cmd)
	if instasis then 
		local words = splitString(cmd, " ")
		if #words == 5 then
			if words[1] == "explosion" then
				local strength = tonumber(words[2]) * 2
				local x = tonumber(words[3])
				local y = tonumber(words[4])
				local z = tonumber(words[5])
				if VecLength(VecSub(Vec(x,y,z), GetBodyTransform(target).pos)) < strength then
					StasisRelease()
				end
			end
		elseif #words == 8 then
			if words[1] == "shot" then
				local x = tonumber(words[3])
				local y = tonumber(words[4])
				local z = tonumber(words[5])				
				local hit, point = GetBodyClosestPoint(target, Vec(x,y,z))
				if hit and VecLength(VecSub(Vec(x,y,z), point)) < 0.08 then
					StasisRelease()
				end
			end
		end
	end
end

function tick(dt)
	local hasBreakage = GetBool("game.break")
	
	if hasBreakage then
		local breakPoint = Vec(GetFloat("game.break.x"), GetFloat("game.break.y"), GetFloat("game.break.z"))
		local dist = VecLength(VecSub(GetBodyTransform(target).pos, breakPoint))
		if dist < 0.5 then
			StasisRelease()
		end
	end
	if instasis then
		local hasBreakage = GetBool("game.break")
	
		if hasBreakage then
			local breakPoint = Vec(GetFloat("game.break.x"), GetFloat("game.break.y"), GetFloat("game.break.z"))
			local dist = VecLength(VecSub(GetBodyTransform(target).pos, breakPoint))
			if dist < 0.5 then
				if GetString("game.player.tool") == "shotgun" then
					hitplace = breakPoint
				end
				StasisRelease()
			end
		end
		
		Glowing(dt)
		
		local rx, ry, rz = GetQuatEuler(GetBodyTransform(target).rot)
		local changeangle = QuatEuler(rx + 0.1, ry + 0.5, rz)	
		SetBodyTransform(target, Transform(GetBodyTransform(target).pos, changeangle))
		
		if IsBodyVisible(target, 3.3) and InputPressed("usetool") and (GetString("game.player.tool") == "blowtorch" or GetString("game.player.tool") == "sledge") then
			StasisRelease()
		end
		
		if not IsHandleValid(target) then
			StasisRelease()
		end
	end
	if not instasis and GetTime() < flashstart and not released then
		lightintensity = lightintensity + 10
		SetLightIntensity(shining, lightintensity)
	elseif not instasis and not released and GetTime() > flashstart then
		SetLightIntensity(shining, 3)
		released = true
	end

end

function update(dt)
	if instasis then
		local allshapes = QueryAabbShapes(target_AA, target_BB)
		if #allshapes > startshapedcount then
			StasisRelease()
		end
	end
end

function StasisRelease()
	instasis = false
	if IsHandleValid(target) then
		SetBodyDynamic(target, true)
		if hitplace ~= nil then
			local dir = VecNormalize(VecSub(hitplace, GetPlayerPos()))
			ApplyBodyImpulse(target, GetBodyTransform(target).pos, VecScale(dir, 190))
			hitplace = nil
		end
	end
	flashstart = GetTime() + 0.2
	if not GetBool("level.alarm") then
		SetBool("level.alarm", true)
	end
end

function Glowing(dt)
	if glowing then
		lightintensity = lightintensity + 0.01
	else
		lightintensity = lightintensity - 0.01
	end
	SetLightIntensity(shining, lightintensity)
	if lightintensity > 5 or lightintensity < 0.5 then
		if glowing then
			glowing = false
		else
			glowing = true
		end
	end
end