Quantcast
local T, C, L = unpack(Tukui)
local function SetupChat(self)
	self:UnregisterAllEvents()

	local general, loot = ChatFrame1, ChatFrame4

	if C.chat.background then
		local leftbg, rightbg = TukuiChatBackgroundLeft, TukuiChatBackgroundRight
		local lefttab, righttab = TukuiTabsLeftBackground, TukuiTabsRightBackground

		leftbg:ThickBorder()
		lefttab:ThickBorder()

		if C.chat.dockedLoot then
			rightbg:SetBackdrop(nil)
			righttab:SetBackdrop(nil)
		else
			rightbg:ThickBorder()
			righttab:ThickBorder()
		end

		general:ClearAllPoints()
		general:SetPoint("BOTTOMLEFT", leftbg, "BOTTOMLEFT", 6, 6)
		general:SetPoint("TOPRIGHT", leftbg, "TOPRIGHT", -6, -32)

		if not C.chat.dockedLoot then
			loot:ClearAllPoints()
			loot:SetPoint("BOTTOMLEFT", rightbg, "BOTTOMLEFT", 6, 6)
			loot:SetPoint("TOPRIGHT", rightbg, "TOPRIGHT", -6, -32)
		end

		leftbg:ClearAllPoints()
		leftbg:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 6, 6)

		rightbg:ClearAllPoints()
		rightbg:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -6, 6)
	else
		general:ClearAllPoints()
		general:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 6, 6)

		loot:ClearAllPoints()
		loot:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -6, 6)

		TukuiChatchatEditBoxBackground:Kill()

		for i=1, NUM_CHAT_WINDOWS do
			local frame = _G["ChatFrame"..i]
			local editbox = _G["ChatFrame"..i.."EditBox"]
			editbox:ClearAllPoints()
			editbox:SetPoint("BOTTOMLEFT", frame, "TOPLEFT", 10, 16)
			editbox:SetPoint("BOTTOMRIGHT", frame, "TOPRIGHT", -10, 16)
		end
	end

	if C.chat.dockedLoot then
		FCF_DockFrame(loot)
		loot:SetJustifyH("LEFT")
	else
		FCF_UnDockFrame(loot)
		loot:SetJustifyH("RIGHT")
	end

	general._SetPoint = general.SetPoint
	general.SetPoint = T.Dummy
	loot._SetPoint = general.SetPoint
	loot.SetPoint = T.Dummy
end

local OnLoad = CreateFrame("Frame")
OnLoad:RegisterEvent("PLAYER_ENTERING_WORLD")
OnLoad:SetScript("OnEvent", function(self) T.Delay(2, SetupChat(self)) end)


if true then return end --Leaving all of this code here to use for later, but stop the file execution right here.
local leftBG = TukuiChatBackgroundLeft or CreateFrame("Frame", "TukuiChatBackgroundLeft", UIParent)
local leftTabs = TukuiTabsLeftBackground
local rightBG = TukuiChatBackgroundRight or CreateFrame("Frame", "TukuiChatBackgroundLeft", UIParent)
local rightTabs = TukuiTabsRightBackground
if not C.chat.lootRight and C.chat.background then rightBG:SetBackdrop(nil); rightBG.SetBackdrop = T.dummy end

local function UpdateChatSizes()
	rightBG:SetSize(C.chat.rightWidth, C.chat.rightHeight)
	leftBG:SetSize(C.chat.leftWidth, C.chat.leftHeight)
end

local function SetupBackgrounds()
	if not leftBG:IsUserPlaced() then
		leftBG:ClearAllPoints()
		leftBG:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 6, 6)
	end
	if leftTabs then
		leftTabs:ClearAllPoints()
		leftTabs:SetPoint("TOPLEFT", leftBG, "TOPLEFT", 6, -6)
		leftTabs:SetPoint("TOPRIGHT", TukuiButtonCF1, "TOPLEFT", -4, 0)
		leftTabs:SetHeight(16)
		leftTabs:SetTemplate("Transparent")
		leftTabs:ThickBorder()
	end

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

	if C.chat.rightLoot then
		rightBG:SetMovable(true)
		tinsert(T.AllowFrameMoving, rightBG)
		if not rightBG:IsUserPlaced() then
			rightBG:ClearAllPoints()
			rightBG:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -6, 6)
		end
		if C.chat.background then
			rightTabs:ClearAllPoints()
			rightTabs:SetPoint("TOPLEFT", rightBG, "TOPLEFT", 6, -6)
			rightTabs:SetPoint("TOPRIGHT", TukuiButtonCF4, "TOPLEFT", -4, 0)
			rightTabs:SetHeight(16)
		else
			if rightTabs then rightTabs:Kill() end
		end
	end


	if C.chat.background == true then
		leftBG:SetTemplate("Transparent")
		leftBG:ThickBorder()
		leftTabs:SetTemplate("Transparent")
		leftTabs:ThickBorder()

		if C.chat.rightLoot then
			rightBG:SetTemplate("Transparent")
			rightBG:ThickBorder()
			rightTabs:SetTemplate("Transparent")
			rightTabs:ThickBorder()
		end
	else
		if leftTabs then leftTabs:Kill() end
		leftBG:SetBackdrop(nil)
		if C.chat.rightLoot then rightBG:SetBackdrop(nil) end
	end
