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

local TUT_PLACED_POS = Vector2(512, 395)
local TUT_PLACED_THRESHOLD_SQ = fixed_float(2000)

local simulating = false
local board_placed = false
local board_dragging = false
local board_selected = false

--Finger positions
local boardDockPos = Vector2(267, 706) --adjusted for smaller toolbar scaling
local boardDragFingerOffset = GetBlueprintTutorialFingerOffset("board")
local boardCurrentPos = boardDockPos

local drag_finger = SwipeTutorialFinger(boardCurrentPos + boardDragFingerOffset, TUT_PLACED_POS + boardDragFingerOffset)
local go_finger = TapGoTutorialFinger()
local tut_effect = nil

function UpdateTutorials()

	if simulating then
	
		drag_finger:Stop()
		go_finger:Stop()
		
	elseif not board_placed then
	
		go_finger:Stop()
		if board_dragging then
			drag_finger:Stop()
		elseif not drag_finger:IsPlaying() then
			drag_finger:SetStartPosition(boardCurrentPos + boardDragFingerOffset)
			drag_finger:Start()
		else
			local startPos = boardCurrentPos + boardDragFingerOffset
			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
		
	else
	
		drag_finger:Stop()
		if not go_finger:IsPlaying() then
			go_finger:Start()
		end
		
	end
end

function OnInit()
	CreateTutorialEffect()
	UpdateTutorials()
end

function CreateTutorialEffect()
	if not tut_effect or not tut_effect:IsValid() then
		tut_effect = World_CreateEffect("tutorial/board", TUT_PLACED_POS)
	end
end

function OnStopSimulation()	
	CreateTutorialEffect()
	simulating = false
	go_finger:Stop()
	drag_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() == "board" then
		board_dragging = true
		board_selected = true
		UpdateTutorials()
	end
end

function OnObjectAdded(blueprintName)
	--This oocurs when the object is picked up from the toolbar area.
	if blueprintName == "board" then
		board_dragging = true
		board_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() == "board" then
		local distVector = TUT_PLACED_POS - object:GetPosition()
		board_placed = (distVector:LengthSqr() < TUT_PLACED_THRESHOLD_SQ)
		board_dragging = false
		board_selected = true
		boardCurrentPos = object:GetPosition()
		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 board_selected then
		board_selected = false
		UpdateTutorials()
	end
end

function OnObjectReturned(blueprintName)
	--This occurs when the object is returned to the toolbar
	if blueprintName == "board" then
		board_placed = false
		board_dragging = false
		board_selected = false
		boardCurrentPos = boardDockPos
		UpdateTutorials()
	end
end

function OnRestartLevel()
	OnObjectReturned("board")
end

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

function OnUpdateNotPlaying()
	drag_finger:SetVisible(true)
	go_finger:SetVisible(true)
end

function OnUpdateGUI(dt)
	drag_finger:OnUpdate(dt)
	go_finger:OnUpdate(dt)
end