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

local FF0 = fixed_float(0)
local ROTATED_MIN_Y_THRESHOLD = fixed_float(-0.6)
local ROTATED_MAX_Y_THRESHOLD = fixed_float(-0.4)

local TUT_PLACED_POS = Vector2(350, 415)
local TUT_PLACED_ROTATION = Vector2(1.73205, -1)
local TUT_PLACED_THRESHOLD_SQ = fixed_float(2000)

local simulating = false
local board_rotated = false
local board_reversed = 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 boardRotateFingerOffset = Vector2(0, -15)
local boardRotateFingerRadius = 85
local boardCurrentPos = boardDockPos

local rotate_finger = RotateTutorialFinger(boardRotateFingerRadius)
local tut_effect = nil

function UpdateTutorials()

	if simulating or not board_selected or board_dragging or board_rotated then
	
		rotate_finger:Stop()
		
	elseif not rotate_finger:IsPlaying() then

		rotate_finger:SetOffset(boardCurrentPos + boardRotateFingerOffset)
		rotate_finger:SetReversed(board_reversed)
		rotate_finger:Start()
	
	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)
		tut_effect:SetFacing(TUT_PLACED_ROTATION)
	end		
end

function OnStopSimulation()	
	CreateTutorialEffect()
	simulating = false
	rotate_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
		board_rotated = false
		board_reversed = false
		local facing = object:GetFacing()
		local facing_x = facing.x
		local facing_y = facing.y
		if facing.x < FF0 then
			facing_x = -facing_x
			facing_y = -facing_y
		end
		if facing_y > ROTATED_MIN_Y_THRESHOLD and facing_y < ROTATED_MAX_Y_THRESHOLD then
			board_rotated = true
		elseif facing_x < -ROTATED_MIN_Y_THRESHOLD then
			if facing_y > FF0 then
				facing_x = -facing.x
				if facing_x < ROTATED_MIN_Y_THRESHOLD or facing_x > ROTATED_MAX_Y_THRESHOLD then
					board_reversed = true
				end
			else
				board_reversed = true
			end
		end
		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_rotated = false
		board_reversed = false
		board_placed = false
		board_dragging = false
		board_selected = false
		boardCurrentPos = boardDockPos
		UpdateTutorials()
	end
end

function OnRestartLevel()
	OnObjectReturned("board")
end

function OnUpdateInModal()
	rotate_finger:SetVisible(false)
end

function OnUpdateNotPlaying()
	rotate_finger:SetVisible(true)
end

function OnUpdateGUI(dt)
	rotate_finger:OnUpdate(dt)
end