end

local function PositionChatWindows()
	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 C.chat.background then
			editbox:AllPoints(leftTabs)
			editbox:SetTemplate()
			editbox:ThickBorder()
		else
			editbox:SetPoint("BOTTOMLEFT", frame, "TOPLEFT", 0, 28)
			editbox:SetPoint("BOTTOMRIGHT", frame, "TOPRIGHT", 0, 28)
		end
		TukuiChatchatEditBoxBackground:Kill()

		copyButton:ClearAllPoints()
		if C.chat.background then
			if i == 4 and C.chat.rightLoot then
				copyButton:SetPoint("TOPRIGHT",	rightBG, "TOPRIGHT", -6, -6)
			else
				copyButton:SetPoint("TOPRIGHT", leftBG, "TOPRIGHT", -6, -6)
			end
		else
			copyButton:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -6, -6)
		end

		copyButton:SetHeight(leftTabs and leftTabs:GetHeight() or 16)
		copyButton:SetWidth(leftTabs and leftTabs:GetHeight() or 16)
		copyButton:SetParent(frame)
		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)

		local anchor = leftBG
		if i==4 then
			if C.chat.rightLoot then
				anchor = rightBG
				FCF_UnDockFrame(frame)
				frame:SetJustifyH("RIGHT")
			else
				FCF_DockFrame(frame)
				frame:SetJustifyH("LEFT")
			end
		end
		frame:ClearAllPoints()
		frame:SetPoint("BOTTOMLEFT", anchor, "BOTTOMLEFT", 6, 6)
		frame:SetPoint("TOPRIGHT", anchor, "TOPRIGHT", -6, -28)


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

