local LDB
local parent = CreateFrame('Frame')

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 == 63644 or spellID == 63645) 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 OnClick(self, button)
	button = button or "LeftButton"
	
	if button == "LeftButton" then
		if GetNumSpecGroups() < 2 then
			DEFAULT_CHAT_FRAME:AddMessage("|cffffff00BrokerDualSpec:|r  You do not have a second spec.")
			return
		end
		
		if GetActiveSpecGroup() == 1 then
			SetActiveSpecGroup(2)
		elseif GetActiveSpecGroup() == 2 then
			SetActiveSpecGroup(1)
		end
	elseif button == "RightButton" then
		if IsAddOnLoaded("EquipmentBuddy") then
			InterfaceOptionsFrame_OpenToCategory(EquipmentBuddyOptions.panel)
		end
		--ToggleTalentFrame()
	end
end

local function OnTooltipShow(self)
	self:AddLine("|cff00ffffBrokerDualSpec|r")
	self:AddLine(" ")
	local primarySpec = GetSpecialization(false, false, 1)
		if primarySpec ~= nil then
			local id, name, description, icon, background, role = GetSpecializationInfo(primarySpec)
			self:AddDoubleLine("Primary Spec:", ("|T%s:16|t%s"):format(icon, name))
		end
	local secondarySpec = GetSpecialization(false, false, 2)
		if secondarySpec ~= nil then
			local id, name, description, icon, background, role = GetSpecializationInfo(secondarySpec)
			self:AddLine(" ")
			self:AddDoubleLine("Secondary Spec:", ("|T%s:16|t%s"):format(icon, name))
		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(" ")
		self:AddLine(format('|T%s:16|t %s: %s',itemTexture, itemName, GetItemCount(79249)))
		self:AddLine(" ")
	elseif UnitLevel("player") >= 81 and UnitLevel("player") <= 85 then
	local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemSellPrice = GetItemInfo(63388) -- Dust of disappearance
		self:AddLine(" ")
		self:AddLine(format('|T%s:16|t %s: %s',itemTexture, itemName, GetItemCount(63388)))
		self:AddLine(" ")
	else
	local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemSellPrice = GetItemInfo(64670) -- Vanishing powder
		self:AddLine(" ")
		self:AddLine(format('|T%s:16|t %s: %s',itemTexture, itemName, GetItemCount(64670)))
		self:AddLine(" ")
	end
	
	if GetNumSpecGroups() == 2 then
		self:AddLine("<Left Click> to switch specs.")
	end
	if IsAddOnLoaded("EquipmentBuddy") then
		self:AddLine("<Right Click> to open EquipmentBuddy options.")
	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,
		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

parent:SetScript('OnEvent', function(self, event, ...) self[event](self, ...) end)
parent:RegisterEvent('PLAYER_LOGIN')