local addon, ns = ...
local C = ns.shared.color
local L = ns.L


local function updateTracking()
	local tActive = 0
	local n = {}

	for i = 1, GetNumTrackingTypes() do
		local name, tex, active, category = GetTrackingInfo(i)
		if active == 1 then
			tActive = tActive + 1
			n[tActive] = {["Name"] = name, ["Texture"] = tex}
		end
	end

	return tActive, n
end 

local tracking_onevent = function(self)
	local numActive, trackActive = updateTracking()
	local n = ""
	local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["Tracking"])

	if numActive == 0 then 
		n = "None"
	else
		for i = 1, numActive, 1 do
			n = trackActive[i]["Name"]
		end
	end

	dataobj.text = n
end


local tracking_events = {
	"MINIMAP_UPDATE_TRACKING",
	"PLAYER_LOGIN",
	"PLAYER_ENTERING_WORLD"
}


local tracking_onclick = function (self, btn)
	if btn == "LeftButton" then 
		ToggleDropDownMenu(1, nil, MiniMapTrackingDropDown, self, 0, 0)
	end
end


local tracking_tooltip = function(t)
	local numActive, trackActive = updateTracking()
	t:AddLine(L["Tracking"])
	t:AddLine(" ")

	if numActive == 0 then
		t:AddLine(C.white(L["No tracking currently active."]))
	else
		for i = 1, numActive do 
			t:AddDoubleLine(C.white(trackActive[i]["Name"]))
			t:AddTexture(trackActive[i]["Texture"])
		end
	end

	t:AddLine(" ")
	t:AddLine(C.dkgreen(L["Hint: Left click to choose tracking skill."]))
end


Broker_Everything:Register(L["Tracking"],
	"Interface\\Addons\\Broker_Everything\\media\\tracking",--"Interface\\Minimap\\TRACKING\\None",
	0,
	tracking_onevent,
	tracking_events,
	tracking_onclick,
	tracking_tooltip,
	nil,
	nil)

