LoadFile("DATA:levels/utility/tutorial_finger.lua")

local TUT_PLACED_POS = Vector2(568, 282)
local TUT_TIME = 10

local simulating = false
local whale_placed = false
local whale_dragging = false
local whale_selected = false
local timer = 0
local timer_max = TUT_TIME

--Finger positions
local whaleDockPos = Vector2(267, 714) --adjusted for smaller toolbar scaling
local whaleDragFingerOffset = GetBlueprintTutorialFingerOffset("whale")
local whaleCurrentPos = whaleDockPos

local drag_finger = SwipeTutorialFinger(whaleCurrentPos + whaleDragFingerOffset, TUT_PLACED_POS + whaleDragFingerOffset)
local stop_finger = TapGoTutorialFinger()

function UpdateTutorials()

	if whale_placed or timer < timer_max then
	
		drag_finger:Stop()
		stop_finger:Stop()
		
	elseif simulating then
	
		drag_finger:Stop()
		if not stop_finger:IsPlaying() then
			stop_finger:Start()
		end
		
	else
	
		if whale_dragging then
			drag_finger:Stop()
		elseif not drag_finger:IsPlaying() then
			drag_finger:SetStartPosition(whaleCurrentPos + whaleDragFingerOffset)
			drag_finger:Start()
		else
			local startPos = whaleCurrentPos + whaleDragFingerOffset
			local prevStartPos = drag_finger:GetStartPosition()
			if startPos.x ~= prevStartPos.x or startPos.y ~= prevStartPos.y then
				drag_finger:SetStartPosition(startPos)
				drag_finger:Start()
			end
		end
		stop_finger:Stop()
		
	end
		
end

function OnInit()
	UpdateTutorials()
end

function OnStopSimulation()	
	simulating = false
	if not whale_placed and timer < timer_max then
		timer = 0
		if timer_max == TUT_TIME then
			timer_max = timer_max / 2
		else
			timer_max = 0
		end
	end
	drag_finger:Stop()
	stop_finger:Stop()
	UpdateTutorials()
end

function OnStartSimulation()
	simulating = true
	UpdateTutorials()
end

function OnObjectPickedUp(object_id)
	--This occurs when the object is picked up from the game screen.
	local object = World_GetObjectByID(object_id)
	if object and object:GetBlueprintName() == "whale" then
		whale_dragging = true
		whale_selected = true
		UpdateTutorials()
	end
end

function OnObjectAdded(blueprintName)
	--This oocurs when the object is picked up from the toolbar area.
	if blueprintName == "whale" then
		whale_dragging = true
		whale_selected = true
		UpdateTutorials()
	end
end

function OnObjectAction(object_id)
	--This occurs when the object is placed on the game screen.
	local object = World_GetObjectByID(object_id)
	if object and object:GetBlueprintName() == "whale" then
		whale_placed = true
		whale_dragging = false
		whale_selected = true
		whaleCurrentPos = object:GetPosition()
		timer = 0
		timer_max = TUT_TIME
		UpdateTutorials()
	end
end

function OnCreateRadial(object_id)
	--This occurs when the radial menu is shown
	OnObjectAction(object_id)
end

function OnHideRadial()
	--This occurs when the radial menu is hidden
	if whale_selected then
		whale_selected = false
		UpdateTutorials()
	end
end

function OnObjectReturned(blueprintName)
	--This occurs when the object is returned to the toolbar
	if blueprintName == "whale" then
		whale_placed = false
		whale_dragging = false
		whale_selected = false
		whaleCurrentPos = whaleDockPos
		UpdateTutorials()
	end
end

function OnRestartLevel()
	OnObjectReturned("whale")
end

function OnUpdateInModal()
	drag_finger:SetVisible(false)
	stop_finger:SetVisible(false)
end

function OnUpdateNotPlaying()
	drag_finger:SetVisible(true)
end

function OnUpdatePlaying()
	stop_finger:SetVisible(true)
end

function OnUpdateGUI(dt)
	drag_finger:OnUpdate(dt)
	stop_finger:OnUpdate(dt)
	if simulating and not whale_placed and timer < timer_max then
		timer = timer + dt
		if timer >= timer_max then
			UpdateTutorials()
		end
	end
end