--Commands and Sounds

local function updateSettings()
	for a,b in pairs(pages.custom:GetChildren()) do
		local settingName = string.sub(b.Name, 5)
		if b:FindFirstChild("SettingValue") then
			b.SettingName.TextLabel.Text = settingName..":"
			local settingValue = main.pdata[settingName]
			b.SettingValue.TextBox.Text = tostring(settingValue)
			local soundName
			local propertyStart
			if string.sub(settingName, 1, 5) == "Error" then
				soundName = "Error"
				propertyStart = 6
			elseif string.sub(settingName, 1, 6) == "Notice" then
				soundName = "Notice"
				propertyStart = 7
			end
			if soundName then
				local sound = main.audio[soundName]
				local propertyName = string.sub(settingName, propertyStart)
				if propertyName == "SoundId" then
					settingValue = "rbxassetid://"..settingValue
				end
				sound[propertyName] = settingValue
			end
		end
	end
	updateThemeSelection()
end
for a,b in pairs(pages.custom:GetChildren()) do
	local settingName = string.sub(b.Name, 5)
	if b:FindFirstChild("SettingValue") then
		local settingDe = true
		local textBox = b.SettingValue.TextBox
		local textLabel = b.SettingValue.TextLabel
		b.SettingValue.TextBox.FocusLost:connect(function(property)
			if settingDe then
				local newValue = b.SettingValue.TextBox.Text
				settingDe = false
				textBox.Visible = false
				textLabel.Visible = true
				textLabel.Text = "Loading..."
				local returnMsg = main.signals.ChangeSetting:InvokeServer{settingName, newValue}
				if returnMsg == "Success" then
					updateSettings()
					local noticeType = "Notice"
					local settingType = string.sub(settingName, 1, 5)
					if settingType == "Error" or settingType == "Alert" then
						noticeType = settingType
					end
					if settingName == "Prefix" then
						main:GetModule("PageCommands"):CreateCommands()
					end
					local noticeMessage = "Successfully changed '"..settingName.."' to '"..newValue.."'"
					if noticeType == "Alert" then
						main:GetModule("cf"):CreateNewCommandMenu("settings", {"Settings Alert", noticeMessage}, 8, true)
					else
						main:GetModule("Notices"):Notice(noticeType, "Settings", noticeMessage)
					end
				else
					textLabel.Text = returnMsg
					wait(1)
				end
				textBox.Visible = true
				textLabel.Visible = false
				b.SettingValue.TextBox.Text = tostring(main.pdata[settingName])
				settingDe = true
			end
		end)
	end
end
updateSettings()
