function mergeProps(D,n)if not D or not n then return end for D,b in pairs(D)do if n[D]~=nil then n[D]=b end end end function methods.CreateGuiObjects(n,D)local b pcall(function()b=Chat:InvokeChatCallback(Enum.ChatCallbackType.OnCreatingChatWindow,nil)end)mergeProps(b,ChatSettings)local M=Instance.new("\070\114\097\109\101")M.BackgroundTransparency=1 M.Active=ChatSettings.WindowDraggable M.Parent=D M.AutoLocalize=false local m=Instance.new("\070\114\097\109\101")m.Selectable=false m.Name="\067\104\097\116\066\097\114\080\097\114\101\110\116\070\114\097\109\101"m.BackgroundTransparency=1 m.Parent=M local i=Instance.new("\070\114\097\109\101")i.Selectable=false i.Name="\067\104\097\110\110\101\108\115\066\097\114\080\097\114\101\110\116\070\114\097\109\101"i.BackgroundTransparency=1 i.Position=UDim2.new(0,0,0,0)i.Parent=M local s=Instance.new("\070\114\097\109\101")s.Selectable=false s.Name="\067\104\097\116\067\104\097\110\110\101\108\080\097\114\101\110\116\070\114\097\109\101"s.BackgroundTransparency=1 s.BackgroundColor3=ChatSettings.BackGroundColor s.BackgroundTransparency=.6 s.BorderSizePixel=0 s.Parent=M local p=Instance.new("\073\109\097\103\101\066\117\116\116\111\110")p.Selectable=false p.Image=""p.BackgroundTransparency=.6 p.BorderSizePixel=0 p.Visible=false p.BackgroundColor3=ChatSettings.BackGroundColor p.Active=true if bubbleChatOnly()then p.Position=UDim2.new(1,-p.AbsoluteSize.X,0,0)else p.Position=UDim2.new(1,-p.AbsoluteSize.X,1,-p.AbsoluteSize.Y)end p.Parent=M local S=Instance.new("\073\109\097\103\101\076\097\098\101\108")S.Selectable=false S.Size=UDim2.new(.8,0,.8,0)S.Position=UDim2.new(.2,0,.2,0)S.BackgroundTransparency=1 S.Image="\114\098\120\097\115\115\101\116\105\100\058\047\047\050\054\049\056\056\048\055\052\051"S.Parent=p local function t()local D=M while D and not D:IsA("\083\099\114\101\101\110\071\117\105")do D=D.Parent end return D end local q=DEVICE_DESKTOP local C=t()if C.AbsoluteSize.X<=PHONE_SCREEN_WIDTH then q=DEVICE_PHONE elseif C.AbsoluteSize.X<=TABLET_SCREEN_WIDTH then q=DEVICE_TABLET end local w=false local function K()if w then return end w=true if not M:IsDescendantOf(PlayerGui)then return end local D=t()local n=ChatSettings.MinimumWindowSize local b=ChatSettings.MaximumWindowSize local s=i.AbsoluteSize.Y+m.AbsoluteSize.Y local p=n.X.Scale*D.AbsoluteSize.X+n.X.Offset local S=math.max(n.Y.Scale*D.AbsoluteSize.Y+n.Y.Offset,s)local q=b.X.Scale*D.AbsoluteSize.X+b.X.Offset local C=b.Y.Scale*D.AbsoluteSize.Y+b.Y.Offset local K=M.AbsoluteSize.X local k=M.AbsoluteSize.Y if K<p then local D=UDim2.new(0,p-K,0,0)M.Size=M.Size+D elseif K>q then local D=UDim2.new(0,q-K,0,0)M.Size=M.Size+D end if k<S then local D=UDim2.new(0,0,0,S-k)M.Size=M.Size+D elseif k>C then local D=UDim2.new(0,0,0,C-k)M.Size=M.Size+D end local W=M.AbsoluteSize.X/D.AbsoluteSize.X local N=M.AbsoluteSize.Y/D.AbsoluteSize.Y M.Size=UDim2.new(W,0,N,0)w=false end M.Changed:connect(function(D)if D=="\065\098\115\111\108\117\116\101\083\105\122\101"then K()end end)p.DragBegin:connect(function(D)M.Draggable=false end)local function k(D)if ChatSettings.WindowDraggable==false and ChatSettings.WindowResizable==false then return end local n=(D-M.AbsolutePosition)+p.AbsoluteSize M.Size=UDim2.new(0,n.X,0,n.Y)if bubbleChatOnly()then p.Position=UDim2.new(1,-p.AbsoluteSize.X,0,0)else p.Position=UDim2.new(1,-p.AbsoluteSize.X,1,-p.AbsoluteSize.Y)end end p.DragStopped:connect(function(D,n)M.Draggable=ChatSettings.WindowDraggable end)local W=false p.Changed:connect(function(D)if D=="\065\098\115\111\108\117\116\101\080\111\115\105\116\105\111\110"and not M.Draggable then if W then return end W=true k(p.AbsolutePosition)W=false end end)local function N(D)if q==DEVICE_PHONE then D=D or ChatSettings.ChatChannelsTabTextSizePhone else D=D or ChatSettings.ChatChannelsTabTextSize end local n=D local b=math.max(32,n+8)+2 return b end local function V(D)if q==DEVICE_PHONE then D=D or ChatSettings.ChatBarTextSizePhone else D=D or ChatSettings.ChatBarTextSize end local n=D local b=(n+14)+10 return b end if bubbleChatOnly()then m.Position=UDim2.new(0,0,0,0)i.Visible=false i.Active=false s.Visible=false s.Active=false local D=0 local n=0 local b=t()if q==DEVICE_PHONE then D=ChatSettings.DefaultWindowSizePhone.X.Scale n=ChatSettings.DefaultWindowSizePhone.X.Offset elseif q==DEVICE_TABLET then D=ChatSettings.DefaultWindowSizeTablet.X.Scale n=ChatSettings.DefaultWindowSizeTablet.X.Offset else D=ChatSettings.DefaultWindowSizeDesktop.X.Scale n=ChatSettings.DefaultWindowSizeDesktop.X.Offset end local p=V()M.Size=UDim2.new(D,n,0,p)M.Position=ChatSettings.DefaultWindowPosition else local D=t()if q==DEVICE_PHONE then M.Size=ChatSettings.DefaultWindowSizePhone elseif q==DEVICE_TABLET then M.Size=ChatSettings.DefaultWindowSizeTablet else M.Size=ChatSettings.DefaultWindowSizeDesktop end M.Position=ChatSettings.DefaultWindowPosition end if q==DEVICE_PHONE then ChatSettings.ChatWindowTextSize=ChatSettings.ChatWindowTextSizePhone ChatSettings.ChatChannelsTabTextSize=ChatSettings.ChatChannelsTabTextSizePhone ChatSettings.ChatBarTextSize=ChatSettings.ChatBarTextSizePhone end local function Q(D)M.Active=D M.Draggable=D end local function L(D)p.Visible=D p.Draggable=D local n=m.Size.Y.Offset if D then m.Size=UDim2.new(1,-n-2,0,n)if not bubbleChatOnly()then m.Position=UDim2.new(0,0,1,-n)end else m.Size=UDim2.new(1,0,0,n)if not bubbleChatOnly()then m.Position=UDim2.new(0,0,1,-n)end end end local function F()local D=N()local n=V()if ChatSettings.ShowChannelsBar then s.Size=UDim2.new(1,0,1,-(((D+n)+2)+2))s.Position=UDim2.new(0,0,0,D+2)else s.Size=UDim2.new(1,0,1,-((n+2)+2))s.Position=UDim2.new(0,0,0,2)end end local function u(D)local n=N(D)i.Size=UDim2.new(1,0,0,n)F()end local function X(D)local n=V(D)m.Size=UDim2.new(1,0,0,n)if not bubbleChatOnly()then m.Position=UDim2.new(0,0,1,-n)end p.Size=UDim2.new(0,n,0,n)p.Position=UDim2.new(1,-n,1,-n)F()L(ChatSettings.WindowResizable)end local function l(D)i.Visible=D F()end u(ChatSettings.ChatChannelsTabTextSize)X(ChatSettings.ChatBarTextSize)Q(ChatSettings.WindowDraggable)L(ChatSettings.WindowResizable)l(ChatSettings.ShowChannelsBar)ChatSettings.SettingsChanged:connect(function(D,n)if D=="\087\105\110\100\111\119\068\114\097\103\103\097\098\108\101"then Q(n)elseif D=="\087\105\110\100\111\119\082\101\115\105\122\097\098\108\101"then L(n)elseif D=="\067\104\097\116\067\104\097\110\110\101\108\115\084\097\098\084\101\120\116\083\105\122\101"then u(n)elseif D=="\067\104\097\116\066\097\114\084\101\120\116\083\105\122\101"then X(n)elseif D=="\083\104\111\119\067\104\097\110\110\101\108\115\066\097\114"then l(n)end end)n.GuiObject=M n.GuiObjects.BaseFrame=M n.GuiObjects.ChatBarParentFrame=m n.GuiObjects.ChannelsBarParentFrame=i n.GuiObjects.ChatChannelParentFrame=s n.GuiObjects.ChatResizerFrame=p n.GuiObjects.ResizeIcon=S n:AnimGuiObjects()end function methods.GetChatBar(D)return D.ChatBar end function methods.RegisterChatBar(n,D)n.ChatBar=D n.ChatBar:CreateGuiObjects(n.GuiObjects.ChatBarParentFrame)end function methods.RegisterChannelsBar(n,D)n.ChannelsBar=D n.ChannelsBar:CreateGuiObjects(n.GuiObjects.ChannelsBarParentFrame)end function methods.RegisterMessageLogDisplay(n,D)n.MessageLogDisplay=D n.MessageLogDisplay.GuiObject.Parent=n.GuiObjects.ChatChannelParentFrame end function methods.AddChannel(n,D)if n:GetChannel(D)then error("\067\104\097\110\110\101\108 \'"..(D.."\' \097\108\114\101\097\100\121 \101\120\105\115\116\115\033"))return end local b=moduleChatChannel.new(D,n.MessageLogDisplay)n.Channels[D:lower()]=b b:SetActive(false)local M=n.ChannelsBar:AddChannelTab(D)M.NameTag.MouseButton1Click:connect(function()n:SwitchCurrentChannel(D)end)b:RegisterChannelTab(M)return b end function methods.GetFirstChannel(D)for D,n in pairs(D.Channels)do return n end return nil end function methods.RemoveChannel(n,D)if not n:GetChannel(D)then error("\067\104\097\110\110\101\108 \'"..(D.."\' \100\111\101\115 \110\111\116 \101\120\105\115\116\033"))end local b=D:lower()local M=false if n.Channels[b]==n:GetCurrentChannel()then M=true n:SwitchCurrentChannel(nil)end n.Channels[b]:Destroy()n.Channels[b]=nil n.ChannelsBar:RemoveChannelTab(D)if M then local D=n:GetChannel(ChatSettings.GeneralChannelName)~=nil local M=b==ChatSettings.GeneralChannelName:lower()local m=nil if D and not M then m=ChatSettings.GeneralChannelName else local D=n:GetFirstChannel()m=D and D.Name or nil end n:SwitchCurrentChannel(m)end if not ChatSettings.ShowChannelsBar then if n.ChatBar.TargetChannel==D then n.ChatBar:SetChannelTarget(ChatSettings.GeneralChannelName)end end end function methods.GetChannel(n,D)return D and n.Channels[D:lower()]or nil end function methods.GetTargetMessageChannel(D)if not ChatSettings.ShowChannelsBar then return D.ChatBar.TargetChannel else local n=D:GetCurrentChannel()return n and n.Name end end function methods.GetCurrentChannel(D)return D.CurrentChannel end function methods.SwitchCurrentChannel(n,D)if not ChatSettings.ShowChannelsBar then local b=n:GetChannel(D)if b then n.ChatBar:SetChannelTarget(b.Name)end D=ChatSettings.GeneralChannelName end local b=n:GetCurrentChannel()local M=n:GetChannel(D)if M==nil then error(string.format("\067\104\097\110\110\101\108 \'\037\115\' \100\111\101\115 \110\111\116 \101\120\105\115\116\046",D))end if M~=b then if b then b:SetActive(false)local D=n.ChannelsBar:GetChannelTab(b.Name)D:SetActive(false)end if M then M:SetActive(true)local D=n.ChannelsBar:GetChannelTab(M.Name)D:SetActive(true)end n.CurrentChannel=M end end function methods.UpdateFrameVisibility(D)D.GuiObject.Visible=D.Visible and D.CoreGuiEnabled end function methods.GetVisible(D)return D.Visible end function methods.SetVisible(n,D)n.Visible=D n:UpdateFrameVisibility()end function methods.GetCoreGuiEnabled(D)return D.CoreGuiEnabled end function methods.SetCoreGuiEnabled(n,D)n.CoreGuiEnabled=D n:UpdateFrameVisibility()end function methods.EnableResizable(D)D.GuiObjects.ChatResizerFrame.Active=true end function methods.DisableResizable(D)D.GuiObjects.ChatResizerFrame.Active=false end function methods.FadeOutBackground(n,D)n.ChannelsBar:FadeOutBackground(D)n.MessageLogDisplay:FadeOutBackground(D)n.ChatBar:FadeOutBackground(D)n.AnimParams.Background_TargetTransparency=1 n.AnimParams.Background_NormalizedExptValue=CurveUtil:NormalizedDefaultExptValueInSeconds(D)end function methods.FadeInBackground(n,D)n.ChannelsBar:FadeInBackground(D)n.MessageLogDisplay:FadeInBackground(D)n.ChatBar:FadeInBackground(D)n.AnimParams.Background_TargetTransparency=.6 n.AnimParams.Background_NormalizedExptValue=CurveUtil:NormalizedDefaultExptValueInSeconds(D)end function methods.FadeOutText(n,D)n.MessageLogDisplay:FadeOutText(D)n.ChannelsBar:FadeOutText(D)end function methods.FadeInText(n,D)n.MessageLogDisplay:FadeInText(D)n.ChannelsBar:FadeInText(D)end function methods.AnimGuiObjects(D)D.GuiObjects.ChatChannelParentFrame.BackgroundTransparency=D.AnimParams.Background_CurrentTransparency D.GuiObjects.ChatResizerFrame.BackgroundTransparency=D.AnimParams.Background_CurrentTransparency D.GuiObjects.ResizeIcon.ImageTransparency=D.AnimParams.Background_CurrentTransparency end function methods.InitializeAnimParams(D)D.AnimParams.Background_TargetTransparency=.6 D.AnimParams.Background_CurrentTransparency=.6 D.AnimParams.Background_NormalizedExptValue=CurveUtil:NormalizedDefaultExptValueInSeconds(0)end function methods.Update(n,D)n.ChatBar:Update(D)n.ChannelsBar:Update(D)n.MessageLogDisplay:Update(D)n.AnimParams.Background_CurrentTransparency=CurveUtil:Expt(n.AnimParams.Background_CurrentTransparency,n.AnimParams.Background_TargetTransparency,n.AnimParams.Background_NormalizedExptValue,D)n:AnimGuiObjects()end

