local C = Broker_Everything.color

local ds_onevent = function (self)
	local i = GetActiveTalentGroup()
	local c = {}

	TalentFrame_UpdateSpecInfoCache(c, false, false, i)

	if c and c[c.primaryTabIndex] then
	    local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["DUALSPEC"])
		dataobj.text = c[c.primaryTabIndex].name
		dataobj.icon = c[c.primaryTabIndex].icon
	end
end


local ds_events = {
	"PLAYER_LOGIN",
	"ACTIVE_TALENT_GROUP_CHANGED",
	"PLAYER_ENTERING_WORLD",

	"SKILL_LINES_CHANGED",
	"CHARACTER_POINTS_CHANGED",
	"PLAYER_TALENT_UPDATE",
}


local ds_onclick = function (self, btn)
	if btn == "RightButton" then
		if GetActiveTalentGroup() == 1 then
			SetActiveTalentGroup(2)
		else
			SetActiveTalentGroup(1)
		end
	else
		--ToggleTalentFrame()
		-- Code from Silverwind to solve glyph frame taint.
		UIParentLoadAddOn"Blizzard_TalentUI"
		--if button == "RightButton" then
		--	ToggleFrame(PlayerTalentFrame)
		--	PlayerTalentFrameTab3:GetScript("OnClick")(PlayerTalentFrameTab3,"LeftButton")
		--else
			ToggleFrame(PlayerTalentFrame)
			PlayerTalentFrameTab1:GetScript("OnClick")(PlayerTalentFrameTab1,"LeftButton")
		--end

	end
end


local ds_tooltip = function (t)
	t:AddLine(L["Talents"])
	t:AddLine(C.dkgreen(L["DUALHINTLEFT"]))
	t:AddLine(C.dkgreen(L["DUALHINTRIGHT"]))
end


Broker_Everything:Register(L["DUALSPEC"],
	GetItemIcon(7516),
	0,
	ds_onevent,
	ds_events,
	ds_onclick,
	ds_tooltip,
	ds_onevent, -- XXX fixme
	5)

