-- Remove with FFlagUserClickToMoveFollowPathRefactor


local function HandleDirectMoveTo(hitPt, myHumanoid)
	if myHumanoid then
		if myHumanoid.Sit then
			myHumanoid.Jump = true
		end
		myHumanoid:MoveTo(hitPt)
	end

	local endWaypoint = ClickToMoveDisplay.CreateEndWaypoint(hitPt)
	ExistingIndicator = endWaypoint
	coroutine.wrap(function()
		myHumanoid.MoveToFinished:wait()
		endWaypoint:Destroy()
	end)()
end

local function HandleMoveTo(thisPather, hitPt, hitChar, character, overrideShowPath)
	if FFlagUserClickToMoveFollowPathRefactor then
		ExistingPather = thisPather
	end

	thisPather:Start(overrideShowPath)

	if not FFlagUserClickToMoveFollowPathRefactor then
		CleanupPath()
	end

	if not FFlagUserClickToMoveFollowPathRefactor then
		ExistingPather = thisPather
	end

	PathCompleteListener = thisPather.Finished.Event:Connect(function()
		if FFlagUserClickToMoveFollowPathRefactor then
			CleanupPath()
		end
		if hitChar then
			local currentWeapon = GetEquippedTool(character)
			if currentWeapon then
				currentWeapon:Activate()
			end
		end
	end)
	PathFailedListener = thisPather.PathFailed.Event:Connect(function()
		CleanupPath()
		if overrideShowPath == nil or overrideShowPath then
			local shouldPlayFailureAnim = PlayFailureAnimation and not (ExistingPather and ExistingPather:IsActive())
			if shouldPlayFailureAnim then
				ClickToMoveDisplay.PlayFailureAnimation()
			end
			ClickToMoveDisplay.DisplayFailureWaypoint(hitPt)
		end
	end)
end

local function ShowPathFailedFeedback(hitPt)
	if ExistingPather and ExistingPather:IsActive() then
		ExistingPather:Cancel()
	end
	if PlayFailureAnimation then
		ClickToMoveDisplay.PlayFailureAnimation()
	end
	ClickToMoveDisplay.DisplayFailureWaypoint(hitPt)
end

function OnTap(tapPositions, goToPoint, wasTouchTap)
	-- Good to remember if this is the latest tap event
	local camera = Workspace.CurrentCamera
	local character = Player.Character

	if not CheckAlive() then return end

	-- This is a path tap position
	if #tapPositions == 1 or goToPoint then
		if camera then
			local unitRay = camera:ScreenPointToRay(tapPositions[1].x, tapPositions[1].y)
			local ray = Ray.new(unitRay.Origin, unitRay.Direction*1000)

			local myHumanoid = findPlayerHumanoid(Player)
			local hitPart, hitPt, hitNormal = Utility.Raycast(ray, true, getIgnoreList())

			local hitChar, hitHumanoid = Utility.FindCharacterAncestor(hitPart)
			if wasTouchTap and hitHumanoid and StarterGui:GetCore("AvatarContextMenuEnabled") then
				local clickedPlayer = Players:GetPlayerFromCharacter(hitHumanoid.Parent)
				if clickedPlayer then
					CleanupPath()
					return
				end
			end
			if goToPoint then
				hitPt = goToPoint
				hitChar = nil
			end
			if FFlagUserClickToMoveFollowPathRefactor then
				if hitPt and character then
					-- Clean up current path
					CleanupPath()
					local thisPather = Pather(hitPt, hitNormal)
					if thisPather:IsValidPath() then
						HandleMoveTo(thisPather, hitPt, hitChar, character)
					else
						-- Clean up
						thisPather:Cleanup()
						-- Feedback here for when we don't have a good path
						ShowPathFailedFeedback(hitPt)
					end
				end
			else
				if UseDirectPath and hitPt and character then
					HandleDirectMoveTo(hitPt, myHumanoid)
				elseif hitPt and character then
					local thisPather = Pather(hitPt, hitNormal)
					if thisPather:IsValidPath() then
						HandleMoveTo(thisPather, hitPt, hitChar, character)
					else
						if hitPt then
							-- Feedback here for when we don't have a good path
							ShowPathFailedFeedback(hitPt)
						end
					end
				end
			end
		end
	elseif #tapPositions >= 2 then
		if camera then
			-- Do shoot
			local currentWeapon = GetEquippedTool(character)
			if currentWeapon then
				currentWeapon:Activate()
			end
		end
	end
end

local function DisconnectEvent(event)
	if event then
		event:Disconnect()
	end
end