function mergeProps(source, target)
	if not source or not target then return end
	for prop, value in pairs(source) do
		if target[prop] ~= nil then
			target[prop] = value
		end
	end
end

function methods:CreateGuiObjects(targetParent)
	local userDefinedChatWindowStyle 
	pcall(function()
		userDefinedChatWindowStyle= Chat:InvokeChatCallback(Enum.ChatCallbackType.OnCreatingChatWindow, nil)
	end)

	-- merge the userdefined settings with the ChatSettings
	mergeProps(userDefinedChatWindowStyle, ChatSettings)

	local BaseFrame = Instance.new("Frame")
	BaseFrame.BackgroundTransparency = 1
	BaseFrame.Active = ChatSettings.WindowDraggable
	BaseFrame.Parent = targetParent
    BaseFrame.AutoLocalize = false

	local ChatBarParentFrame = Instance.new("Frame")
	ChatBarParentFrame.Selectable = false
	ChatBarParentFrame.Name = "ChatBarParentFrame"
	ChatBarParentFrame.BackgroundTransparency = 1
	ChatBarParentFrame.Parent = BaseFrame

	local ChannelsBarParentFrame = Instance.new("Frame")
	ChannelsBarParentFrame.Selectable = false
	ChannelsBarParentFrame.Name = "ChannelsBarParentFrame"
	ChannelsBarParentFrame.BackgroundTransparency = 1
	ChannelsBarParentFrame.Position = UDim2.new(0, 0, 0, 0)
	ChannelsBarParentFrame.Parent = BaseFrame

	local ChatChannelParentFrame = Instance.new("Frame")
	ChatChannelParentFrame.Selectable = false
	ChatChannelParentFrame.Name = "ChatChannelParentFrame"
	ChatChannelParentFrame.BackgroundTransparency = 1
	ChatChannelParentFrame.BackgroundColor3 = ChatSettings.BackGroundColor
	ChatChannelParentFrame.BackgroundTransparency = 0.6
	ChatChannelParentFrame.BorderSizePixel = 0
	ChatChannelParentFrame.Parent = BaseFrame

	local ChatResizerFrame = Instance.new("ImageButton")
	ChatResizerFrame.Selectable = false
	ChatResizerFrame.Image = ""
	ChatResizerFrame.BackgroundTransparency = 0.6
	ChatResizerFrame.BorderSizePixel = 0
	ChatResizerFrame.Visible = false
	ChatResizerFrame.BackgroundColor3 = ChatSettings.BackGroundColor
	ChatResizerFrame.Active = true
	if bubbleChatOnly() then
		ChatResizerFrame.Position = UDim2.new(1, -ChatResizerFrame.AbsoluteSize.X, 0, 0)
	else
		ChatResizerFrame.Position = UDim2.new(1, -ChatResizerFrame.AbsoluteSize.X, 1, -ChatResizerFrame.AbsoluteSize.Y)
	end
	ChatResizerFrame.Parent = BaseFrame

	local ResizeIcon = Instance.new("ImageLabel")
	ResizeIcon.Selectable = false
	ResizeIcon.Size = UDim2.new(0.8, 0, 0.8, 0)
	ResizeIcon.Position = UDim2.new(0.2, 0, 0.2, 0)
	ResizeIcon.BackgroundTransparency = 1
	ResizeIcon.Image = "rbxassetid://261880743"
	ResizeIcon.Parent = ChatResizerFrame

	local function GetScreenGuiParent()
		--// Travel up parent list until you find the ScreenGui that the chat window is parented to
		local screenGuiParent = BaseFrame
		while (screenGuiParent and not screenGuiParent:IsA("ScreenGui")) do
			screenGuiParent = screenGuiParent.Parent
		end

		return screenGuiParent
	end


	local deviceType = DEVICE_DESKTOP

	local screenGuiParent = GetScreenGuiParent()
	if (screenGuiParent.AbsoluteSize.X <= PHONE_SCREEN_WIDTH) then
		deviceType = DEVICE_PHONE

	elseif (screenGuiParent.AbsoluteSize.X <= TABLET_SCREEN_WIDTH) then
		deviceType = DEVICE_TABLET

	end

	local checkSizeLock = false
	local function doCheckSizeBounds()
		if (checkSizeLock) then return end
		checkSizeLock = true

		if (not BaseFrame:IsDescendantOf(PlayerGui)) then return end

		local screenGuiParent = GetScreenGuiParent()

		local minWinSize = ChatSettings.MinimumWindowSize
		local maxWinSize = ChatSettings.MaximumWindowSize

		local forceMinY = ChannelsBarParentFrame.AbsoluteSize.Y + ChatBarParentFrame.AbsoluteSize.Y

		local minSizePixelX = (minWinSize.X.Scale * screenGuiParent.AbsoluteSize.X) + minWinSize.X.Offset
		local minSizePixelY = math.max((minWinSize.Y.Scale * screenGuiParent.AbsoluteSize.Y) + minWinSize.Y.Offset, forceMinY)

		local maxSizePixelX = (maxWinSize.X.Scale * screenGuiParent.AbsoluteSize.X) + maxWinSize.X.Offset
		local maxSizePixelY = (maxWinSize.Y.Scale * screenGuiParent.AbsoluteSize.Y) + maxWinSize.Y.Offset

		local absSizeX = BaseFrame.AbsoluteSize.X
		local absSizeY = BaseFrame.AbsoluteSize.Y

		if (absSizeX < minSizePixelX) then
			local offset = UDim2.new(0, minSizePixelX - absSizeX, 0, 0)
			BaseFrame.Size = BaseFrame.Size + offset

		elseif (absSizeX > maxSizePixelX) then
			local offset = UDim2.new(0, maxSizePixelX - absSizeX, 0, 0)
			BaseFrame.Size = BaseFrame.Size + offset

		end

		if (absSizeY < minSizePixelY) then
			local offset = UDim2.new(0, 0, 0, minSizePixelY - absSizeY)
			BaseFrame.Size = BaseFrame.Size + offset

		elseif (absSizeY > maxSizePixelY) then
			local offset = UDim2.new(0, 0, 0, maxSizePixelY - absSizeY)
			BaseFrame.Size = BaseFrame.Size + offset

		end

		local xScale = BaseFrame.AbsoluteSize.X / screenGuiParent.AbsoluteSize.X
		local yScale = BaseFrame.AbsoluteSize.Y / screenGuiParent.AbsoluteSize.Y
		
		BaseFrame.Size = UDim2.new(xScale, 0, yScale, 0)

		checkSizeLock = false
	end


	BaseFrame.Changed:connect(function(prop)
		if (prop == "AbsoluteSize") then
			doCheckSizeBounds()
		end
	end)

	ChatResizerFrame.DragBegin:connect(function(startUdim)
		BaseFrame.Draggable = false
	end)

	local function UpdatePositionFromDrag(atPos)
		if ChatSettings.WindowDraggable == false and ChatSettings.WindowResizable == false then
			return
		end
		local newSize = atPos - BaseFrame.AbsolutePosition + ChatResizerFrame.AbsoluteSize
		BaseFrame.Size = UDim2.new(0, newSize.X, 0, newSize.Y)
		if bubbleChatOnly() then
			ChatResizerFrame.Position = UDim2.new(1, -ChatResizerFrame.AbsoluteSize.X, 0, 0)
		else
			ChatResizerFrame.Position = UDim2.new(1, -ChatResizerFrame.AbsoluteSize.X, 1, -ChatResizerFrame.AbsoluteSize.Y)
		end
	end

	ChatResizerFrame.DragStopped:connect(function(endX, endY)
		BaseFrame.Draggable = ChatSettings.WindowDraggable
		--UpdatePositionFromDrag(Vector2.new(endX, endY))
	end)

	local resizeLock = false
	ChatResizerFrame.Changed:connect(function(prop)
		if (prop == "AbsolutePosition" and not BaseFrame.Draggable) then
			if (resizeLock) then return end
			resizeLock = true

			UpdatePositionFromDrag(ChatResizerFrame.AbsolutePosition)

			resizeLock = false
		end
	end)

	local function CalculateChannelsBarPixelSize(textSize)
		if (deviceType == DEVICE_PHONE) then
			textSize = textSize or ChatSettings.ChatChannelsTabTextSizePhone
		else
			textSize = textSize or ChatSettings.ChatChannelsTabTextSize
		end

		local channelsBarTextYSize = textSize
		local chatChannelYSize = math.max(32, channelsBarTextYSize + 8) + 2

		return chatChannelYSize
	end

	local function CalculateChatBarPixelSize(textSize)
		if (deviceType == DEVICE_PHONE) then
			textSize = textSize or ChatSettings.ChatBarTextSizePhone
		else
			textSize = textSize or ChatSettings.ChatBarTextSize
		end

		local chatBarTextSizeY = textSize
		local chatBarYSize = chatBarTextSizeY + (7 * 2) + (5 * 2)

		return chatBarYSize
	end

	if bubbleChatOnly() then
		ChatBarParentFrame.Position = UDim2.new(0, 0, 0, 0)
		ChannelsBarParentFrame.Visible = false
		ChannelsBarParentFrame.Active = false
		ChatChannelParentFrame.Visible = false
		ChatChannelParentFrame.Active = false

		local useXScale = 0
		local useXOffset = 0

		local screenGuiParent = GetScreenGuiParent()

		if (deviceType == DEVICE_PHONE) then
			useXScale = ChatSettings.DefaultWindowSizePhone.X.Scale
			useXOffset = ChatSettings.DefaultWindowSizePhone.X.Offset

		elseif (deviceType == DEVICE_TABLET) then
			useXScale = ChatSettings.DefaultWindowSizeTablet.X.Scale
			useXOffset = ChatSettings.DefaultWindowSizeTablet.X.Offset

		else
			useXScale = ChatSettings.DefaultWindowSizeDesktop.X.Scale
			useXOffset = ChatSettings.DefaultWindowSizeDesktop.X.Offset

		end

		local chatBarYSize = CalculateChatBarPixelSize()

		BaseFrame.Size = UDim2.new(useXScale, useXOffset, 0, chatBarYSize)
		BaseFrame.Position = ChatSettings.DefaultWindowPosition

	else

		local screenGuiParent = GetScreenGuiParent()

		if (deviceType == DEVICE_PHONE) then
			BaseFrame.Size = ChatSettings.DefaultWindowSizePhone

		elseif (deviceType == DEVICE_TABLET) then
			BaseFrame.Size = ChatSettings.DefaultWindowSizeTablet

		else
			BaseFrame.Size = ChatSettings.DefaultWindowSizeDesktop

		end

		BaseFrame.Position = ChatSettings.DefaultWindowPosition

	end

	if (deviceType == DEVICE_PHONE) then
		ChatSettings.ChatWindowTextSize = ChatSettings.ChatWindowTextSizePhone
		ChatSettings.ChatChannelsTabTextSize = ChatSettings.ChatChannelsTabTextSizePhone
		ChatSettings.ChatBarTextSize = ChatSettings.ChatBarTextSizePhone
	end

	local function UpdateDraggable(enabled)
		BaseFrame.Active = enabled
		BaseFrame.Draggable = enabled
	end

	local function UpdateResizable(enabled)
		ChatResizerFrame.Visible = enabled
		ChatResizerFrame.Draggable = enabled

		local frameSizeY = ChatBarParentFrame.Size.Y.Offset

		if (enabled) then
			ChatBarParentFrame.Size = UDim2.new(1, -frameSizeY - 2, 0, frameSizeY)
			if not bubbleChatOnly() then
				ChatBarParentFrame.Position = UDim2.new(0, 0, 1, -frameSizeY)
			end
		else
			ChatBarParentFrame.Size = UDim2.new(1, 0, 0, frameSizeY)
			if not bubbleChatOnly() then
				ChatBarParentFrame.Position = UDim2.new(0, 0, 1, -frameSizeY)
			end
		end
	end

	local function UpdateChatChannelParentFrameSize()
		local channelsBarSize = CalculateChannelsBarPixelSize()
		local chatBarSize = CalculateChatBarPixelSize()

		if (ChatSettings.ShowChannelsBar) then
			ChatChannelParentFrame.Size = UDim2.new(1, 0, 1, -(channelsBarSize + chatBarSize + 2 + 2))
			ChatChannelParentFrame.Position = UDim2.new(0, 0, 0, channelsBarSize + 2)

		else
			ChatChannelParentFrame.Size = UDim2.new(1, 0, 1, -(chatBarSize + 2 + 2))
			ChatChannelParentFrame.Position = UDim2.new(0, 0, 0, 2)

		end
	end

	local function UpdateChatChannelsTabTextSize(size)
		local channelsBarSize = CalculateChannelsBarPixelSize(size)
		ChannelsBarParentFrame.Size = UDim2.new(1, 0, 0, channelsBarSize)

		UpdateChatChannelParentFrameSize()
	end

	local function UpdateChatBarTextSize(size)
		local chatBarSize = CalculateChatBarPixelSize(size)

		ChatBarParentFrame.Size = UDim2.new(1, 0, 0, chatBarSize)
		if not bubbleChatOnly() then
			ChatBarParentFrame.Position = UDim2.new(0, 0, 1, -chatBarSize)
		end

		ChatResizerFrame.Size = UDim2.new(0, chatBarSize, 0, chatBarSize)
		ChatResizerFrame.Position = UDim2.new(1, -chatBarSize, 1, -chatBarSize)

		UpdateChatChannelParentFrameSize()
		UpdateResizable(ChatSettings.WindowResizable)
	end

	local function UpdateShowChannelsBar(enabled)
		ChannelsBarParentFrame.Visible = enabled
		UpdateChatChannelParentFrameSize()
	end

	UpdateChatChannelsTabTextSize(ChatSettings.ChatChannelsTabTextSize)
	UpdateChatBarTextSize(ChatSettings.ChatBarTextSize)
	UpdateDraggable(ChatSettings.WindowDraggable)
	UpdateResizable(ChatSettings.WindowResizable)
	UpdateShowChannelsBar(ChatSettings.ShowChannelsBar)

	ChatSettings.SettingsChanged:connect(function(setting, value)
		if (setting == "WindowDraggable") then
			UpdateDraggable(value)

		elseif (setting == "WindowResizable") then
			UpdateResizable(value)

		elseif (setting == "ChatChannelsTabTextSize") then
			UpdateChatChannelsTabTextSize(value)

		elseif (setting == "ChatBarTextSize") then
			UpdateChatBarTextSize(value)

		elseif (setting == "ShowChannelsBar") then
			UpdateShowChannelsBar(value)

		end
	end)

	self.GuiObject = BaseFrame

	self.GuiObjects.BaseFrame = BaseFrame
	self.GuiObjects.ChatBarParentFrame = ChatBarParentFrame
	self.GuiObjects.ChannelsBarParentFrame = ChannelsBarParentFrame
	self.GuiObjects.ChatChannelParentFrame = ChatChannelParentFrame
	self.GuiObjects.ChatResizerFrame = ChatResizerFrame
	self.GuiObjects.ResizeIcon = ResizeIcon
	self:AnimGuiObjects()