local function LoadConfig()
	local leftConfig = CreateFrame("Frame", nil, leftBG)
	leftConfig:SetAllPoints(leftBG)
	leftConfig:AddEditBox("width", 30, 14, {"BOTTOMRIGHT", -6, 6})
	leftConfig.width:AddText("label", "Width", "RIGHT", leftConfig.width, "LEFT", -3, 0)
	leftConfig:AddEditBox("height", 30, 14, {"BOTTOM", leftConfig.width, "TOP", 0, 4})
	leftConfig.height:AddText("label", "Height", "RIGHT", leftConfig.height, "LEFT", -3, 0)
	leftConfig:Hide()
	leftConfig.width:SetText(format("%d", floor(C.chat.leftWidth)))
	leftConfig.width:SetScript("OnEnterPressed", function(self)
		self:ClearFocus()
		if not tonumber(self:GetText()) then self:SetText(format("%d", floor(C.chat.leftWidth))) 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.leftWidth = newChatWidth
		UpdateChatSizes()
	end)
	leftConfig.height:SetText(C.chat.leftHeight)
	leftConfig.height:SetScript("OnEnterPressed", function(self)
		self:ClearFocus()
		if not tonumber(self:GetText()) then self:SetText(C.chat.leftHeight) 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.leftHeight = newChatHeight
		UpdateChatSizes()
	end)

	local rightConfig
	if C.chat.rightLoot then
		rightConfig = CreateFrame("Frame", nil, rightBG)
		rightConfig:SetAllPoints(rightBG)
		rightConfig:AddEditBox("width", 30, 14, {"BOTTOMRIGHT", -6, 6})
		rightConfig.width:AddText("label", "Width", "RIGHT", rightConfig.width, "LEFT", -3, 0)
		rightConfig:AddEditBox("height", 30, 14, {"BOTTOM", rightConfig.width, "TOP", 0, 4})
		rightConfig.height:AddText("label", "Height", "RIGHT", rightConfig.height, "LEFT", -3, 0)
		rightConfig:Hide()
		rightConfig.width:SetText(format("%d", floor(C.chat.rightWidth)))
		rightConfig.width:SetScript("OnEnterPressed", function(self)
			self:ClearFocus()
			if not tonumber(self:GetText()) then self:SetText(format("%d", floor(C.chat.rightWidth))) 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.rightWidth = newChatWidth
			UpdateChatSizes()
		end)
		rightConfig.height:SetText(C.chat.rightHeight)
		rightConfig.height:SetScript("OnEnterPressed", function(self)
			self:ClearFocus()
			if not tonumber(self:GetText()) then self:SetText(C.chat.rightHeight) 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.rightHeight = newChatHeight
			UpdateChatSizes()
		end)
	end
	local enable = false
	local function MoveUI()
		if InCombatLockdown() then return end
		if not leftBG.moveText then leftBG:AddText("moveText", "", "CENTER", 0, 1) end
		if not rightBG.moveText then rightBG:AddText("moveText", "", "CENTER", 0, 1) end
		if enable then enable = false else enable = true end

		if enable then
			leftBG:SetTemplate("Transparent")
			leftBG:SetBackdropBorderColor(1, 0, 0)
			leftBG.moveText:SetText(leftBG:GetName())
			leftBG:SetFrameStrata("MEDIUM")
			leftBG:EnableMouse(true)
			leftConfig:Show()
			if C.chat.rightLoot then
				rightBG:SetTemplate("Transparent")
				rightBG:SetBackdropBorderColor(1, 0, 0)
				rightBG.moveText:SetText(rightBG:GetName())
				rightBG:SetFrameStrata("MEDIUM")
				rightBG:EnableMouse(true)
				rightConfig:Show()
			end
		else
			if C.chat.background then
				leftBG:SetTemplate("Transparent")
				if rightBG then rightBG:SetTemplate("Transparent") end
			else
				leftBG:SetBackdrop(nil)
				if rightBG then rightBG:SetBackdrop(nil) end
			end
			leftBG.moveText:SetText("")
			leftBG:SetFrameStrata("BACKGROUND")
			leftBG:EnableMouse(false)
			leftConfig:Hide()

			rightBG.moveText:SetText("")
			rightBG:SetFrameStrata("BACKGROUND")
			rightBG:EnableMouse(false)
			if rightConfig then rightConfig:Hide() end
		end


	end
	hooksecurefunc(_G.SlashCmdList, "MOVING", MoveUI)
end

local function RestoreSavedVariables()
	if SaftUISaved and SaftUISaved.Chat then
		chatSaved = SaftUISaved.Chat
		if rightBG then
			C.chat.rightWidth = chatSaved.rightWidth or rightBG:GetWidth()
			C.chat.rightHeight = chatSaved.rightHeight or rightBG:GetHeight()
		end
		C.chat.leftWidth = chatSaved.leftWidth or leftBG:GetWidth()
		C.chat.leftHeight = chatSaved.leftHeight or leftBG:GetHeight()
	else
		if rightBG then
			C.chat.rightWidth = rightBG:GetWidth()
			C.chat.rightHeight = rightBG:GetHeight()
		end
		C.chat.leftWidth = leftBG:GetWidth()
		C.chat.leftHeight = leftBG:GetHeight()
	end
	UpdateChatSizes()
end



local function SaveVariables()
	if not SaftUISaved then SaftUISaved = {} end
	if not SaftUISaved["Chat"] then SaftUISaved["Chat"] = {} end
	chatSaved = SaftUISaved.Chat

	chatSaved.rightWidth = C.chat.rightWidth
	chatSaved.rightHeight = C.chat.rightHeight
	chatSaved.leftWidth = C.chat.leftWidth
	chatSaved.leftHeight = C.chat.leftHeight
end

local function SetupChat(self, event)
	self:UnregisterEvent(event)
	RestoreSavedVariables()
	SetupBackgrounds()
	PositionChatWindows()
	LoadConfig()

	self:RegisterEvent("PLAYER_LOGOUT")
	self:SetScript("OnEvent", SaveVariables)
end

local OnLoad = CreateFrame("Frame")
OnLoad:RegisterEvent("PLAYER_ENTERING_WORLD")
OnLoad:RegisterEvent("PLAYER_LOGIN")
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