-- << BANLAND >>

local banlandDe = true
function module:CreateBanland()
	coroutine.wrap(function()
	
		if not banlandDe then
			return
		else
			banlandDe = false
		end
		local totalY = 0
		if main.pdata.Rank < main.settings.ViewBanland then
			main:GetModule("cf"):ClearPage(pages.banland)
			local label = templates.banTitle:Clone()
			label.BackgroundColor3 = Color3.fromRGB(95, 95, 95)
			label.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
			label.TextLabel.Text = "Must be rank '".. main:GetModule("cf"):GetRankName(main.settings.ViewBanland).."' or higher to view the Banland"
			label.Name = "TitleLabel"
			label.Visible = true
			label.Parent = pages.banland
		else
			
			--Retrieve Info
			local banlandInfo = main.signals.RetrieveBanland:InvokeServer()
			local canModifyBans = false
			if main.pdata.Rank >= (main.commandRanks.directban or 4) then
				canModifyBans = true
			end		
			
			--Clear labels
			main:GetModule("cf"):ClearPage(pages.banland)
			
			--Setup Labels
			local sectionNames = {"Current Server", "All Servers"}
			for section, records in pairs(banlandInfo) do
				--
				if #records > 0 then
					local label = templates.banTitle:Clone()
					label.BackgroundColor3 = Color3.fromRGB(95, 95, 95)
					label.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
					label.TextLabel.Text = sectionNames[section]
					label.Name = "TitleLabel"
					totalY = totalY + templates.banTitle.AbsoluteSize.Y
					label.Visible = true
					label.Parent = pages.banland
				end
				--
				for i, record in pairs(records) do
					local userId = record.UserId
					if userId > 0 then
						totalY = totalY + templates.ban.AbsoluteSize.Y
						local settingsBan = record.SettingsBan
						local label = templates.ban:Clone()
						label.Name = "BanLabel"
						label.PlrName.Text = main:GetModule("cf"):GetName(userId)
						local banDateString
						local banTime = record.BanTime
						if tonumber(banTime) then
							banDateString = main:GetModule("cf"):GetBanDateString(os.date("*t", banTime))
						elseif settingsBan then
							banDateString = "Auto Settings Ban"
						else
							banDateString = "Infinite"
						end
						label.BanLength.Text = banDateString
						label.PlayerImage.Image = main:GetModule("cf"):GetUserImage(userId)
						label.BackgroundColor3 = main:GetModule("cf"):GetLabelBackgroundColor(i)
						local viewMore = label.ViewMore
						if canModifyBans and not settingsBan then
							viewMore.Visible = true
							viewMore.MouseButton1Down:Connect(function()
								main:GetModule("cf"):ShowBannedUser{label.PlrName.Text, userId, record.Reason, record.BannedBy}
							end)
						else
							viewMore.Visible = false
						end
						label.Visible = true
						label.Parent = pages.banland
					end
				end
			end
		
		
		end
		
		--Canvas Size
		pages.banland.CanvasSize = UDim2.new(0, 0, 0, totalY)
		
		banlandDe = true
	
	end)()
end

function module:UpdatePages()
	module:CreateServerRanks()
	module:CreateBanland()
	module:CreateRanks()
end


