--[[
if not service.UserInputService.KeyboardEnabled then
    warn("User is on mobile :: CustomChat Disabled")
    chatenabled = true
    drag.Visible = false
    service.StarterGui:SetCoreGuiEnabled('Chat',true)
end
--]]


	client.Handlers.RemoveCustomChat = function()
		local chat = gui
		if chat then
			chat:Destroy()
			client.Variables.ChatEnabled = true
			service.StarterGui:SetCoreGuiEnabled('Chat', true)
		end
	end

	client.Handlers.ChatHandler = function(plr, message, mode)
		if not message then
			return
		end
		if string.sub(message, 1, 2) == '/e' then
			return
		end
		if gui then
			local player

			if plr and type(plr) == "userdata" then
				player = plr
			else
				player = {
					Name = tostring(plr or "System"),
					TeamColor = BrickColor.White()
				}
			end

			if #message > 150 then
				message = `{string.sub(message, 1, 150)}...`
			end

			if mode then
				if mode == "Private" or mode == "System" then
					table.insert(storedChats, {
						Color = player.TeamColor or BrickColor.White(),
						Player = player.Name,
						Message = message,
						Mode = "Global",
						Private = true
					})
					table.insert(storedChats, {
						Color = player.TeamColor or BrickColor.White(),
						Player = player.Name,
						Message = message,
						Mode = "Team",
						Private = true
					})
					table.insert(storedChats, {
						Color = player.TeamColor or BrickColor.White(),
						Player = player.Name,
						Message = message,
						Mode = "Local",
						Private = true
					})
					table.insert(storedChats, {
						Color = player.TeamColor or BrickColor.White(),
						Player = player.Name,
						Message = message,
						Mode = "Admins",
						Private = true
					})
					table.insert(storedChats, {
						Color = player.TeamColor or BrickColor.White(),
						Player = player.Name,
						Message = message,
						Mode = "Cross",
						Private = true
					})
				else
					local plr = player.Name
					table.insert(storedChats, {
						Color = player.TeamColor or BrickColor.White(),
						Player = plr,
						Message = message,
						Mode = mode
					})
				end
			else
				local plr = player.Name
				table.insert(storedChats, {
					Color = player.TeamColor or BrickColor.White(),
					Player = plr,
					Message = message,
					Mode = "Global"
				})
			end

			if mode == "Local" then
				if not localTab.Visible then
					localb.Text = "Local*"
				end
			elseif mode == "Team" then
				if not teamTab.Visible then
					team.Text = "Team*"
				end
			elseif mode == "Admins" then
				if not adminsTab.Visible then
					admins.Text = "Admins*"
				end
			elseif mode == "Cross" then
				if not crossTab.Visible then
					cross.Text = "Cross*"
				end
			else
				if not globalTab.Visible then
					global.Text = "Global*"
				end
			end

			if #storedChats >= 50 then
				table.remove(storedChats, 1)
			end

			UpdateChat()

			if not nohide then
				if player and type(player) == "userdata" then
					local char = player.Character
					local head = char:FindFirstChild("Head")

					if head then
						local cont = service.LocalContainer():FindFirstChild(`{player.Name}Bubbles`)
						if not cont then
							cont = Instance.new("BillboardGui", service.LocalContainer())
							cont.Name = `{player.Name}Bubbles`
							cont.StudsOffset = Vector3.new(0, 2, 0)
							cont.SizeOffset = Vector2.new(0, 0.5)
							cont.Size = UDim2.new(0, 200, 0, 150)
						end

						cont.Adornee = head

						local clone = bubble:Clone()
						clone.TextLabel.Text = message
						clone.Parent = cont

						local xsize = clone.TextLabel.TextBounds.X + 40
						if xsize > 400 then
							xsize = 400
						end
						clone.Size = UDim2.new(0, xsize, 0, 50)


						if #cont:GetChildren() > 3 then
							cont:GetChildren()[1]:Destroy()
						end

						for i, v in pairs(cont:GetChildren()) do
							local xsize = v.TextLabel.TextBounds.X + 40
							if xsize > 400 then
								xsize = 400
							end
							v.Position = UDim2.new(0.5, -xsize / 2, 1, -(math.abs((i - 1) - #cont:GetChildren()) * 50))
						end

						local cam = workspace.CurrentCamera
						local char = player.Character
						local head = char:FindFirstChild("Head")

						Routine(function()
							repeat
								if not head then
									break
								end
								local dist = (head.Position - cam.CFrame.p).Magnitude
								if dist <= 50 then
									clone.Visible = true
								else
									clone.Visible = false
								end
								wait(0.1)
							until not clone.Parent or not clone or not head or not head.Parent or not char
						end)

						wait(10)

						if clone then
							clone:Destroy()
						end
					end
				end
			end
		end
	end

	local textbox = service.UserInputService:GetFocusedTextBox()
	if textbox then
		textbox:ReleaseFocus()
	end
end
