ADGroupTool = {}

-- Written by M0R_Gaming
--> For AD, by AD

ADGroupTool.name = "ADGroupTool"
ADGroupTool.groupList = {}
ADGroupTool.adgttToggle = false
ADGroupTool.varversion = 1
--[[
ADGroupTool.guildID = 341929

ADGroupTool.offCrownTimerMin = 10

ADGroupTool.offCrownTimer = 60*ADGroupTool.offCrownTimerMin --in seconds

ADGroupTool.inviteToggle = false
ADGroupTool.discord = ""
]]


ADGroupTool.DefaultSettings = {
	guildID = 341929,
	offCrownTimer = 600,
	offCrownTimerMin = 10,
	discordLink = "[Insert Discord Link Here]",
	discordInvite = "Come join us in discord! Even if you don't have a mic, it still helps us coordinate attacks! Come join us at",
	radius = 25500
}


ZO_CreateStringId("SI_BINDING_NAME_ADGROUPTOOL_TOGGLE_SOC", "Toggle [Stay On Crown] Module")
ZO_CreateStringId("SI_BINDING_NAME_ADGROUPTOOL_POST_DISCORD", "Send Discord Message")



-- The range of a forward camp is 25500 units


function ADGroupTool.startTimer()
	if ADGroupTool.adgttToggle then
		EVENT_MANAGER:UnregisterForUpdate("AD Group Tool SOC") --milliseconds
		EVENT_MANAGER:UnregisterForEvent("AD Group Tool SOC DC", EVENT_GROUP_MEMBER_LEFT)
		d("|cFFD700AD Group Tool|r: [Stay On Crown] module |cFF0000deactivated|r.")
		ADGroupTool.adgttToggle = false
		ADGroupTool.groupList = {}
	else
		EVENT_MANAGER:RegisterForUpdate("AD Group Tool SOC", 1000, ADGroupTool.groupTimer) --milliseconds
		EVENT_MANAGER:RegisterForEvent("AD Group Tool SOC DC", EVENT_GROUP_MEMBER_LEFT, ADGroupTool.SOCDC)
		d("|cFFD700AD Group Tool|r: [Stay On Crown] module |c00FF00activated|r.")
		ADGroupTool.adgttToggle = true
	end
end

function ADGroupTool.setTimer(minutes)
	if not minutes then
		d("Please provide a valid amount of time (in minutes)")
	else
		ADGroupTool.vars.offCrownTimerMin = tonumber(minutes)
		ADGroupTool.vars.offCrownTimer = 60*ADGroupTool.vars.offCrownTimerMin
		d("Pugs will be kicked after "..minutes.." minutes of not being on crown")
	end
end
--[[
function ADGroupTool.startInvite(discord)
	if ADGroupTool.inviteToggle then
		EVENT_MANAGER:UnregisterForEvent("AD Group Tool INV", EVENT_GROUP_MEMBER_JOINED)
		ADGroupTool.inviteToggle = false
		d("|cFFD700AD Group Tool|r: [Auto Discord Invite] module |cFF0000deactivated|r.")
	else
		EVENT_MANAGER:RegisterForEvent("AD Group Tool INV", EVENT_GROUP_MEMBER_JOINED, ADGroupTool.InvDiscord)
		ADGroupTool.inviteToggle = true
		ADGroupTool.discord = discord
		d("Discord link set: "..ADGroupTool.discord)
		d("|cFFD700AD Group Tool|r: [Auto Discord Invite] module |c00FF00activated|r.")
	end
end
]]

function ADGroupTool.setDiscord(discord)
	ADGroupTool.vars.discordLink = discord
	d("Discord link set: "..ADGroupTool.vars.discordLink)
end

function ADGroupTool.setDiscordInv(discord)
	ADGroupTool.vars.discordInvite = discord
	d("Discord Invite Message set: "..ADGroupTool.vars.discordInvite)
end

function ADGroupTool.sendDiscord()
	local channel = CHAT_CHANNEL_PARTY
	local target = nil
	local message = ADGroupTool.vars.discordInvite.." "..ADGroupTool.vars.discordLink
	CHAT_SYSTEM:StartTextEntry(message, channel, target)
end

SLASH_COMMANDS["/stayoncrown"] = ADGroupTool.startTimer
SLASH_COMMANDS["/adtimerset"] = ADGroupTool.setTimer
--SLASH_COMMANDS["/addiscord"] = ADGroupTool.startInvite
SLASH_COMMANDS["/addiscord"] = ADGroupTool.sendDiscord
SLASH_COMMANDS["/adsetdiscord"] = ADGroupTool.setDiscord
SLASH_COMMANDS["/adsetdiscordinv"] = ADGroupTool.setDiscordInv







