local LDB
local parent = CreateFrame('Frame')

 -- Get a reference to the lib
local LibQTip = LibStub('LibQTip-1.0')
local tooltip = nil

local spam1 = ERR_LEARN_ABILITY_S:gsub("%.", "%."):gsub("%%s", "(.*)")
local spam2 = ERR_LEARN_SPELL_S:gsub("%.", "%."):gsub("%%s", "(.*)")
local spam3 = ERR_SPELL_UNLEARNED_S:gsub("%.", "%."):gsub("%%s", "(.*)")
local spam4 = ERR_LEARN_PASSIVE_S:gsub("%.", "%."):gsub("%%s", "(.*)")
local spam5 = ERR_PET_LEARN_ABILITY_S:gsub("%.", "%."):gsub("%%s", "(.*)")
local spam6 = ERR_PET_LEARN_SPELL_S:gsub("%.", "%."):gsub("%%s", "(.*)")
local spam7 = ERR_PET_SPELL_UNLEARNED_S:gsub("%.", "%."):gsub("%%s", "(.*)")

local start = 0

local function SpamFilter(self, event, msg)
	if (start > GetTime() - 8) and (strfind(msg, spam1) or strfind(msg, spam2) or strfind(msg, spam3) or strfind(msg, spam4) or strfind(msg, spam5) or strfind(msg, spam6) or strfind(msg, spam7)) then return true end
end

local function SpamCheck(self, ...)
local unit, _, _, _, spellID = ...
	if unit == "player" and spellID == 200749 then
		start = GetTime()
	end
end

local function UpdateDisplay()
		local currentSpec = GetSpecialization()
		if currentSpec ~= nil then
			local id, name, description, icon, background, role = GetSpecializationInfo(currentSpec)
			LDB.text = name
			LDB.icon = icon
		else
			LDB.icon = "Interface\\Icons\\INV_Misc_QuestionMark.blp"
			LDB.text = "None"
		end
end	

local function ModifiedClick(button, index)
local id, name, description, icon, background, role = GetSpecializationInfo(index)
local currentSpec = GetSpecialization()

if currentSpec == index then
	DEFAULT_CHAT_FRAME:AddMessage("|cffffff00BrokerDualSpec:|r  Please choose a different spec from the tooltip window.")
else
	SetSpecialization(index)
	LibQTip:Release(tooltip)
	tooltip = nil
end
end

local function OnEnter(self)	
   -- Acquire a tooltip with 1 columns, aligned to left
tooltip = LibQTip:Acquire("BrokerDualSpecTooltip", 1, "LEFT") 
self.tooltip = tooltip
tooltip:EnableMouse(true)
tooltip:SetAutoHideDelay(.2, self)
  
   -- Use smart anchoring code to anchor the tooltip to our frame
tooltip:SmartAnchorTo(self)
tooltip:Clear()   

tooltip:AddHeader("|cff00ffffBrokerDualSpec|r")
--tooltip:AddSeparator()
--tooltip:AddLine(" ")
local numspecs = GetNumSpecializations()
local numlines = 1
	
	for i = 1, numspecs do
			local id, name, description, icon, background, role = GetSpecializationInfo(i)
			numlines = numlines + 1
			tooltip:AddLine(("|T%s:16|t%s"):format(icon, name))
			tooltip:SetCellScript(numlines, 1, "OnMouseUp", ModifiedClick, i)
	
	end
	--[[if UnitLevel("player") >= 86 then
	local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemSellPrice = GetItemInfo(79249) -- Tome of the Clear Mind
		self:AddLine(" ")
		if itemName then
		self:AddLine(format('|T%s:16|t %s: %s',itemTexture, itemName, GetItemCount(79249)))
		end
		self:AddLine(" ")
	end]]
		tooltip:AddSeparator()
		tooltip:AddLine("|cffffff00<Left Click>|r spec to activate.")
		tooltip:AddLine("|cffffff00<Left Click>|r plugin to toggle talent frame.")
	if IsAddOnLoaded("EquipmentBuddy") then
		tooltip:AddLine("|cffffff00<Right Click>|r plugin to open EquipmentBuddy options.")
	end
	
	 -- Show it
	--if tooltip and tooltip:IsShown() then
		--LibQTip:Release(self.tooltip)
		--self.tooltip = nil
	--else
		tooltip:Show()
	--end
end

--local function OnLeave(self)  
   -- Release the tooltip
   --LibQTip:Release(self.tooltip)
   --self.tooltip = nil 
   
--end

local function OnClick(self, button)
	button = button or "LeftButton"
	
	if button == "LeftButton" then
	ToggleTalentFrame(2)
	--TalentFrame_LoadUI();
	--ShowUIPanel(PlayerTalentFrame)
	--DEFAULT_CHAT_FRAME:AddMessage("|cffffff00BrokerDualSpec:|r  Please choose spec from the tooltip window.")
	elseif button == "RightButton" then
		if IsAddOnLoaded("EquipmentBuddy") then
			InterfaceOptionsFrame_OpenToCategory(EquipmentBuddyOptions.panel)
			InterfaceOptionsFrame_OpenToCategory(EquipmentBuddyOptions.panel)-- call twice since first call doesn't open
		end
		
	end
end

function parent:PLAYER_LOGIN()
	LDB = LibStub('LibDataBroker-1.1'):NewDataObject('BrokerDualSpec', {
		icon = "Interface\\Icons\\INV_Misc_QuestionMark.blp",
		text = "BrokerDualSpec",
		type = 'data source',
		--OnTooltipShow = OnTooltipShow,
		OnEnter = OnEnter,
		--OnLeave = OnLeave,
		OnClick = OnClick,
	})
	self:RegisterEvent('ACTIVE_TALENT_GROUP_CHANGED')
	self.ACTIVE_TALENT_GROUP_CHANGED = UpdateDisplay
	self:RegisterEvent('PLAYER_SPECIALIZATION_CHANGED')
	self.PLAYER_SPECIALIZATION_CHANGED = UpdateDisplay
	self:RegisterEvent('UNIT_SPELLCAST_START')
	self.UNIT_SPELLCAST_START = SpamCheck
	
	UpdateDisplay()
	ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", SpamFilter)
end
 
 -- Somewhere in the anchor initialization
parent:SetScript('OnEnter', OnEnter)
parent:SetScript('OnLeave', OnLeave)
parent:SetScript('OnEvent', function(self, event, ...) self[event](self, ...) end)
parent:RegisterEvent('PLAYER_LOGIN')