--[[
  * AutoInvite
  * Author: Sasky
]]--

local AutoInvite = {}
AutoInvite.watch = ""
AutoInvite.AddonId = "AutoInvite"
AutoInvite.listening = false
AutoInvite.maxSize = 24

--Main interaction switch
SLASH_COMMANDS["/ai"] = function(str)
	if not str or str == "" or str == "help" then
		if not AutoInvite.listening or str == "help" then
			AutoInvite.help()
			return
		end
		d("Disabling AutoInvite")
		AutoInvite.disable()
		return
	end
	AutoInvite.enable(str)
end

--Main callback - sends invites
AutoInvite.callback = function(eventCode, messageType, from, message)
	if not AutoInvite.listening then
		d("WARN: AutoInvite not listening properly. Please reset. ('/ai help' for commands)")
		return
	end

	if GetGroupSize() >= AutoInvite.maxSize then
		d("Group full. Disabling AutoInvite")
		AutoInvite.disable()
	end

	if string.lower(message) == AutoInvite.watch and from ~= nil and from ~= "" then
		if (messageType >= CHAT_CHANNEL_GUILD_1 and messageType <= CHAT_CHANNEL_OFFICER_5) then
			from = AutoInvite.guildLookup(messageType, from)
			if from == "" then return end
		end

		from = from:gsub("%^.+", "")
		d("Sending invite to " .. from)
		GroupInvite(from)
		GroupInviteByName(from)
	end

	--d("Checking message '" .. string.lower(message) .."' ?= '" .. AutoInvite.watch .."'")
end

AutoInvite.guildLookup = function(channel, acctName)
	local guildId = 0
	if channel == CHAT_CHANNEL_GUILD_1 or channel == CHAT_CHANNEL_OFFICER_1 then guildId = GetGuildId(1) end
	if channel == CHAT_CHANNEL_GUILD_2 or channel == CHAT_CHANNEL_OFFICER_2 then guildId = GetGuildId(2) end
	if channel == CHAT_CHANNEL_GUILD_3 or channel == CHAT_CHANNEL_OFFICER_3 then guildId = GetGuildId(3) end
	if channel == CHAT_CHANNEL_GUILD_4 or channel == CHAT_CHANNEL_OFFICER_4 then guildId = GetGuildId(4) end
	if channel == CHAT_CHANNEL_GUILD_5 or channel == CHAT_CHANNEL_OFFICER_5 then guildId = GetGuildId(5) end

	if guildId == 0 then d("Error - couldn't invite on channel: " .. channel) end

	local aName
	for i=1,GetNumGuildMembers(guildId) do
		aName = GetGuildMemberInfo(guildId,i)
		if aName == acctName then
			hasChar, charName, zone = GetGuildMemberCharacterInfo(guildId,i)
			-- Might use zone check to NOT auto-invite people outside current zone if leader in Cyrodiil
			if hasChar then
				return charName:gsub("%^.+", "")
			else
				d("Could not find player name for " .. acctName .. ". Please manually invite.")
				return ""
			end
		end
	end
end

--Stop listening
AutoInvite.disable = function()
	AutoInvite.watch = ""
	EVENT_MANAGER:UnregisterForEvent(AutoInvite.AddonId, EVENT_CHAT_MESSAGE_CHANNEL)
	AutoInvite.listening = false
end

AutoInvite.enable = function(str)
	AutoInvite.watch = string.lower(str)
	if not AutoInvite.listening then
		--Add handler
		EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_CHAT_MESSAGE_CHANNEL, AutoInvite.callback)
		AutoInvite.listening = true
	end

	d("AutoInvite set on string '" .. AutoInvite.watch .. "'")
end

AutoInvite.help = function()
	d("AutoInvite - command '/ai <str>'. Usage")
	d("  '/ai foo' - autoInvite on 'foo' command'")
	d("  '/ai help' - show this menu")
	d("  '/ai' - turn off autoInvite (auto on group full)")
	return
end