#include "script/wheels.lua"

function init()
	enabled = true

	tutorialDone = GetBool("savegame.mod.tutorial.complete",false)

	if tutorialDone then
		enabled = false
	end

	smsTimer = 8
	introSMSsent = false

	uiBlur = 1
	uiCloseEyes = 1

	SetValue("uiCloseEyes", 0, "linear", 1.8)
	garage1 = FindLocation("garage1")

	toolsLocation = FindLocation("tools")
	toolsLocation2 = FindLocation("tools2")
	toolsTrigger = FindTrigger("tools")
	toolsDone = false

	freyas_vehicle = FindVehicle("freyas", true)
	carDone = false


	exitLocation = FindLocation("exitlocation")
	exitTrigger = FindTrigger("exittrigger")

	arrow = LoadSprite("gfx/arrowdown.png")

	--Disable all tools
	local tools = ListKeys("game.tool")
	for i=1,#tools do
		local k = "game.tool."..tools[i]..".enabled"
		if GetBool(k) then
			SetBool(k, false)
		end
	end
	SetString("game.player.tool", "none")

	if GetBool("savegame.mod.toolsdone") then
		removeToolsAndSkipTutorial()
	end

end


function drawArrow(location)
	local arrowPos = GetLocationTransform(location).pos
	local c = GetCameraTransform().pos
	c[2] = arrowPos[2]
	local p = VecCopy(arrowPos)
	p[2] = p[2] + math.sin(GetTime()*5)*0.3+0.3 + 2
	local t = Transform(p, QuatLookAt(arrowPos, c))
	DrawSprite(arrow, t, 2, 2, .3, .3, .3, 3, false, true)
	DrawSprite(arrow, t, 2, 2, 1, 1, 1, 1, true, true)
end
function drawArrowVehicle(vehicle)
	local arrowPos = GetVehicleTransform(vehicle).pos
	local c = GetCameraTransform().pos
	c[2] = arrowPos[2]
	local p = VecCopy(arrowPos)
	p[2] = p[2] + math.sin(GetTime()*5)*0.3+0.3 + 3
	local t = Transform(p, QuatLookAt(arrowPos, c))
	DrawSprite(arrow, t, 2, 2, .3, .3, .3, 3, false, true)
	DrawSprite(arrow, t, 2, 2, 1, 1, 1, 1, true, true)
end

function tick(dt)
	if not enabled then
		return
	end

	local curinput = GetString("game.input.curdevice")
	local p = GetPlayerPos()

	local ammoBodies = FindBodies("ammo")
	local toolBodies = FindBodies("tool")
	if #ammoBodies + #toolBodies == 0 then
		toolsDone = true
		SetBool("savegame.mod.toolsdone",true)
	end
	if not toolsDone and isMessageRead("intro") then
		if not IsPointInTrigger(toolsTrigger, p) then
			SetString("hud.hintinfo", "Ïöóôãåï¿íöéõ¼ å åºõöãåòýñºí ëãï")
			drawArrow(garage1)
		else
			SetString("hud.hintinfo", "Âòë¼ðìöé ìñõöôùðéñöº")
			drawArrow(toolsLocation)
			drawArrow(toolsLocation2)
		end
	end

	local vehicle = GetPlayerVehicle()
	local health = GetVehicleHealth(freyas_vehicle)

	
	local vt = GetVehicleTransform(freyas_vehicle)
	carInWater, depth = IsPointInWater(vt.pos)

	if (health <= 0 and toolsDone) or (carInWater and toolsDone) then
		carDone = true
		if IsPointInTrigger(exitTrigger, p) then
			SetBool("savegame.mod.tutorial.complete",true)
            StartLevel("paperboy", "MOD/tillaggaryd.xml", "paperboy")			
		else
			drawArrow(exitLocation)
			SetString("hud.hintinfo", "Ðòõéöìö¼ Tilläggaryd")
		end
	end

	if vehicle == freyas_vehicle and toolsDone then
		carDone = true
		if IsPointInTrigger(exitTrigger, p) then
			SetBool("savegame.mod.tutorial.complete",true)
            StartLevel("paperboy", "MOD/tillaggaryd.xml", "paperboy")			
		else
			drawArrow(exitLocation)
			if health <= 0 then
				SetString("hud.hintinfo", "Íãþìñã õïòðãïãõ¼, ìèìöé óéþîòð èò Tilläggaryd")
			else
				SetString("hud.hintinfo", "Åûãö¼ å Tilläggaryd")
			end
		end
	end

	if toolsDone and not carDone then
		SetString("hud.hintinfo", "Éèìöé î õåòéí ðãþìñé")
		drawArrowVehicle(freyas_vehicle)
	end


end

function isMessageReceived(messageId)
	return GetInt("savegame.mod.message."..messageId) > 0
end
function isMessageRead(messageId)
	return GetInt("savegame.mod.message."..messageId..".read") > 0
end


function draw()

	local delay = 0.6

	if GetTime() < .4+delay then
		SetValue("uiBlur", 1, "linear", 0.1)		
	end
	if GetTime() > .4+delay and GetTime() < 7.0+delay then
		SetValue("uiBlur", 0, "linear", 0.1)		
	end
	if GetTime() > 0.7+delay and GetTime() < 1.0+delay then
		SetValue("uiBlur", 1, "linear", 0.1)		
	end
	if GetTime() > 1.0+delay and GetTime() < 1.1+delay then
		SetValue("uiBlur", 0, "linear", 0.1)		
	end


	UiBlur(uiBlur)

	UiColor(0,0,0,uiCloseEyes)
	UiRect(UiWidth(),UiHeight())
end

function removeToolsAndSkipTutorial()
	tools = FindBodies("ammo")
	for i=1,#tools do
		Delete(tools[i])
	end

	pTool = "spraycan"
	SetBool("savegame.tool."..pTool..".enabled", true)
	SetBool("game.tool."..pTool..".enabled", true)

	pTool = "extinguisher"
	SetBool("savegame.tool."..pTool..".enabled", true)
	SetBool("game.tool."..pTool..".enabled", true)

	pTool = "sledge"
	SetBool("savegame.tool."..pTool..".enabled", true)
	SetString("game.player.tool", pTool)
	SetBool("game.tool."..pTool..".enabled", true)

	pTool = "wire"
	SetBool("game.tool."..pTool..".enabled", true)
	SetInt("game.tool."..pTool..".ammo", 30)
	SetInt("game.tool."..pTool..".stretch", 1)

	pTool = "gun"
	SetBool("game.tool."..pTool..".enabled", true)
	SetInt("game.tool."..pTool..".ammo", 12)
	SetInt("game.tool."..pTool..".damage", 1)
	SetInt("game.tool."..pTool..".range", 5)

	pTool = "plank"
	SetBool("game.tool."..pTool..".enabled", true)
	SetInt("game.tool."..pTool..".ammo", 48)
	SetInt("game.tool."..pTool..".length", 50)
	SetInt("game.tool."..pTool..".width", 4)

	pTool = "blowtorch"
	SetBool("game.tool."..pTool..".enabled", true)
	SetInt("game.tool."..pTool..".ammo", 20)

	pTool = "steroid"
	SetBool("game.tool."..pTool..".enabled", true)
	SetInt("game.tool."..pTool..".ammo", 4)

	-- Custom tool
	RegisterTool("jackhammer", "Jackhammer", "MOD/vox/jackhammer.vox", 3)
    SetBool("game.tool.jackhammer.enabled", true)
    SetString("game.tool.jackhammer.ammo.display","")
end