end

function methods:GetChatBar()
	return self.ChatBar
end

function methods:RegisterChatBar(ChatBar)
	self.ChatBar = ChatBar
	self.ChatBar:CreateGuiObjects(self.GuiObjects.ChatBarParentFrame)
end

function methods:RegisterChannelsBar(ChannelsBar)
	self.ChannelsBar = ChannelsBar
	self.ChannelsBar:CreateGuiObjects(self.GuiObjects.ChannelsBarParentFrame)
end

function methods:RegisterMessageLogDisplay(MessageLogDisplay)
	self.MessageLogDisplay = MessageLogDisplay
	self.MessageLogDisplay.GuiObject.Parent = self.GuiObjects.ChatChannelParentFrame
end

function methods:AddChannel(channelName)
	if (self:GetChannel(channelName)) then
		error("Channel '" .. channelName .. "' already exists!")
		return
	end

	local channel = moduleChatChannel.new(channelName, self.MessageLogDisplay)
	self.Channels[channelName:lower()] = channel

	channel:SetActive(false)

	local tab = self.ChannelsBar:AddChannelTab(channelName)
	tab.NameTag.MouseButton1Click:connect(function()
		self:SwitchCurrentChannel(channelName)
	end)

	channel:RegisterChannelTab(tab)

	return channel
