--COMBAT_LOG_EVENT_UNFILTERED ADD_BUFF REMOVE_BUFF
--https://pastebin.com/4xyHRftN
function(e, ...)
	if e == "COMBAT_LOG_EVENT_UNFILTERED" then
		local se = select(2, ...)
		if se == "SPELL_AURA_APPLIED" or se == "SPELL_AURA_REMOVED" then
			aura_env.scanBuffs()
		end
	elseif e == "ADD_BUFF" then
		local msg = select(1, ...)
		local arg = select(2, ...)
		if msg then
			if not WeakAurasSaved.CustomTrash.BuffButtons.Buffs[msg] then 
				WeakAurasSaved.CustomTrash.BuffButtons.Buffs[msg] = {["active"] = false} 
				if arg then
					WeakAurasSaved.CustomTrash.BuffButtons.Buffs[msg].group = arg
					if not WeakAurasSaved.CustomTrash.BuffButtons.Groups[arg] then WeakAurasSaved.CustomTrash.BuffButtons.Groups[arg] = {["active"] = 0, ["shown"] = true} end
				end
				print(msg, "added")
			else
				print(msg, "already exists")
			end
		end
		aura_env.scanBuffs()
	elseif e == "REMOVE_BUFF" then
		local msg = select(1, ...)
		if msg and WeakAurasSaved.CustomTrash.BuffButtons.Buffs[msg] then
			WeakAurasSaved.CustomTrash.BuffButtons.Buffs[msg] = nil
			print(msg, "removed")
		else
			print(msg, WeakAurasSaved.CustomTrash.BuffButtons.Buffs[msg])
			print("Cannot remove", msg)
		end
		aura_env.scanBuffs()
	end
end

--INIT
if not WeakAurasSaved.CustomTrash then WeakAurasSaved.CustomTrash = {} end
if not WeakAurasSaved.CustomTrash.BuffButtons then WeakAurasSaved.CustomTrash.BuffButtons = {} end
if not WeakAurasSaved.CustomTrash.BuffButtons.Buffs then WeakAurasSaved.CustomTrash.BuffButtons.Buffs = {} end
if not WeakAurasSaved.CustomTrash.BuffButtons.Buttons then WeakAurasSaved.CustomTrash.BuffButtons.Buttons = {} end
if not WeakAurasSaved.CustomTrash.BuffButtons.Groups then WeakAurasSaved.CustomTrash.BuffButtons.Groups = {} end
aura_env.buttons = 0

aura_env.scanBuffs = function()
	for k,v in pairs(WeakAurasSaved.CustomTrash.BuffButtons.Buffs) do
		v.active = false
	end
	for i = 1, 40 do
		local name = UnitBuff("player", i)
		print(name)
		if WeakAurasSaved.CustomTrash.BuffButtons.Buffs[name] then
			WeakAurasSaved.CustomTrash.BuffButtons.Buffs[name].active = true
		end
		if not name then break end
	end
	aura_env.refreshButtons()
end

aura_env.resortButtons = function()
	aura_env.buttons = 0
	for k,v in pairs(WeakAurasSaved.CustomTrash.BuffButtons.Buttons) do
		if v:IsShown() == true then
			if not WeakAurasSaved.CustomTrash.BuffButtons.Buffs[k] then
				v:Hide()
			else
				v:SetPoint("CENTER", aura_env.region, "CENTER", aura_env.buttons * aura_env.config.size, 0)
				aura_env.buttons = aura_env.buttons + 1
			end
		end
	end
end

aura_env.refreshButtons = function()
	for k,v in pairs(WeakAurasSaved.CustomTrash.BuffButtons.Buffs) do
		if not WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k] or not WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k][0] then
			WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k] = CreateFrame("Button", "buton", aura_env.region, "SecureActionButtonTemplate")
			WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:SetAttribute("type", "macro")
			WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:RegisterForClicks("LeftButtonDown")
			WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:Show()
			WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:SetPoint("CENTER", aura_env.region, "CENTER", aura_env.buttons * aura_env.config.size, 0)
		end
		WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:SetAttribute("macrotext", format("/use %s", k))
		WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:SetSize(aura_env.config.size, aura_env.config.size)
	end
	for k,v in pairs(WeakAurasSaved.CustomTrash.BuffButtons.Buffs) do
		if v.active == false then
			local texture = GetItemIcon(k) or GetSpellTexture(k)
			if not GetSpellTexture(k) then
				local count = GetItemCount(k)
				if not WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k].Text then WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k].Text = WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:CreateFontString("ItemCount", "ARTWORK") end
				WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k].Text:SetFont("Fonts\\FRIZQT__.TTF", aura_env.config.size / 2, "OUTLINE, MONOCHROME")
				WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k].Text:SetPoint("CENTER", WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k], "CENTER")
				WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k].Text:SetText(count)
				WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k].Text:Show()
			end
			WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:SetNormalTexture(texture)
			WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:Show()
			ActionButton_ShowOverlayGlow(WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k])
		else
			WeakAurasSaved.CustomTrash.BuffButtons.Buttons[k]:Hide()
		end
	end
	aura_env.resortButtons()
end

for k,v in pairs(WeakAurasSaved.CustomTrash.BuffButtons.Buttons) do
	if k[0] then
		print(k,v)
		v:SetSize(aura_env.config.size, aura_env.config.size)
		ActionButton_HideOverlayGlow(k)
	end
end
aura_env.scanBuffs()


--INIT
local xsize, ysize = aura_env.region:GetSize()
if not aura_env.button then aura_env.button = CreateFrame("Button", "buton", aura_env.region, "SecureActionButtonTemplate") end
if aura_env.button then
	aura_env.button:SetPoint("CENTER", aura_env.region, "CENTER")
	aura_env.button:SetSize(xsize, ysize)
	aura_env.button:SetAttribute("type", "macro")
	aura_env.button:RegisterForClicks("LeftButtonDown")
	aura_env.button:SetAttribute("macrotext", format("/use %s", aura_env.id))
end