--CHAT_MSG_SAY CHAT_MSG_YELL CHAT_MSG_WHISPER ADD_KEYWORD REMOVE_KEYWORD LIST_KEYWORDS TOGGLE_MAGUS CHAT_MSG_SYSTEM COMBAT_LOG_EVENT_UNFILTERED TRADE_SHOW TRADE_MONEY_CHANGED PLAYER_TRADE_MONEY TRADE_ACCEPT_UPDATE TRADE_CLOSED
function(e, msg, sender, ...)
	local function updateText()
		local texts = {[1] = {}, [2] = {}, [3] = {}, [4] = {}}
		for k,v in pairs(aura_env.people) do
			if v.state == 5 then
				if v.kw == "uc" or v.kw == "under" or v.kw == "undercity" then
					texts[1][#texts[1] + 1] = k
				elseif v.kw == "org" or v.kw == "og" or v.kw == "orgrimmar" then
					texts[2][#texts[2] + 1] = k
				elseif v.kw == "tb" or v.kw == "thunder" or v.kw == "thunderbluff" then
					texts[3][#texts[3] + 1] = k
				elseif v.kw == "test" then
					texts[4][#texts[4] + 1] = k
				end
			end
		end
		for k,v in pairs(texts) do
			local text = ""
			for k2,v2 in pairs(v) do
				text = text .. v2 .. "\n"
			end
			WeakAurasSaved.MagusKeywords.buttons[k].text:SetText(text)
		end
	end

	local aura_env = aura_env
	if not aura_env.toggle then aura_env.toggle = 1 end
	if not WeakAurasSaved.MagusKeywords then WeakAurasSaved.MagusKeywords = {
		["port"] = 1,
		["portal"] = 1,
		["org"] = 1,
		["og"] = 1,
		["uc"] = 1,
		["under"] = 1,
		["thunder"] = 1,
		["tb"] = 1,
		["wtb"] = 1,
		["test"] = 1,
	} end
	if e == "CHAT_MSG_SAY" or e == "CHAT_MSG_YELL" or e == "CHAT_MSG_WHISPER" then
		local class = select(2, GetPlayerInfoByGUID(select(10, ...)))
		if msg and aura_env.toggle == 1 then
			msg = msg:lower()
			local noRealmSender = sender:match("(.+)-?%w*") or sender
			for k,v in pairs(WeakAurasSaved.MagusKeywords) do
				if msg:match(k) then
					local st, en = msg:find(k)
					if not (st > 1 and msg:match(".", st - 1) ~= " ") and not (en < strlen(msg) and msg:match(".", en + 1) ~= " ") and class ~= "MAGE" then
						InviteUnit(sender)
						if not aura_env.people[noRealmSender] then aura_env.people[noRealmSender] = {["state"] = 0, ["kw"] = k} end
						return true
					end
				end
			end
		end
	elseif e == "CHAT_MSG_SYSTEM" then
		if msg:match("You have invited") then
			local sender = msg:match("You have invited (.+) to join your group")
			local noRealmSender = sender:match("(.+)-%w+") or sender
			if aura_env.people[noRealmSender] then
				aura_env.people[noRealmSender].state = 0
			end
		elseif msg:match("joins the party") then
			local sender = msg:match("(.+) joins the party")
			local noRealmSender = sender:match("(.+)-%w+") or sender
			if aura_env.people[noRealmSender] and aura_env.people[noRealmSender].state == 0 then
				aura_env.people[noRealmSender].state = 1
				for i = 1, GetNumGroupMembers() do
					if UnitName("party" .. i) == sender then
						aura_env.people[noRealmSender].unitID = "party" .. i
						break
					end
				end
				--If not tradeable create ticker and do other stuff
				if CheckInteractDistance(aura_env.people[noRealmSender].unitID, 2) then
					InitiateTrade(aura_env.people[noRealmSender].unitID)
					aura_env.people[noRealmSender].state = 2
				end
			end
		elseif msg:match("leaves the party") then
			local sender = msg:match("(.+) leaves the party")
			local noRealmSender = sender:match("(.+)-%w+") or sender
			if aura_env.people[noRealmSender] and aura_env[noRealmSender].state == 6 then
				aura_env.people[noRealmSender] = nil
			end
		end
	elseif e == "TRADE_SHOW" then
		for k,v in pairs(aura_env.people) do
			if v.state == 2 then
				aura_env.isTrade = true
				aura_env.tradee = k
				aura_env.people[k].state = 3
			end
		end
	elseif e == "TRADE_ACCEPT_UPDATE" then
		if msg == 1 and sender == 1 then
			if aura_env.isTrade == true then
				aura_env.people[aura_env.tradee].state = 4
				if aura_env.people[aura_env.tradee].kw == "uc" or aura_env.people[aura_env.tradee].kw == "under" or aura_env.people[aura_env.tradee].kw == "undercity" then
					ActionButton_ShowOverlayGlow(WeakAurasSaved.MagusKeywords.buttons[3])
					aura_env.castQueue["Portal: Undercity"] = true
					aura_env.people[aura_env.tradee]["Portal: Undercity"] = true
					aura_env.people[aura_env.tradee].state = 5
				elseif aura_env.people[aura_env.tradee].kw == "org" or aura_env.people[aura_env.tradee].kw == "og" or aura_env.people[aura_env.tradee].kw == "orgrimmar" then
					ActionButton_ShowOverlayGlow(WeakAurasSaved.MagusKeywords.buttons[2])
					aura_env.castQueue["Portal: Orgrimmar"] = true
					aura_env.people[aura_env.tradee]["Portal: Orgrimmar"] = true
					aura_env.people[aura_env.tradee].state = 5
				elseif aura_env.people[aura_env.tradee].kw == "tb" or aura_env.people[aura_env.tradee].kw == "thunder" or aura_env.people[aura_env.tradee].kw == "thunderbluff" then
					ActionButton_ShowOverlayGlow(WeakAurasSaved.MagusKeywords.buttons[1])
					aura_env.castQueue["Portal: Thunder Bluff"] = true
					aura_env.people[aura_env.tradee]["Portal: Thunder Bluff"] = true
					aura_env.people[aura_env.tradee].state = 5
				elseif aura_env.people[aura_env.tradee].kw == "test" then
					ActionButton_ShowOverlayGlow(WeakAurasSaved.MagusKeywords.buttons[4])
					aura_env.castQueue["Consecration"] = true
					aura_env.people[aura_env.tradee]["Consecration"] = true
					aura_env.people[aura_env.tradee].state = 5
				end
				aura_env.isTrade = false
				aura_env.tradee = ""
				updateText()
			end
		end
	elseif e == "TRADE_CLOSED" then
		if aura_env.isTrade == true then
			aura_env.people[aura_env.tradee].state = 2
			aura_env.isTrade = false
			aura_env.tradee = ""
		end
	elseif e == "COMBAT_LOG_EVENT_UNFILTERED" then
		local se = sender
		local caster = select(3, ...)
		if se == "SPELL_CAST_SUCCESS" and caster == UnitName("player") then
			local spell = select(11, ...)
			if aura_env.castQueue[spell] == true then aura_env.castQueue[spell] = false end
			if spell == "Portal: Undercity" then
				ActionButton_HideOverlayGlow(WeakAurasSaved.MagusKeywords.buttons[3])
			elseif spell == "Portal: Orgrimmar" then
				ActionButton_HideOverlayGlow(WeakAurasSaved.MagusKeywords.buttons[2])
			elseif spell == "Portal: Thunder Bluff" then
				ActionButton_HideOverlayGlow(WeakAurasSaved.MagusKeywords.buttons[1])
			elseif spell == "Consecration" then
				ActionButton_HideOverlayGlow(WeakAurasSaved.MagusKeywords.buttons[4])
			end
			for k,v in pairs(aura_env.people) do
				if v[spell] and v.state == 5 then
					v[spell] = false
					aura_env.people[k] = nil
					updateText()
				end
			end
		end
	elseif e == "TOGGLE_MAGUS" then
		if aura_env.toggle == 0 then
			print("Toggling script on")
			aura_env.toggle = 1
			for i = 1, 3 do
				WeakAurasSaved.MagusKeywords.buttons[i]:Show()
			end
		else
			print("Toggling script off")
			aura_env.toggle = 0
			for i = 1, 3 do
				WeakAurasSaved.MagusKeywords.buttons[i]:Hide()
			end
		end
	elseif e == "ADD_KEYWORD" then
		if msg then
			print("Adding", msg)
			msg = msg:lower()
			WeakAurasSaved.MagusKeywords[msg] = 1
		else
			print("No keyword provided")
		end
	elseif e == "LIST_KEYWORDS" then
		local test = ""
		for k,v in pairs(WeakAurasSaved.MagusKeywords) do
			print(k)
			test = test .. k .. "\n"
		end
		message(test)
	elseif e == "REMOVE_KEYWORD" then
		if msg then
			if WeakAurasSaved.MagusKeywords[msg] then
				print("Removing", msg)
				WeakAurasSaved.MagusKeywords[msg] = nil
			else
				print(msg, "does not exist in the keyword table")
			end
		else
			print("No keyword provided")
		end
	end
end

--INIT
if not aura_env.toggle then aura_env.toggle = 1 end
if not WeakAurasSaved.MagusKeywords then WeakAurasSaved.MagusKeywords = {
	["port"] = 1,
	["portal"] = 1,
	["org"] = 1,
	["og"] = 1,
	["uc"] = 1,
	["under"] = 1,
	["thunder"] = 1,
	["tb"] = 1,
	["wtb"] = 1, 
} end

--[[
Player state table
0 - invited
1 - joined
2 - trade queue
3 - trading
4 - trade complete
5 - light up portal
6 - portal cast
7 - left? clean table

Cancel everything if person leave
]]
aura_env.people = {}
aura_env.castQueue = {}
aura_env.isTrade = false
aura_env.tradee = ""

--[[local function hideButon(self)
	ActionButton_HideOverlayGlow(self)
end--]]

if not WeakAurasSaved.MagusKeywords.buttons or not WeakAurasSaved.MagusKeywords.buttons[1][0] then WeakAurasSaved.MagusKeywords.buttons = {
	[1] = CreateFrame("Button", "buton", aura_env.region, "SecureActionButtonTemplate"),
	[2] = CreateFrame("Button", "buton", aura_env.region, "SecureActionButtonTemplate"),
	[3] = CreateFrame("Button", "buton", aura_env.region, "SecureActionButtonTemplate"),
	[4] = CreateFrame("Button", "buton", aura_env.region, "SecureActionButtonTemplate"),
} end

for i = 1, 4 do
	WeakAurasSaved.MagusKeywords.buttons[i]:SetAttribute("type", "macro")
	WeakAurasSaved.MagusKeywords.buttons[i]:SetSize(aura_env.config.size, aura_env.config.size)
	WeakAurasSaved.MagusKeywords.buttons[i]:SetPoint("CENTER", aura_env.region, "CENTER", (i - 1) * aura_env.config.size, 0)
	WeakAurasSaved.MagusKeywords.buttons[i]:RegisterForClicks("LeftButtonDown")
	--WeakAurasSaved.MagusKeywords.buttons[i]:SetScript("PreClick", hideButon)
	WeakAurasSaved.MagusKeywords.buttons[i].text = WeakAurasSaved.MagusKeywords.buttons[i]:CreateFontString("text", "ARTWORK")
	WeakAurasSaved.MagusKeywords.buttons[i].text:SetFont("Fonts\\FRIZQT__.TTF", 14, "OUTLINE, MONOCHROME")
	WeakAurasSaved.MagusKeywords.buttons[i].text:SetPoint("CENTER", WeakAurasSaved.MagusKeywords.buttons[i], "CENTER", 0, -aura_env.config.size / 2 - 14)
	WeakAurasSaved.MagusKeywords.buttons[i].text:Show()
	WeakAurasSaved.MagusKeywords.buttons[i]:Show()
end

WeakAurasSaved.MagusKeywords.buttons[1]:SetNormalTexture(135750)
WeakAurasSaved.MagusKeywords.buttons[1]:SetAttribute("macrotext", format("/cast Portal: Thunder Bluff"))
WeakAurasSaved.MagusKeywords.buttons[2]:SetNormalTexture(135744)
WeakAurasSaved.MagusKeywords.buttons[2]:SetAttribute("macrotext", format("/cast Portal: Orgrimmar"))
WeakAurasSaved.MagusKeywords.buttons[3]:SetNormalTexture(135751)
WeakAurasSaved.MagusKeywords.buttons[3]:SetAttribute("macrotext", format("/cast Portal: Undercity"))
WeakAurasSaved.MagusKeywords.buttons[4]:SetNormalTexture(135926)
WeakAurasSaved.MagusKeywords.buttons[4]:SetAttribute("macrotext", format("/cast Consecration"))

aura_env.printTable = function(table, n)
	if not n then n = 0 end
	for k,v in pairs(table) do
		local printText = ""
		for i = 1, n do
			printText = printText .. "    "
		end
		printText = printText .. "[" .. k .. "] = " .. tostring(v)
		print(printText)
		if type(v) == "table" then
			n = n + 1
			aura_env.printTable(v, n)
		end
	end
end