end

function methods:GetFirstChannel()
	--// Channels are not indexed numerically, so this function is necessary.
	--// Grabs and returns the first channel it happens to, or nil if none exist.
	for i, v in pairs(self.Channels) do
		return v
	end
	return nil
end

function methods:RemoveChannel(channelName)
	if (not self:GetChannel(channelName)) then
		error("Channel '" .. channelName .. "' does not exist!")
	end

	local indexName = channelName:lower()

	local needsChannelSwitch = false
	if (self.Channels[indexName] == self:GetCurrentChannel()) then
		needsChannelSwitch = true

		self:SwitchCurrentChannel(nil)
	end

	self.Channels[indexName]:Destroy()
	self.Channels[indexName] = nil

	self.ChannelsBar:RemoveChannelTab(channelName)

	if (needsChannelSwitch) then
		local generalChannelExists = (self:GetChannel(ChatSettings.GeneralChannelName) ~= nil)
		local removingGeneralChannel = (indexName == ChatSettings.GeneralChannelName:lower())

		local targetSwitchChannel = nil

		if (generalChannelExists and not removingGeneralChannel) then
			targetSwitchChannel = ChatSettings.GeneralChannelName
		else
			local firstChannel = self:GetFirstChannel()
			targetSwitchChannel = (firstChannel and firstChannel.Name or nil)
		end

		self:SwitchCurrentChannel(targetSwitchChannel)
	end

	if not ChatSettings.ShowChannelsBar then
		if self.ChatBar.TargetChannel == channelName then
			self.ChatBar:SetChannelTarget(ChatSettings.GeneralChannelName)
		end
	end
