--------------------[ GUI SETUP FUNCTION ]--------------------------------------------

function convertKey(Key)
	if Key == string.char(8) then
		return "BKSPCE" 
	elseif Key == string.char(9) then
		return "TAB"
	elseif Key == string.char(13) then
		return "ENTER"
	elseif Key == string.char(17) then
		return "UP"
	elseif Key == string.char(18) then
		return "DOWN"
	elseif Key == string.char(19) then
		return "RIGHT"
	elseif Key == string.char(20) then
		return "LEFT"
	elseif Key == string.char(22) then
		return "HOME"
	elseif Key == string.char(23) then
		return "END"
	elseif Key == string.char(27) then
		return "F2"
	elseif Key == string.char(29) then
		return "F4"
	elseif Key == string.char(30) then
		return "F5"
	elseif Key == string.char(32) or Key == " " then
		return "F7"
	elseif Key == string.char(33) or Key == "!" then
		return "F8"
	elseif Key == string.char(34) or Key == '"' then
		return "F9"
	elseif Key == string.char(35) or Key == "#" then
		return "F10"
	elseif Key == string.char(37) or Key == "%" then
		return "F12"
	elseif Key == string.char(47) or Key == "/" then
		return "R-SHIFT"
	elseif Key == string.char(48) or Key == "0" then
		return "L-SHIFT"
	elseif Key == string.char(49) or Key == "1" then
		return "R-CTRL"
	elseif Key == string.char(50) or Key == "2" then
		return "L-CTRL"
	elseif Key == string.char(51) or Key == "3" then
		return "R-ALT"
	elseif Key == string.char(52) or Key == "4" then
		return "L-ALT"
	else
		return string.upper(Key)
	end
end

function createControlFrame(Key, Desc, Num)
	local C = Instance.new("Frame")
	C.BackgroundTransparency = ((Num % 2) == 1 and 0.7 or 1)
	C.BorderSizePixel = 0
	C.Name = "C"..Num
	C.Position = UDim2.new(0, 0, 0, Num * 20)
	C.Size = UDim2.new(1, 0, 0, 20)
	C.ZIndex = 10
	
	local K = Instance.new("TextLabel")
	K.BackgroundTransparency = 1
	K.Name = "Key"
	K.Size = UDim2.new(0, 45, 1, 0)
	K.ZIndex = 10
	K.Font = Enum.Font.ArialBold
	K.FontSize = Enum.FontSize.Size14
	K.Text = Key
	K.TextColor3 = Color3.new(1, 1, 1)
	K.TextScaled = (string.len(Key) > 5)
	K.TextWrapped = (string.len(Key) > 5)
	K.Parent = C
	
	local D = Instance.new("TextLabel")
	D.BackgroundTransparency = 1
	D.Name = "Desc"
	D.Position = UDim2.new(0, 50, 0, 0)
	D.Size = UDim2.new(1, -50, 1, 0)
	D.ZIndex = 10
	D.Font = Enum.Font.SourceSansBold
	D.FontSize = Enum.FontSize.Size14
	D.Text = "- "..Desc
	D.TextColor3 = Color3.new(1, 1, 1)
	D.TextXAlignment = Enum.TextXAlignment.Left
	D.Parent = C
	
	C.Parent = Controls
end

function createModes()
	numModes = 0
	for i, v in pairs(S.selectFireSettings.Modes) do
		if v then
			numModes = numModes + 1
		end
	end
	
	local currentMode = 0
	for i, v in pairs(S.selectFireSettings.Modes) do
		if v then
			local Frame = Instance.new("Frame")
			Frame.BackgroundTransparency = 1
			Frame.Name = currentMode
			Frame.Position = UDim2.new()
			Frame.Size = UDim2.new()
			Frame.Parent = fireModes
			local modeLabel = Instance.new("TextLabel")
			modeLabel.BackgroundTransparency = 1
			modeLabel.Name = "Label"
			modeLabel.Position = UDim2.new(0, -20, 0, 0)
			modeLabel.Size = UDim2.new(0, 40, 0, 20)
			modeLabel.Font = Enum.Font.SourceSansBold
			modeLabel.FontSize = Enum.FontSize.Size18
			modeLabel.Text = string.upper(i)
			modeLabel.TextColor3 = Color3.new(1, 1, 1)
			modeLabel.TextScaled = true
			modeLabel.TextStrokeTransparency = 0
			modeLabel.TextTransparency = 0.5
			modeLabel.TextWrapped = true
			modeLabel.Parent = Frame
			table.insert(Modes, string.upper(i))
			currentMode = currentMode + 1
		end
	end
	
	guiAngOffset = -15 * (numModes ^ 3) + 150 * (numModes ^ 2) - 525 * numModes + 660
