local C = Broker_Everything.color
local tooltip = nil

local friends_onevent = function (self)
	local numFriends, friendsOnline = GetNumFriends()
	local numBNFriends, numOnlineBNFriends = BNGetNumFriends()
	local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["FRIENDS"])


	dataobj.text = friendsOnline + numOnlineBNFriends .. "/" .. numFriends + numBNFriends
end


local friends_events = {
	"PLAYER_LOGIN",
	"FRIENDLIST_UPDATE",
	"PLAYER_ENTERING_WORLD",
}

local function drawFriendsTip()
	local line, column
	tooltip:AddHeader(C.dkyellow(L["FRIENDS"]))

	local numBNFriends, numOnlineBNFriends = BNGetNumFriends()
	if numOnlineBNFriends > 0 then
		tooltip:AddLine(C.ltyellow(L["RealID"]), C.ltyellow(L["CHARACTERNAME"]), C.ltyellow(L["GAMEREALM"]), C.ltyellow(L["NOTES"]))	
		tooltip:AddSeparator()

		for i = 1, numBNFriends, 1 do
			local presenceID, givenName, surname, toonName, toonID, client, isOnline, lastOnline, isAFK, isDND, broadcast, notes = BNGetFriendInfo(i)
			local _, _, game, realm, faction, race, class, guild, zone, level, gameText = BNGetToonInfo(toonID or 0)

			if isOnline then
				line, column = tooltip:AddLine(C.ltblue(string.format("%s %s", givenName, surname)), C.className(toonName, class), C.white(string.format("%s : %s", game, realm)), C.grey(notes or " ") )
						local name = presenceID and givenName.." "..surname 
		--SetItemRef( "player:"..name, format( "|Hplayer:%1$s|h[%1$s]|h", name ), "LeftButton" )
				tooltip:SetLineScript(line, "OnMouseUp", function(self) if IsAltKeyDown() then InviteUnit(n) else SetItemRef("player:"..name, "|Hplayer:"..name.."|h["..name.."|h", "LeftButton") end end, n)	
				tooltip:SetLineScript(line, "OnEnter", function(self) tooltip:SetLineColor(line, 1,192/255, 90/255, 0.3) end )
				tooltip:SetLineScript(line, "OnLeave", function(self) tooltip:SetLineColor(line, 0,0,0,0) end)
			end
		end
		tooltip:AddLine(" ")
	end


	local numFriends, friendsOnline = GetNumFriends()

	if friendsOnline > 0 then 
		tooltip:AddLine(C.ltyellow(L["CHARACTERNAME"]), C.ltyellow(L["LEVEL"]), C.ltyellow(L["ZONE"]), C.ltyellow(L["NOTES"]))
		tooltip:AddSeparator()
	
		for i = 1, numFriends do
			local n, l, c, a, o, s, no = GetFriendInfo(i)
			if n and o then
				local character = string.format("%s %s", s or "", n)
				line, column = tooltip:AddLine(C.className(character,c), l, a or "", no or "")
				tooltip:SetLineScript(line, "OnMouseUp", function(self) if IsAltKeyDown() then InviteUnit(n) else SetItemRef("player:"..name, "|Hplayer:"..name.."|h["..name.."|h", "LeftButton") end end, n)			
				tooltip:SetLineScript(line, "OnEnter", function(self) tooltip:SetLineColor(line, 1,192/255, 90/255, 0.3) end )
				tooltip:SetLineScript(line, "OnLeave", function(self) tooltip:SetLineColor(line, 0,0,0,0) end)
			end
		end
	else
		tooltip:AddLine(L["No Friends Online."])
	end
	local line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["FRIENDSHINT"]), nil, nil, 4)
	line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["FRIENDSHINTWHISPER"]), nil, nil, 4)
	line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["FRIENDSHINTINVITE"]), nil, nil, 4)

	line, column = nil, nil
end

-- function to hide the tooltip, if the mouse isn't over it.
local function hideTooltip()
	if MouseIsOver(tooltip) then return end
  
	tooltip:SetScript('OnLeave', nil)
	LibQTip:Release(tooltip)
	tooltip = nil

end

local friends_init = function(obj)
	if not obj then return end

	obj = obj.obj

	obj.OnLeave = function(self)
		if tooltip then
			if MouseIsOver(tooltip) then
					tooltip:SetScript('OnLeave', hideTooltip)
			else
					hideTooltip()
			end
		end
	end

	obj.OnEnter = function(self)
		tooltip = LibQTip:Acquire("FriendsTT", 4 , "LEFT", "CENTER", "CENTER", "LEFT") 
		tooltip:SetScale(1)
		

		drawFriendsTip()

		tooltip:SmartAnchorTo(self)
		tooltip:UpdateScrolling()
		tooltip:Show()

	end

end


local friends_onclick = function()
	ToggleFriendsFrame(1)
end


Broker_Everything:Register(L["FRIENDS"],
	--GetItemIcon(22167),
	"Interface\\Addons\\Broker_Everything\\media\\friends",
	friends_init,
	friends_onevent,
	friends_events,
	friends_onclick,
	0, -- friends_tooltip,
	nil,
	nil)