end

function methods:GetChannel(channelName)
	return channelName and self.Channels[channelName:lower()] or nil
end

function methods:GetTargetMessageChannel()
	if (not ChatSettings.ShowChannelsBar) then
		return self.ChatBar.TargetChannel
	else
		local curChannel = self:GetCurrentChannel()
		return curChannel and curChannel.Name
	end
end

function methods:GetCurrentChannel()
	return self.CurrentChannel
end

function methods:SwitchCurrentChannel(channelName)
	if (not ChatSettings.ShowChannelsBar) then
		local targ = self:GetChannel(channelName)
		if (targ) then
			self.ChatBar:SetChannelTarget(targ.Name)
		end

		channelName = ChatSettings.GeneralChannelName
	end

	local cur = self:GetCurrentChannel()
	local new = self:GetChannel(channelName)
	if new == nil then
		error(string.format("Channel '%s' does not exist.", channelName))
	end

	if (new ~= cur) then
		if (cur) then
			cur:SetActive(false)
			local tab = self.ChannelsBar:GetChannelTab(cur.Name)
			tab:SetActive(false)
		end

		if (new) then
			new:SetActive(true)
			local tab = self.ChannelsBar:GetChannelTab(new.Name)
			tab:SetActive(true)
		end

		self.CurrentChannel = new
	end