end

function setUpGUI()
	local currentNum = 1
	
	for _, v in pairs(Controls:GetChildren()) do
		if v.Name ~= "Title" then
			v:Destroy()
		end
	end
	
	for _, PTable in pairs(Plugins.KeyDown) do
		createControlFrame(convertKey(PTable.Key), PTable.Description, currentNum)
		currentNum = currentNum + 1
	end
	
	if S.canChangeStance then
		local Dive = (S.dolphinDive and " / Dive" or "")
		createControlFrame(convertKey(S.Keys.lowerStance), "Lower Stance"..Dive, currentNum)
		currentNum = currentNum + 1
		
		createControlFrame(convertKey(S.Keys.raiseStance), "Raise Stance", currentNum)
		currentNum = currentNum + 1
	end
	
	if S.selectFire then
		createControlFrame(convertKey(S.Keys.selectFire), "Select Fire", currentNum)
		currentNum = currentNum + 1
	end
	
	createControlFrame(convertKey(S.Keys.Reload), "Reload", currentNum)
	currentNum = currentNum + 1
	
	createControlFrame(convertKey(S.Keys.Sprint), "Sprint", currentNum)
	currentNum = currentNum + 1
	
	if S.canADS then
		local Hold = (S.aimSettings.holdToADS and "HOLD " or "")
		if S.Keys.ADS ~= "" then
			createControlFrame(Hold..convertKey(S.Keys.ADS).." OR R-MOUSE", "Aim Down Sights", currentNum)
		else
			createControlFrame(Hold.." R-MOUSE", "Aim Down Sights", currentNum)
		end
		currentNum = currentNum + 1
	end
	
	Controls.Size = UDim2.new(1, 0, 0, currentNum * 20)
	Controls.Position = UDim2.new(0, 0, 0, -(currentNum * 20) - 80)
	
	if S.guiScope then
		scopeSteady.Text = "Hold "..convertKey(S.Keys.scopeSteady).." to Steady"
	end
	
	if mainGUI:FindFirstChild("Co") then
		mainGUI.Co:Destroy()
	end
	local Co = Instance.new("TextLabel")
	Co.BackgroundTransparency = 1
	Co.Name = "Co"
	Co.Visible = true
	Co.Position = UDim2.new(0, 0, 0, 0)
	Co.Size = UDim2.new(1, 0, 0, 20)
	Co.Font = Enum.Font.SciFi
	Co.FontSize = Enum.FontSize.Size14
	Co.Text = ("")
	Co.TextColor3 = Color3.new(1, 1, 1)
	Co.TextStrokeColor3 = Color3.new(1, 1, 1)
	Co.TextStrokeTransparency = 1
	Co.TextTransparency = 0.9
	Co.TextXAlignment = Enum.TextXAlignment.Center
	Co.Parent = mainGUI
	
	gunNameTitle.Text = Gun.Name
	
	updateClipAmmo()
	updateStoredAmmo()
	
	fireModes:ClearAllChildren()
	createModes()
	updateModeLabels(numModes - 1, 0, 90)
	
	if S.selectFire then
		modeGUI.Text = Modes[((rawFireMode - 1) % numModes) + 1]
	else
		modeGUI.Text = (
			S.gunType.Semi and "SEMI" or
			S.gunType.Auto and "AUTO" or
			S.gunType.Burst and "BURST" or
			"SAFETY"
		)
	end
end