--Search Bar suggestions


function module:SetupTextbox(commandName, props)
	local function collectArguments(suggestions, currentBarArg, totalArgs, argName)
		if string.sub(string.lower(argName), 1, #currentBarArg) == currentBarArg then
			table.insert(suggestions, {Name = argName, ArgPos = totalArgs})
			if #suggestions == props.maxSuggestions then
				return true
			end
		end
	end
	local specificArg = props.specificArg
	currentProps[commandName] = props
	createSuggestionLabels(props)
	setupEnterPress(props)
	local originalPosition
	props.textBox.Changed:connect(function(property)
		if property == "Text" then
			local totalArgs = -1
			local barCommandName
			local args = {}
			if specificArg then
				totalArgs = 1
				table.insert(args, props.textBox.Text)
			else
				props.textBox.Text:gsub('([^'..main.pdata.SplitKey..']+)',function(c) totalArgs = totalArgs + 1 if totalArgs == 0 then barCommandName = c elseif totalArgs > 0 then table.insert(args, c) end end);
				if totalArgs > 0 and not props.currentBarCommand then
					local firstChar = string.sub(barCommandName,1,1)
					if firstChar == main.settings.UniversalPrefix or firstChar == main.pdata.Prefix then
						barCommandName = string.sub(barCommandName,2)
					end
					barCommandName = string.lower(barCommandName)
					--
					for _, command in pairs(main.commandInfo) do
						local cName = string.lower(command.Name)
						if cName == barCommandName then
							props.currentBarCommand = command
						elseif command.Aliases then
							for i,v in pairs(command.Aliases) do
								if string.lower(v) == barCommandName then
									props.currentBarCommand = command
								end
							end
						end
					end
					--
				elseif totalArgs <= 0 then
					props.currentBarCommand = nil
				end
			end
			local suggestions = {}
			props.suggestionPos = 1
			if #props.textBox.Text > 1 or (specificArg and #props.textBox.Text > 0) then
				if totalArgs == 0 then
					local barInput = string.lower(string.sub(props.textBox.Text, 2))
					local barPrefix = string.lower(string.sub(props.textBox.Text, 1,1))
					for _, command in pairs(main.commandInfo) do
						local cName = string.lower(command.Name)
						local cPrefix = command.Prefixes[1]
						if string.sub(cName, 1, #barInput) == barInput and cPrefix == barPrefix then
							table.insert(suggestions, {Name = command.Name, Args = command.Args, Prefixes = command.Prefixes, ArgPos = 0})
							if #suggestions == props.maxSuggestions then
								break
							end
						end
						local unName = command.UnFunction
						if unName then
							if string.sub(string.lower(unName), 1, #barInput) == barInput and cPrefix == barPrefix then
								table.insert(suggestions, {Name = unName, Args = {}, Prefixes = command.Prefixes, ArgPos = 0})
								if #suggestions == props.maxSuggestions then
									break
								end
							end
						end
					end
				elseif props.currentBarCommand or specificArg then
					local cArg
					if specificArg then
						cArg = specificArg
					else
						cArg = props.currentBarCommand.Args[totalArgs]
					end
					local newSuggestion = {}
					local currentBarArg = string.lower(args[totalArgs])
					local endChar = string.sub(props.textBox.Text,-1)
					if endChar ~= " " and endChar ~= main.pdata.SplitKey then
						if cArg == "player" or cArg == "playerarg" then
							for i,v in pairs(main.qualifiers) do
								if collectArguments(suggestions, currentBarArg, totalArgs, v) then
									break
								end
							end
							if #suggestions < props.maxSuggestions then
								for i,plr in pairs(main.players:GetChildren()) do
									if collectArguments(suggestions, currentBarArg, totalArgs, plr.Name) then
										break
									end
								end
							end
						elseif cArg == "colour" or cArg == "color" or cArg == "color3" then
							for i,v in pairs(main.colors) do
								if collectArguments(suggestions, currentBarArg, totalArgs, v) then
									break
								end
							end
						elseif cArg == "material" then
							for i,v in pairs(main.materials) do
								if collectArguments(suggestions, currentBarArg, totalArgs, v) then
									break
								end
							end
						elseif cArg == "rank" then
							for _, rankDetails in pairs(main.settings.Ranks) do
								if collectArguments(suggestions, currentBarArg, totalArgs, rankDetails[2]) then
									break
								end
							end
						elseif cArg == "team" or cArg == "teamcolor" then
							for _,team in pairs(main.teams:GetChildren()) do
								if collectArguments(suggestions, currentBarArg, totalArgs, team.Name) then
									break
								end
							end
						elseif cArg == "morph" then
							for morphName,_ in pairs(main.morphNames) do
								if collectArguments(suggestions, currentBarArg, totalArgs, morphName) then
									break
								end
							end
						elseif cArg == "tools" or cArg == "gears" or cArg == "tool" or cArg == "gear" then
							for toolName,_ in pairs(main.toolNames) do
								if collectArguments(suggestions, currentBarArg, totalArgs, toolName) then
									break
								end
							end
						end
					end
				end
			end
			
			props.suggestionDisplayed = #suggestions
			if props.currentBarCommand and #props.currentBarCommand.Args == totalArgs and props.suggestionDisplayed > 0 then
				forceExecute = true
			else
				forceExecute = false
			end
			
			for i = 1, props.maxSuggestions do
				local suggestion = suggestions[i]
				local label = props.rframe["Label"..i]
				if suggestion then
					local command = false
					if suggestion.Args then
						command = true
					end
					local UP = false
					if command and suggestion.Prefixes[1] == main.settings.UniversalPrefix then
						UP = true
					end
					local newDetail = {}
					if command then
						for i,v in pairs(suggestion.Args) do
							if not UP or v ~= "player" or v == "playerarg" then
								table.insert(newDetail, "<"..v..">")
							end
						end
					end
					label.TextLabel.Text = suggestion.Name.." "..table.concat(newDetail, " ")
					if i == props.suggestionPos then
						label.BackgroundColor3 = props.highlightColor
					else
						label.BackgroundColor3 = props.otherColor
					end
					props.suggestionLabels["Label"..i] = suggestion
					--
					if props.mainParent.Name ~= "CmdBar" then
						local sizeY = props.rframe.LabelTemplate.AbsoluteSize.Y
						local totalSizeY = sizeY * i
						local finalY = props.rframe.AbsolutePosition.Y + totalSizeY
						local limitY = main.gui.Notices.AbsoluteSize.Y
						if finalY+5 > limitY then
							if not originalPosition then
								originalPosition = props.mainParent.Position
							end
							props.mainParent.Position = props.mainParent.Position - UDim2.new(0,0,0,sizeY)
						end
					end
					--
					label.Visible = true
				else
					label.Visible = false
				end
			end
			--
			if originalPosition and #suggestions < 1 then
				props.mainParent.Position = originalPosition
				originalPosition = nil
			end
			if #suggestions < 1 and props.mainParent.Name ~= "CmdBar" then
				props.mainParent.ClipsDescendants = true
			else
				props.mainParent.ClipsDescendants = false
			end
			--
	    end
	end)
end
module:SetupTextbox("cmdbar1", mainProps)