end

function methods:UpdateFrameVisibility()
	self.GuiObject.Visible = (self.Visible and self.CoreGuiEnabled)
end

function methods:GetVisible()
	return self.Visible
end

function methods:SetVisible(visible)
	self.Visible = visible
	self:UpdateFrameVisibility()
end

function methods:GetCoreGuiEnabled()
	return self.CoreGuiEnabled
end

function methods:SetCoreGuiEnabled(enabled)
	self.CoreGuiEnabled = enabled
	self:UpdateFrameVisibility()
end

function methods:EnableResizable()
	self.GuiObjects.ChatResizerFrame.Active = true
end

function methods:DisableResizable()
	self.GuiObjects.ChatResizerFrame.Active = false
end

function methods:FadeOutBackground(duration)
	self.ChannelsBar:FadeOutBackground(duration)
	self.MessageLogDisplay:FadeOutBackground(duration)
	self.ChatBar:FadeOutBackground(duration)

	self.AnimParams.Background_TargetTransparency = 1
	self.AnimParams.Background_NormalizedExptValue = CurveUtil:NormalizedDefaultExptValueInSeconds(duration)
end

function methods:FadeInBackground(duration)
	self.ChannelsBar:FadeInBackground(duration)
	self.MessageLogDisplay:FadeInBackground(duration)
	self.ChatBar:FadeInBackground(duration)

	self.AnimParams.Background_TargetTransparency = 0.6
	self.AnimParams.Background_NormalizedExptValue = CurveUtil:NormalizedDefaultExptValueInSeconds(duration)
