-- @Author: Cody Smith
-- @Date:   2015-08-11 12:49:48
-- @Last Modified by:   Cody Smith
-- @Last Modified time: 2015-08-11 16:59:13
local LAM2 = LibStub("LibAddonMenu-2.0")

local AGM = {}
AGM.SettingsFile = "AutomatedGuildManagerSettings"
AGM.SAVED_VAR_VERSION = 0.1
AGM.CODE_VERSION = 0.1
AGM.SVProfile = nil

AGM.Defaults = {}
AGM.Defaults.Account = {}
AGM.Defaults.Account.gid = 1
AGM.Defaults.Account.Notes = {}
AMG.ValidNotes = {}
AGM.ValidNotes.Recruited = "Date Recruited"
AGM.Account.ValidRanks = {}


function getSavedNotes(playername)
	--access save table and return table of notes only neccesary for double checking that we have information saved.
	-- do some checking to make sure that our gid hasn't changed first and fix tables around if it did
	local table = {}
	if savedtable[AGM.Account.gid].name == nil then --TODO: finish this. Check guild names and gids against items in db
	end
end

function readNotes(playername)
	local tbl = {}
	local numGuildMembers = GetNumGuildMembers(AGM.Account.gid)
	for i = 1, numGuildMembers do
		local name,note,rankIndex = GetGuildMemberInfo(AGM.Account.gid, i)
		if name == playername then
			tbl = parseNotes(note)
		end
	end
	return tbl
end

function saveNotes(table,playername)
	--save any notes in our table of notes
end

function parseNotes(noteString)
	local tbl = {}
	for k,v in string.gmatch(noteString,"([^[]+):([^]]+)") do
		tbl[k] = v
	end
	return tbl
end

local function dateRecruited(memberID)
	local date = os.time("%x")
	local str = "["..AGM.ValidNotes.Recruited..":"..date"]"
	--get current date and build our string to add to the notes.
	return str
end

local function onMemberAdd(eventCode, guildId, DisplayName,vald)
	d(eventCode.."  :  "..guildId.."  :  "..DisplayName.."  :  "..vald)
	if guildId == AGM.Account.gid then
		local notes = readNotes(DisplayName)
		if notes ~= "" then
			--operate on notes
		else
			notes = dateRecruited(guildId)
			d(notes)
		end
	end
end

function setGuild(name)
	local gid = false
	for g=1, #AGM.Account.Guilds do
		if name == AGM.Account.Guilds[i] then
			AGM.Account.gid = i
		end
	end
end

function listGuilds()
	local tbl
	for i in GetNumGuilds() do
		local name = GetGuildName(i)
		table.insert(tbl,name)
	end
	AGM.Account.Guilds = tbl
	return tbl
end

function populateRanks()
	local gid = AGM.Account.gid
	local numRanks = GetNumGuildRanks(gid)
	local tbl = {}
	for i=1, numRanks do
		rname = GetGuildRankCustomName(gid,i)
		tbl[i] = rname
	end
	AGM.Account.ValidRanks = tbl
end

function getGuildMemberIndexByName(displayName)
	local num = GetNumGuildMembers(AGM.Account.gid)
	for i = 1, num do
		local name,note,rankIndex = GetGuildMemberInfo(i)
		if name == displayName then
			return i
		end
	end
end

function addNote()
	--validate that this note is not already there then add
end

---------------------LAM 2.0 SETTINGS--------------------------

local function CreateSettingsMenu()
	local colorYellow = "|cFFFF22"

	local panelData = {
		type = "panel",
		name = "Automated Guild Manager",
		displayName = colorYellow.."Talen-Shei's Automated Guild Manager",
		author = "Talen-Shei",
		version = AGM.CODE_VERSION,
		slashCommand = "/AGM",
		registerForRefresh = true,
		registerForDefaults = true,
	}

	local cntrlOptionsPanel = LAM2:RegisterAddonPanel("AGM_Options", panelData)

	optionsData = {
		[1] = {
			type = "header",
			name = colorYellow.."Loading",
			width = "full",
		},
		[2] = {
			type = "description",
			text = "A Tool to manage guild notes and automated deletion.",
		},
		[3] = {
			type = "dropdown",
			name = "Guild",
			choices = AGM.Account.Guilds,
			tooltip = "Guild to manage",
			sort = "name-up",
			reference = "AGMGuild",
			default = AGM.Account.Guilds[AGM.Account.gid],
			getFunc = function() local Guild = AGM.Accounts.Guilds[AGM.Account.gid] return Guild end,
			setFunc = function(bValue) setGuild(bValue) end,
			 -- KBS.Character.BindingTarget = bValue end,
		},
	}

	LAM2:RegisterOptionControls("AGM_Options", optionsData)
end


local function Initialize()
	--set up settings files and such
	AGM.Account = ZO_SavedVars:NewAccountWide(AGM.SettingsFile, AGM.SAVED_VAR_VERSION, nil, AGM.Defaults.Account, AGM.SVProfile)
end

local function OnLoad(event,addon)
	if addon ~= "AutomatedGuildManager" then return end
	Initialize()
	CreateSettingsMenu()
	EVENT_MANAGER:RegisterForEvent(EVENT_GUILD_MEMBER_ADDED, onMemberAdd)
end


EVENT_MANAGER:RegisterForEvent("AutomatedGuildManager", EVENT_ADD_ON_LOADED, OnLoad)