Quantcast
local T, C, L = unpack(Tukui)

local chatBG = TukuiChatBackgroundLeft
local tabBG = TukuiTabsLeftBackground

local function SetupChat(self)
	self:UnregisterAllEvents()

	if tabBG then
		tabBG:ClearAllPoints()
		tabBG:SetPoint("TOPLEFT", chatBG, "TOPLEFT", 6, -6)
		tabBG:SetPoint("TOPRIGHT", TukuiButtonCF1, "TOPLEFT", -4, 0)
		tabBG:SetHeight(16)
	end


	if chatBG then
		chatBG:SetTemplate("Transparent")
		chatBG:ThickBorder()
		tabBG:SetTemplate("Transparent")
		tabBG:ThickBorder()

		chatBG:SetMovable(true)
		tinsert(T.AllowFrameMoving, chatBG)

		if not chatBG:IsUserPlaced() then
			chatBG:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 6, 6)
		end
	end

	for i = 1, NUM_CHAT_WINDOWS do
		local frameName = format("ChatFrame%s", i)
		local frame = _G[frameName]
		local tab = _G[frameName.."Tab"]
		local tabText = _G[frameName.."TabText"]
		local ID = frame:GetID()
		local copyButton = _G[format("TukuiButtonCF%s", i)]
		local editbox = _G[frameName.."EditBox"]


		editbox:ClearAllPoints()
		if tabBG then
			editbox:AllPoints(tabBG)
			editbox:SetTemplate()
			editbox:ThickBorder()
		else
			editbox:SetPoint("TOPLEFT", frame, "BOTTOMLEFT", 0, -2)
			editbox:SetPoint("TOPRIGHT", frame, "BOTTOMRIGHT", 0, -2)
		end
		TukuiChatchatEditBoxBackground:Kill()


		copyButton:ClearAllPoints()
		copyButton:SetPoint("TOPRIGHT", chatBG or frame, "TOPRIGHT", -6, -6)
		copyButton:SetParent(frame)
		copyButton:SetHeight(tabBG and tabBG:GetHeight() or 16)
		copyButton:SetWidth(tabBG and tabBG:GetHeight() or 16)
		copyButton:SetAlpha(C.chat.background and 1 or 0)
		copyButton:SetScript("OnEnter", function(self)
			if C.chat.background then
				T.SetModifiedBackdrop(self)
			else
				self:SetAlpha(1)
			end
		end)
		copyButton:SetScript("OnLeave", function(self)
			if C.chat.background then
				T.SetOriginalBackdrop(self)
			else
				self:SetAlpha(0)
			end
		end)

		if C.chat.background then
			frame:ClearAllPoints()
			frame:SetPoint("BOTTOMLEFT", chatBG, "BOTTOMLEFT", 6, 6)
			frame:SetPoint("TOPRIGHT", chatBG, "TOPRIGHT", -6, -28)
		end

		if i==4 then
			FCF_SetLocked(frame, 1)
			FCF_DockFrame(frame)
		end

		tabText:SetFont(unpack(C.pFont))
		tabText:SetShadowOffset(0, 0)
	end

	if C.chat.background then
		local ConfigFrame = CreateFrame("Frame", nil, chatBG)
		ConfigFrame:SetAllPoints(chatBG)
		ConfigFrame:AddEditBox("width", 30, 14, {"BOTTOMRIGHT", -6, 6})
		ConfigFrame.width:AddText("label", "Width", "RIGHT", ConfigFrame.width, "LEFT", -3, 0)
		ConfigFrame:AddEditBox("height", 30, 14, {"BOTTOM", ConfigFrame.width, "TOP", 0, 4})
		ConfigFrame.height:AddText("label", "Height", "RIGHT", ConfigFrame.height, "LEFT", -3, 0)
		ConfigFrame:Hide()
		if SaftUISaved and SaftUISaved.Chat then
			chatSaved = SaftUISaved.Chat
			C.chat.width = chatSaved.width
			C.chat.height = chatSaved.height
		else
			C.chat.width = floor(chatBG:GetWidth())
			C.chat.height = floor(chatBG:GetHeight())
		end
		ConfigFrame:RegisterEvent("PLAYER_LOGOUT")
		ConfigFrame:SetScript("OnEvent", function(self, event)
			if event=="PLAYER_LOGOUT" then
				if not SaftUISaved then SaftUISaved = {} end
				if not SaftUISaved["Chat"] then SaftUISaved["Chat"] = {} end
				chatSaved = SaftUISaved.Chat
				chatSaved.width = C.chat.width
				chatSaved.height = C.chat.height
			end
		end)

		ConfigFrame.width:SetText(format("%d", floor(C.chat.width)))
		ConfigFrame.width:SetScript("OnEnterPressed", function(self)
			self:ClearFocus()
			if not tonumber(self:GetText()) then self:SetText(format("%d", floor(C.chat.width))) return end
			local newChatWidth = tonumber(self:GetText())
			if newChatWidth > 500 then newChatWidth = 500; self:SetText("500") end
			if newChatWidth < 200 then newChatWidth = 200; self:SetText("200") end
			C.chat.width = newChatWidth
			chatBG:SetWidth(C.chat.width)
		end)
		ConfigFrame.height:SetText(C.chat.height)
		ConfigFrame.height:SetScript("OnEnterPressed", function(self)
			self:ClearFocus()
			if not tonumber(self:GetText()) then self:SetText(C.chat.height) return end
			local newChatHeight = tonumber(self:GetText())
			if newChatHeight > 300 then newChatHeight = 300; self:SetText("500") end
			if newChatHeight < 50 then newChatHeight = 50; self:SetText("50") end
			C.chat.height = newChatHeight
			chatBG:SetHeight(C.chat.height)
		end)

		chatBG:SetWidth(C.chat.width)
		chatBG:SetHeight(C.chat.height)
		local enable = true
		local function MoveUI()
			if InCombatLockdown() then return end
			if not chatBG.moveText then chatBG:AddText("moveText", "", "CENTER", 0, 1) end

			if enable then
				chatBG:SetTemplate("Transparent")
				chatBG:SetBackdropBorderColor(1, 0, 0)
				chatBG.moveText:SetText(chatBG:GetName())
				chatBG:SetFrameStrata("MEDIUM")
				chatBG:EnableMouse(true)
				ConfigFrame:Show()
			else
				chatBG:SetTemplate("Transparent")
				chatBG:ThickBorder()
				tabBG:SetTemplate("Transparent")
				tabBG:ThickBorder()
				chatBG.moveText:SetText("")
				chatBG:SetFrameStrata("BACKGROUND")
				chatBG:EnableMouse(false)
				ConfigFrame:Hide()
			end

			if enable then enable = false else enable = true end
		end
		hooksecurefunc(_G.SlashCmdList, "MOVING", MoveUI)
	end
end

local OnLoad = CreateFrame("Frame")
OnLoad:RegisterEvent("PLAYER_ENTERING_WORLD")
OnLoad:SetScript("OnEvent", SetupChat)

local numlines = 1
function FloatingChatFrame_OnMouseScroll(self, delta)
	if delta < 0 then
		if IsShiftKeyDown() then
			self:ScrollToBottom()
		else
			for i=1, numlines do
				self:ScrollDown()
			end
		end
	elseif delta > 0 then
		if IsShiftKeyDown() then
			self:ScrollToTop()
		else
			for i=1, numlines do
				self:ScrollUp()
			end
		end
	end
end