-- << FUNCTIONS >>

function module:ToggleBar(keyCode)
	if keyCode == Enum.KeyCode.Quote then
		local cmdBar = main.gui.CmdBar
		if cmdBar.Visible then
			main:GetModule("CmdBar"):CloseCmdBar()
		else
			main:GetModule("CmdBar"):OpenCmdBar()
		end
	elseif keyCode == Enum.KeyCode.Semicolon then
		local requiredRank = main.settings.Cmdbar2
		if main.pdata.Rank > 0 or requiredRank <= 0 then
			if main.pdata.Rank < requiredRank then
				local notice = main:GetModule("cf"):FormatNotice("CommandBarLocked", "2", main:GetModule("cf"):GetRankName(requiredRank))
				main:GetModule("Notices"):Notice("Error", notice[1], notice[2])
			else
				local props = currentProps["cmdbar2"]
				if not props then
					main:GetModule("ClientCommands")["cmdbar2"].Function()
				else
					if props.mainParent.Visible then
						props.mainParent.Visible = false
					else
						props.mainParent.Visible = true
					end
				end
			end
		end
	end
end

function module:OpenCmdBar()
	local props = mainProps
	local requiredRank = main.settings.Cmdbar
	if (main.pdata.Rank > 0 or requiredRank <= 0) and not isOpen then
		isOpen = true
		if main.pdata.Rank < requiredRank then
			local notice = main:GetModule("cf"):FormatNotice("CommandBarLocked", "", main:GetModule("cf"):GetRankName(requiredRank))
			main:GetModule("Notices"):Notice("Error", notice[1], notice[2])
		else
			props.textBox:CaptureFocus()
			for _,coreName in pairs(coreToHide) do
				local originalState = main.starterGui:GetCoreGuiEnabled(coreName)
				originalCoreStates[coreName] = originalState
				main.starterGui:SetCoreGuiEnabled(Enum.CoreGuiType[coreName], false)
			end
			--main:GetModule("TopBar"):CoreGUIsChanged()
			topBar.BackgroundColor3 = Color3.fromRGB(50,50,50)
			originalTopBarTransparency = topBar.BackgroundTransparency
			originalTopBarVisibility = topBar.Visible
			topBar.BackgroundTransparency = 0
			topBar.Visible = true
			props.textBox.Text = ""
			cmdBar.Visible = true
			wait()
			props.textBox.Text = main.pdata.Prefix
			props.textBox.CursorPosition = 3
		end
	end
end

function module:CloseCmdBar()
	if not isOpen then return end
	isOpen = false
	local props = mainProps
	cmdBar.Visible = false
	for _,coreName in pairs(coreToHide) do
		local originalState = originalCoreStates[coreName]
		if originalState then
			main.starterGui:SetCoreGuiEnabled(Enum.CoreGuiType[coreName], originalState)
		end
	end
	--main:GetModule("TopBar"):CoreGUIsChanged()
	topBar.BackgroundColor3 = Color3.fromRGB(31,31,31)
	topBar.BackgroundTransparency = originalTopBarTransparency
	topBar.Visible = originalTopBarVisibility
	coroutine.wrap(function()
		for i = 1,10 do
			props.textBox:ReleaseFocus()
			wait()
		end
	end)()
end

function module:PressedArrowKey(key)
	for i, props in pairs(currentProps) do
		if props.mainParent.Visible and props.textBox:IsFocused() then
			local originalLabel = props.rframe:FindFirstChild("Label"..props.suggestionPos)
			if key == Enum.KeyCode.Down then
				props.suggestionPos = props.suggestionPos + 1
				if props.suggestionPos > props.suggestionDisplayed then
					props.suggestionPos = 1
				end
			elseif key == Enum.KeyCode.Up then
				props.suggestionPos = props.suggestionPos - 1
				if props.suggestionPos < 1 then
					props.suggestionPos = props.suggestionDisplayed
				end
			elseif key == Enum.KeyCode.Right then
				selectSuggestion(props)
			end
			local newLabel = props.rframe["Label"..props.suggestionPos]
			if newLabel ~= originalLabel then
				originalLabel.BackgroundColor3 = props.otherColor
				newLabel.BackgroundColor3 = props.highlightColor
			end
		end
	end
end



return module
