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

local chatBGLeft = TukuiChatBackgroundLeft
local tabBGLeft = TukuiTabsLeftBackground
local chatBGRight = TukuiChatBackgroundRight
local tabBGRight = TukuiTabsRightBackground

local function SetupChat(self)
	self:UnregisterAllEvents()

	if C.chat.background == true then
		tabBGLeft:ClearAllPoints()
		tabBGLeft:SetPoint("TOPLEFT", chatBGLeft, "TOPLEFT", 6, -6)
		tabBGLeft:SetPoint("TOPRIGHT", TukuiButtonCF1, "TOPLEFT", -4, 0)
		tabBGLeft:SetHeight(16)
		tabBGLeft:SetTemplate("Transparent")
		tabBGLeft:ThickBorder()

		chatBGLeft:SetTemplate("Transparent")
		chatBGLeft:ThickBorder()
		chatBGLeft:SetMovable(true)
		tinsert(T.AllowFrameMoving, chatBGLeft)
		if not chatBGLeft:IsUserPlaced() then
			chatBGLeft:ClearAllPoints()
			chatBGLeft:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 6, 6)
		end

		if C.chat.rightLoot then
			tabBGRight:ClearAllPoints()
			tabBGRight:SetPoint("TOPLEFT", chatBGRight, "TOPLEFT", 6, -6)
			tabBGRight:SetPoint("TOPRIGHT", TukuiButtonCF4, "TOPLEFT", -4, 0)
			tabBGRight:SetHeight(16)
			tabBGRight:SetTemplate("Transparent")
			tabBGRight:ThickBorder()

			chatBGRight:SetTemplate("Transparent")
			chatBGRight:ThickBorder()
			chatBGRight:SetMovable(true)
			tinsert(T.AllowFrameMoving, chatBGRight)
			if not chatBGRight:IsUserPlaced() then
				chatBGRight:ClearAllPoints()
				chatBGRight:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -6, 6)
			end
		else
			chatBGRight:Kill()
			tabBGRight:Kill()
		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 tabBGLeft then
			editbox:AllPoints(tabBGLeft)
			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", (i == 4 and chatBGRight or chatBGLeft) or frame, "TOPRIGHT", -6, -6)
		copyButton:SetParent(frame)
		copyButton:SetHeight(tabBGLeft and tabBGLeft:GetHeight() or 16)
		copyButton:SetWidth(tabBGLeft and tabBGLeft: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", chatBGLeft, "BOTTOMLEFT", 6, 6)
			frame:SetPoint("TOPRIGHT", chatBGLeft, "TOPRIGHT", -6, -28)
		end

		-- FCF_SetLocked(frame, 1)
		if i==4 then
			if C.chat.rightLoot then
				FCF_DockFrame(frame)
			else
				FCF_UndockFrame(frame)
				frame:ClearAllPoints()
				frame:SetPoint("BOTTOMLEFT", chatBGRight, "BOTTOMLEFT", 6, 6)
				frame:SetPoint("TOPRIGHT", chatBGRight, "TOPRIGHT", -6, -28)
				frame:SetJustifyH("RIGHT")
			end
		end

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

	if C.chat.background then
		local ConfigFrame = CreateFrame("Frame", nil, chatBGLeft)
		ConfigFrame:SetAllPoints(chatBGLeft)
		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(chatBGLeft:GetWidth())
			C.chat.height = floor(chatBGLeft: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
			chatBGLeft: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
			chatBGLeft:SetHeight(C.chat.height)
		end)

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

			if enable then
				chatBGLeft:SetTemplate("Transparent")
				chatBGLeft:SetBackdropBorderColor(1, 0, 0)
				chatBGLeft.moveText:SetText(chatBGLeft:GetName())
				chatBGLeft:SetFrameStrata("MEDIUM")
				chatBGLeft:EnableMouse(true)
				ConfigFrame:Show()
			else
				chatBGLeft:SetTemplate("Transparent")
				chatBGLeft:ThickBorder()
				tabBGLeft:SetTemplate("Transparent")
				tabBGLeft:ThickBorder()
				chatBGLeft.moveText:SetText("")
				chatBGLeft:SetFrameStrata("BACKGROUND")
				chatBGLeft: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