--------------------[ 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 Controls = Gui_Clone:WaitForChild("HUD"):WaitForChild("Controls")
	
	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)
	
	local K = Instance.new("TextLabel")
	K.BackgroundTransparency = 1
	K.Name = "Key"
	K.Size = UDim2.new(0, 45, 1, 0)
	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.Font = Enum.Font.ArialBold
	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 SetUpGui()
	local HUD = Gui_Clone:WaitForChild("HUD")
	local Scope = Gui_Clone:WaitForChild("Scope")
	local Grenades = HUD:WaitForChild("Grenades")
	local Controls = HUD:WaitForChild("Controls")
	local CurrentNum = 1
	
	if S.CanChangeStance then
		local Dive = (S.DolphinDive and " / Dive" or "")
		CreateControlFrame(ConvertKey(S.LowerStanceKey), "Lower Stance"..Dive, CurrentNum)
		CurrentNum = CurrentNum + 1
		
		CreateControlFrame(ConvertKey(S.RaiseStanceKey), "Raise Stance", CurrentNum)
		CurrentNum = CurrentNum + 1
	end
	
	CreateControlFrame(ConvertKey(S.ReloadKey), "Reload", CurrentNum)
	CurrentNum = CurrentNum + 1
	
	if S.CanKnife then
		CreateControlFrame(ConvertKey(S.KnifeKey), "Knife", CurrentNum)
		CurrentNum = CurrentNum + 1
	end
	
	if S.Throwables then
		CreateControlFrame(ConvertKey(S.LethalGrenadeKey), "Throw Lethal", CurrentNum)
		CurrentNum = CurrentNum + 1
		
		CreateControlFrame(ConvertKey(S.TacticalGrenadeKey), "Throw Tactical", CurrentNum)
		CurrentNum = CurrentNum + 1
	else
		Grenades.Visible = false
		HUD.Position = UDim2.new(1, -200, 1, -100)
		HUD.Size = UDim2.new(0, 175, 0, 50)
	end
	
	CreateControlFrame(ConvertKey(S.SprintKey), "Sprint", CurrentNum)
	CurrentNum = CurrentNum + 1
	
	if S.ADSKey ~= "" then
		local Hold = (S.HoldMouseOrKeyToADS and "HOLD " or "")
		CreateControlFrame(Hold..ConvertKey(S.ADSKey).." OR R-MOUSE", "Aim Down Sights", CurrentNum)
		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
		Scope:WaitForChild("Img").Image = S.GuiId
		Scope:WaitForChild("Steady").Text = "Hold "..ConvertKey(S.ScopeSteadyKey).." to Steady"
	end
	
	if HUD:FindFirstChild("Co") then
		HUD.Co:Destroy()
	end
	local Co = Instance.new("TextLabel")
	Co.BackgroundTransparency = 1
	Co.Name = "Co"
	Co.Position = UDim2.new(0, 0, 1, 5)
	Co.Size = UDim2.new(1, 0, 0, 20)
	Co.Font = Enum.Font.ArialBold
	Co.FontSize = Enum.FontSize.Size14
	Co.Text = ("noisuFobruT yb detpircs tiK nuG"):reverse()
	Co.TextColor3 = Color3.new(1, 1, 0)
	Co.TextStrokeTransparency = 0
	Co.TextXAlignment = Enum.TextXAlignment.Right
	Co.Parent = HUD
	
	HUD:WaitForChild("Grenades"):WaitForChild("Lethals"):WaitForChild("Icon").Image = LethalIcons[S.LethalGrenadeType]
	HUD:WaitForChild("Grenades"):WaitForChild("Tacticals"):WaitForChild("Icon").Image = TacticalIcons[S.TacticalGrenadeType]
end