end

function methods:FadeOutText(duration)
	self.MessageLogDisplay:FadeOutText(duration)
	self.ChannelsBar:FadeOutText(duration)
end

function methods:FadeInText(duration)
	self.MessageLogDisplay:FadeInText(duration)
	self.ChannelsBar:FadeInText(duration)
end

function methods:AnimGuiObjects()
	self.GuiObjects.ChatChannelParentFrame.BackgroundTransparency = self.AnimParams.Background_CurrentTransparency
	self.GuiObjects.ChatResizerFrame.BackgroundTransparency = self.AnimParams.Background_CurrentTransparency
	self.GuiObjects.ResizeIcon.ImageTransparency = self.AnimParams.Background_CurrentTransparency
end

function methods:InitializeAnimParams()
	self.AnimParams.Background_TargetTransparency = 0.6
	self.AnimParams.Background_CurrentTransparency = 0.6
	self.AnimParams.Background_NormalizedExptValue = CurveUtil:NormalizedDefaultExptValueInSeconds(0)
end

function methods:Update(dtScale)
	self.ChatBar:Update(dtScale)
	self.ChannelsBar:Update(dtScale)
	self.MessageLogDisplay:Update(dtScale)

	self.AnimParams.Background_CurrentTransparency = CurveUtil:Expt(
			self.AnimParams.Background_CurrentTransparency,
			self.AnimParams.Background_TargetTransparency,
			self.AnimParams.Background_NormalizedExptValue,
			dtScale
	)

	self:AnimGuiObjects()
end
