--	first reset scrollingFrame to zero position

	scrollingFrame.CanvasPosition = Vector2.new(0, 0)
	local width = scrollingFrame.AbsoluteSize.X
	local height = scrollingFrame.AbsoluteSize.Y

	local isBottom = false
	local instance
	local threshold = 2
	while not isBottom do
		wait(0.1)
		--if find the element then return
		instance = absPath:getFirstInstance()
		if instance then return instance end
		--scroll
		local oldPosition = scrollingFrame.CanvasPosition
		scrollingFrame.CanvasPosition = scrollingFrame.CanvasPosition +
			Vector2.new(math.min(width, 300), math.min(height, 300))
		--wait for content to refresh
		local delta = scrollingFrame.CanvasPosition - oldPosition
		isBottom = delta.Magnitude < threshold
		--if it is the bottom, then return not found
	end
	return nil
end

function Element:centralizeWithInfiniteScrolling()
	local instances, lastSeenIndex = self.path:getInstances()
	if #instances > 0 then self:centralizeInstance() end

	local lastSeenPath = self.path:copy()
	while #lastSeenPath.data > lastSeenIndex do
		table.remove(lastSeenPath.data)
	end

	local lastSeenInstance = lastSeenPath:getFirstInstance()
	local lastScrollingFrame = nil
	while true do
		if lastSeenInstance.ClassName == "ScrollingFrame" then
			lastScrollingFrame = lastSeenInstance
			break
		end
		lastSeenInstance = lastSeenInstance.Parent
		if lastSeenInstance == game then break end
	end
	if lastScrollingFrame == nil then return end
	if self:_scrollToFindInstance(lastScrollingFrame, self.path) == nil then return end
	self:_centralizeInScrollingFrame(self:getRbxInstance())
end

function Element:setPluginWindow()
	local window = self.rbxInstance:FindFirstAncestorOfClass("DockWidgetPluginGui")
	VirtualInput.setCurrentWindow(window)
end

function Element:click(repeatCount)
	self:centralize()
	self:setPluginWindow()

	repeatCount = repeatCount or 1
	VirtualInput.Mouse.multiClick(self:getAnchor(), repeatCount)
end

function Element:rightClick()
	self:centralize()
	self:setPluginWindow()

	VirtualInput.Mouse.rightClick(self:getAnchor())
end

function Element:mouseWheel(num)
	self:centralize()
	VirtualInput.Mouse.mouseWheel(self:getAnchor(), num)
end

function Element:mouseDrag(xOffset, yOffset, duration)
	self:centralize()
	local posTo = self:getAnchor() + Vector2.new(xOffset, yOffset)
	VirtualInput.Mouse.mouseDrag(self:getAnchor(), posTo, duration, true)
end

function Element:mouseDragTo(posTo, duration)
	self:centralize()
	VirtualInput.Mouse.mouseDrag(self:getAnchor(), posTo, duration, true)
end

function Element:sendKey(key)
	self:setPluginWindow()
	VirtualInput.Keyboard.hitKey(key)
end

function Element:sendText(str)
	self:click()
	wait(0)
	VirtualInput.Text.sendText(str)
end

function Element:tap()
	self:centralize()
	VirtualInput.Touch.tap(self:getAnchor())
end

function Element:touchScroll(xOffset, yOffset, duration, multitouchId)
	self:centralize()
	VirtualInput.Touch.touchScroll(self:getAnchor(), xOffset, yOffset, duration, true, multitouchId)
end

return Element