function ADGroupTool.groupTimer()

	local crown = GetGroupLeaderUnitTag()
	local crownWorld,crownX,crownY,crownZ = GetUnitWorldPosition(crown)


	for i = 1,GetGroupSize() do
		local unit = GetGroupUnitTagByIndex(i)
		local displayName = GetUnitDisplayName(unit)


		local unitWorld,unitX,unitY,unitZ = GetUnitWorldPosition(unit)
		local distance = math.sqrt( (crownX-unitX)^2 + (crownZ-unitZ)^2 )

		--d(displayName)
		--d(distance)

		if distance > ADGroupTool.vars.radius then

			if ADGroupTool.groupList[displayName] == nil then
				ADGroupTool.groupList[displayName] = 0
			else
				ADGroupTool.groupList[displayName] = ADGroupTool.groupList[displayName] + 1

				if ADGroupTool.groupList[displayName] > ADGroupTool.vars.offCrownTimer then
					ADGroupTool.groupList[displayName] = nil

					if GetGuildMemberIndexFromDisplayName(ADGroupTool.vars.guildID, displayName) == nil then

						if IsUnitGroupLeader('player') then
							GroupKick(unit)
							d("Kicked user "..displayName.." for not being on crown.")
							break
						else
							d("User "..displayName.." should be kicked for not being on crown.")
							break
						end

					else
						d("Guild Member "..displayName.." has not on crown for more than "..ADGroupTool.vars.offCrownTimerMin.." minutes.")
					end

				end

			end
		else
			ADGroupTool.groupList[displayName] = nil
		end
	end

end

-- Stay on Crown : Disconnect
function ADGroupTool.SOCDC(eventCode, characterName, reason, isLocalPlayer, isLeader, displayName, actionRequiredVote)
	--d("User "..displayName.." has left the group")
	if ADGroupTool.groupList[displayName] ~= nil then
		ADGroupTool.groupList[displayName] = nil
	end
end








--[[
function ADGroupTool.InvDiscord(eventCode, characterName, displayName, isLocalPlayer)
	d("User "..displayName.." has joined the group")
	if GetGuildMemberIndexFromDisplayName(ADGroupTool.guildID, displayName) == nil then
		local channel = CHAT_CHANNEL_PARTY
		local target = nil
		local message = "Come join us in discord! Even if you don't have a mic, it still helps us coordinate attacks! Come join us at "..ADGroupTool.discord
		CHAT_SYSTEM:StartTextEntry(message, channel, target)
		d("A new player has joined the group.")
		d("Press |cFFFFFF[Enter]|r twice to send a discord link.")
	end
end
]]
-- 341929
-- GetGuildMemberIndexFromDisplayName(




--[[


-- the following code was receieved ESOUI's wiki
function ADGroupTool.OnAddOnLoaded(event, addonName)
	d(addonName)
  	if addonName == ADGroupTool.name then
    	ADGroupTool:Initialize()
 	end
end
function ADGroupTool:Initialize(self)
	-- init function (Not nessicary rn)
	d("Init")
	ADGroupTool.vars = ZO_SavedVars:NewAccountWide("ADVars", ADGroupTool.varversion, nil, ADGroupTool.DefaultSettings)
end

-- EVENTS --
-- Finally, we'll register our event handler function to be called when the proper event occurs.
EVENT_MANAGER:RegisterForEvent(ADGroupTool.name, EVENT_ADD_ON_LOADED, ADGroupTool.OnAddOnLoaded)
--EVENT_MANAGER:RegisterForUpdate("AD Group Tool Timer", 1000, ADGroupTool.groupTimer) --milliseconds

]]


-- The following was adapted from https://wiki.esoui.com/Circonians_Stamina_Bar_Tutorial#lua_Structure

-------------------------------------------------------------------------------------------------
--  OnAddOnLoaded  --
-------------------------------------------------------------------------------------------------
function ADGroupTool.OnAddOnLoaded(event, addonName)
	if addonName ~= ADGroupTool.name then return end

	ADGroupTool:Initialize()
end

-------------------------------------------------------------------------------------------------
--  Initialize Function --
-------------------------------------------------------------------------------------------------
function ADGroupTool:Initialize()
	ADGroupTool.vars = ZO_SavedVars:NewAccountWide("ADVars", ADGroupTool.varversion, nil, ADGroupTool.DefaultSettings)

	EVENT_MANAGER:UnregisterForEvent(ADGroupTool.name, EVENT_ADD_ON_LOADED)
end

-------------------------------------------------------------------------------------------------
--  Register Events --
-------------------------------------------------------------------------------------------------
EVENT_MANAGER:RegisterForEvent(ADGroupTool.name, EVENT_ADD_ON_LOADED, ADGroupTool.OnAddOnLoaded)