Quantcast
local addonname, addon = ...
local L = addon.L

addon.hooks = {}

local function ReplaceText(frame)
	if frame and frame.GetText and frame.SetText then
		local text = frame:GetText()
		if not text then return end

		for orig, nick in addon:IterNicknames() do
			if text:find(orig, nil, true) then
				return frame:SetText(text:replace(orig, nick))
			end
		end
	end
end

local function ReplaceLink(text)
	return text:gsub("|HBNplayer:(.-):(.-):(.-):(.-):(.-)|h%[(.+)%]|h", function(a1, a2, a3, a4, a5, a6)
		return ("|HBNplayer:%s:%s:%s:%s:%s|h[%s]|h"):format(a1, a2, a3, a4, a5, addon:GetNickname(a6) or a6)
	end)
end

function addon.hooks.chat()
	local origs = {}

	local function AddMessage(self, text, ...)
		if text:find"You are now in a conversation with" then
			for orig, nick in addon:IterNicknames() do
				if text:find(orig, nil, true) then
					text = text:replace(orig, nick)
				end
			end
		end

		return origs[self](self, ReplaceLink(text), ...)
	end

	local function HookChatFrame(cf)
		if not origs[cf] then
			origs[cf] = cf.AddMessage
			cf.AddMessage = AddMessage
		end
	end

	hooksecurefunc("FCF_SetWindowName", function(frame, name, ...)
		HookChatFrame(frame)
		ReplaceText(_G[frame:GetName().."Tab"])
	end)

	hooksecurefunc("ChatEdit_UpdateHeader", function(editbox)
		local event = editbox:GetAttribute"chatType"
		local header = _G[editbox:GetName().."Header"]
		local target = editbox:GetAttribute"tellTarget"

		if event == "BN_WHISPER" then
			header:SetFormattedText(CHAT_BN_WHISPER_SEND, addon:GetNickname(target) or target)
			editbox:SetTextInsets(15 + header:GetWidth(), 13, 0, 0)
		end
	end)

	for i = 1, NUM_CHAT_WINDOWS do
		HookChatFrame(_G["ChatFrame"..i])
	end
end

function addon.hooks.friendsframe()
	-- Hook tooltips
	FriendsTooltip:HookScript("OnShow", function(self)
		ReplaceText(FriendsTooltipHeader)
	end)

	BNToastFrame:HookScript("OnShow", function(self)
		ReplaceText(BNToastFrameTopLine)
	end)

	hooksecurefunc("FriendsFrameTooltip_Show", function(self)
		ReplaceText(FriendsTooltipHeader)
	end)

	-- Hook friends frame and it's subframes
	hooksecurefunc("FriendsFrame_SetButton", function(button, index, firstbutton)
		if button.buttonType == FRIENDS_BUTTON_TYPE_BNET then
			ReplaceText(button.name)
		end
	end)

	hooksecurefunc("FriendsFrame_ShowBNDropdown", function(...)
		ReplaceText(DropDownList1Button1)
	end)

	hooksecurefunc(FriendsFrameFriendsScrollFrame, "buttonFunc", function(button, index, firstbutton)
		if button.buttonType == FRIENDS_BUTTON_TYPE_BNET then
			ReplaceText(button.name)
		end
	end)

	hooksecurefunc("FriendsFriendsList_Update", function()
		ReplaceText(FriendsFriendsFrameTitle)

		for i = 1, FRIENDS_FRIENDS_TO_DISPLAY do
			local frame = _G["FriendsFriendsButton"..i]
			if frame then
				ReplaceText(frame.name)
			end
		end
	end)

	hooksecurefunc("BNConversationInvite_Update", function()
		for i = 1, BN_CONVERSATION_INVITE_NUM_DISPLAYED do
			local frame = _G["BNConversationInviteDialogListFriend"..i]
			if frame then
				ReplaceText(frame.name)
			end
		end
	end)

	hooksecurefunc(StaticPopupDialogs["SET_BNFRIENDNOTE"], "OnShow", function(self)
		ReplaceText(self.text)
	end)

	addon:Update()
end

function addon.hooks.popup()
	StaticPopupDialogs["BN_RENAME_FRIEND"] = {
		text = L["Set a nickname for %s"],
		button1 = ACCEPT,
		button2 = CANCEL,
		hasEditBox = 1,
		hideOnEscape = 1,
		timeout = 0,
		exclusive = 1,
		whileDead = 1,

		OnAccept = function(self)
			local name = self.editBox:GetText()
			addon:SetNickname(self.data, name)
		end,

		OnShow = function(self)
			local name = addon:GetNickname(self.data) or ""

			self.editBox:SetText(name)
			self.editBox:SetFocus()
		end,

		EditBoxOnEnterPressed = function(self)
			local parent = self:GetParent()
			local name = parent.editBox:GetText()

			addon:SetNickname(parent.data, name)
			parent:Hide()
		end,

		EditBoxOnEscapePressed = function(self)
			self:GetParent():Hide()
		end,
	}

	UnitPopupButtons["BN_RENAME"] = { text = PET_RENAME, dist = 0 }

	-- Handle right click menu
	hooksecurefunc("UnitPopup_OnClick", function(self)
		local dropdown = UIDROPDOWNMENU_INIT_MENU
		local button = self.value

		if button == "BN_RENAME" then
			local pid, firstname, lastname = BNGetFriendInfoByID(dropdown.presenceID)
			local name = firstname.." "..lastname

			StaticPopup_Show("BN_RENAME_FRIEND", name, nil, name)
		end
	end)
end