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

local chatBG = TukuiChatBackgroundLeft
local tabBG = TukuiTabsLeftBackground

if not chatBG then
	chatBG = CreateFrame("Frame", "TukuiChatBackgroundLeft", UIParent)
end
if not tabBG then
	tabBG = CreateFrame("Frame", "TukuiTabsLeftBackground", chatBG)
end

local function SetupChatBG()
	print("settin' shit up")
	if C.chat.background == true then
		for _, f in pairs({chatBG, tabBG}) do
			f:SetTemplate("Transparent")
			f:ThickBorder()
		end
	else
		for _, f in pairs({chatBG, tabBG}) do
			f:SetBackdrop(nil)
			if f.innerborder then f.innerborder:Hide() end
			if f.outerborder then f.outerborder:Hide() end
		end
	end
end

local function SetupChat(self)
	self:UnregisterAllEvents()
	tabBG:ClearAllPoints()
	tabBG:SetPoint("TOPLEFT", chatBG, "TOPLEFT", 6, -6)
	tabBG:SetPoint("TOPRIGHT", TukuiButtonCF1, "TOPLEFT", -4, 0)
	tabBG:SetHeight(16)
	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 not C.chat.background then
			editbox:AllPoints(TukuiInfoLeft)
		else
			editbox:Point("TOPLEFT", tabBG, "BOTTOMLEFT", 2, -2)
			editbox:Point("TOPRIGHT", tabBG, "BOTTOMRIGHT", -2, 2)
			editbox:SetTemplate()
			editbox:ThickBorder()
		end

		copyButton:ClearAllPoints()
		copyButton:SetPoint("TOPRIGHT", chatBG, "TOPRIGHT", -6, -6)
		copyButton:SetHeight(tabBG:GetHeight())
		copyButton:SetWidth(tabBG:GetHeight())
		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)

		SetupChatBG()
		chatBG:SetMovable(true)
		tinsert(T.AllowFrameMoving, chatBG)
		if not chatBG:IsUserPlaced() then
			chatBG:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 6, 6)
		end

		if TukuiChatBackgroundRight then
			TukuiChatBackgroundRight:Kill()
			TukuiTabsRightBackground:Kill()
		end

		if i==1 then
			frame:ClearAllPoints()
			frame:SetPoint("BOTTOMLEFT", chatBG, "BOTTOMLEFT", 6, 6)
			frame:SetPoint("TOPRIGHT", chatBG, "TOPRIGHT", -6, -28)
		elseif i==4 then
			FCF_SetLocked(frame, 1)
			FCF_DockFrame(frame)
		end

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

	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)
	print("settin' shit up")
	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
			SetupChatBG()
			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

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