local toc, privateData = ...
local AddonId = toc.identifier

EM = {}
EM.Event = {}
EM.Event.Trigger = {}
EM.Mail = {}
EM.Mail.List = {}
EM.UI = {}
EM.UI.Created = false
EM.UI.Callback = {}
EM.UI.Setup = {}
EM.Messages = {}
EM.Temp = {}
EM.Context = {}
EM.Created = { Inbox = false, Send = false, Archive = false, Settings = false }

EM.Context.Main = UI.CreateContext("emContextMain")

EM.PlayerName = Inspect.Unit.Detail('player').name .. "@" .. Inspect.Shard().name

EM.Event.Trigger.RefreshMail, EM.Event.RefreshMail = Utility.Event.Create(AddonId, "RefreshMail")
EM.Event.Trigger.UICreated, EM.Event.UICreated = Utility.Event.Create(AddonId, "UICreated")

function EM.UI.Create()

	local window = UI.CreateFrame("RiftWindow", "emMailWindow", EM.Context.Main)
		window:SetPoint("TOPLEFT", UI.Native.Mail, "TOPLEFT", 0, -8)
		window:SetWidth(456)
		window:SetHeight(597)
		window:SetLayer(5)
		window:SetTitle("MAIL")
		window:SetVisible(true)
		window.leftDown = false
		window:EventAttach(Event.UI.Input.Mouse.Left.Down.Dive, function(handle)
			if handle:GetName() == "emMailWindowBorder" then
				window.leftDown = true
				local mouse = Inspect.Mouse()
				local top = window:GetTop()
				local left = window:GetLeft()
				window.x = ((mouse.x - left)/window:GetWidth())
				window.y = ((mouse.y - top)/window:GetHeight())
			end
		end, "LeftDown")
		window:EventAttach(Event.UI.Input.Mouse.Left.Up.Dive, function(handle)
			if handle:GetName() == "emMailWindowBorder" then
				window.leftDown = false
			end
		end, "LeftUp")
		window:EventAttach(Event.UI.Input.Mouse.Left.Upoutside.Dive, function(handle)
			if handle:GetName() == "emMailWindowBorder" then
				window.leftDown = false
			end
		end, "LeftUpoutside")

	window.Close = UI.CreateFrame("RiftButton", "Mail_Close", window)
		window.Close:SetPoint("TOPRIGHT", window, "TOPRIGHT", -8, 15)
		window.Close:SetSkin("close")
		window.Close:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			window:SetVisible(false)
			if EM.UI.Contacts.Window then
				EM.UI.Contacts.Window:SetVisible(false)
			end
			for child, st in pairs(EM.Context.Inventory:GetChildren()) do
				child:SetVisible(false)
			end
		end, "LeftClick")

	EM.UI.MailWindow = window
	EM.Context.Main:SetStrata("tutorial")

	EM.UI.Display = UI.CreateFrame("Frame", "emMailWindowTabs", EM.UI.MailWindow)
		EM.UI.Display:SetPoint("TOPLEFT", EM.UI.MailWindow, "TOPLEFT", 14, 58)
		EM.UI.Display:SetWidth(428)
		EM.UI.Display:SetHeight(526)
		EM.UI.Display:SetLayer(10)

		EM.UI.Display.Tabs = UI.CreateFrame("Frame", "emMailWindowTabs_Tabs", EM.UI.Display)
			EM.UI.Display.Tabs:SetPoint("TOPLEFT", EM.UI.Display, "TOPLEFT")
			EM.UI.Display.Tabs:SetWidth(428)
			EM.UI.Display.Tabs:SetHeight(29)
			EM.UI.Display.Tabs:SetLayer(15)

			EM.UI.Display.Tabs.Inbox = UI.CreateFrame("Frame", "emMailWindowTabs_Tabs_Inbox", EM.UI.Display.Tabs)
				EM.UI.Display.Tabs.Inbox:SetPoint("BOTTOMLEFT", EM.UI.Display.Tabs, "BOTTOMLEFT", 2, 0)
				EM.UI.Display.Tabs.Inbox:SetWidth(104)
				EM.UI.Display.Tabs.Inbox:SetHeight(29)
				EM.UI.Display.Tabs.Inbox:SetLayer(20)
				EM.UI.Display.Tabs.Inbox.Name = "Inbox"

				EM.UI.Display.Tabs.Inbox.Texture = UI.CreateFrame("Texture", "emMailWindowTabs_Tabs_Inbox_Texture", EM.UI.Display.Tabs.Inbox)
					EM.UI.Display.Tabs.Inbox.Texture:SetPoint("TOPLEFT", EM.UI.Display.Tabs.Inbox, "TOPLEFT")
					EM.UI.Display.Tabs.Inbox.Texture:SetPoint("BOTTOMRIGHT", EM.UI.Display.Tabs.Inbox, "BOTTOMRIGHT")
					EM.UI.Display.Tabs.Inbox.Texture:SetTexture(AddonId, "gfx/tabPaneHorizActive.png")
					EM.UI.Display.Tabs.Inbox.Texture:SetLayer(25)

				EM.UI.Display.Tabs.Inbox.Text = UI.CreateFrame("Text", "emMailWindowTabs_Tabs_Inbox_Text", EM.UI.Display.Tabs.Inbox)
					EM.UI.Display.Tabs.Inbox.Text:SetPoint("CENTER", EM.UI.Display.Tabs.Inbox, "CENTER")
					EM.UI.Display.Tabs.Inbox.Text:SetFontSize(14)
					EM.UI.Display.Tabs.Inbox.Text:SetFontColor(0.847,0.8,0.62,1)
					EM.UI.Display.Tabs.Inbox.Text:SetLayer(30)
					EM.UI.Display.Tabs.Inbox.Text:SetText("INBOX")

			EM.UI.Display.Tabs.Send = UI.CreateFrame("Frame", "emMailWindowTabs_Tabs_Send", EM.UI.Display.Tabs)
				EM.UI.Display.Tabs.Send:SetPoint("BOTTOMLEFT", EM.UI.Display.Tabs.Inbox, "BOTTOMRIGHT", 3, 0)
				EM.UI.Display.Tabs.Send:SetWidth(104)
				EM.UI.Display.Tabs.Send:SetHeight(29)
				EM.UI.Display.Tabs.Send:SetLayer(20)
				EM.UI.Display.Tabs.Send.Name = "Send"

				EM.UI.Display.Tabs.Send.Texture = UI.CreateFrame("Texture", "emMailWindowTabs_Tabs_Send_Texture", EM.UI.Display.Tabs.Send)
					EM.UI.Display.Tabs.Send.Texture:SetPoint("TOPLEFT", EM.UI.Display.Tabs.Send, "TOPLEFT")
					EM.UI.Display.Tabs.Send.Texture:SetPoint("BOTTOMRIGHT", EM.UI.Display.Tabs.Send, "BOTTOMRIGHT")
					EM.UI.Display.Tabs.Send.Texture:SetTexture(AddonId, "gfx/tabPaneHorizInActive.png")
					EM.UI.Display.Tabs.Send.Texture:SetLayer(25)

				EM.UI.Display.Tabs.Send.Text = UI.CreateFrame("Text", "emMailWindowTabs_Tabs_Send_Text", EM.UI.Display.Tabs.Send)
					EM.UI.Display.Tabs.Send.Text:SetPoint("CENTER", EM.UI.Display.Tabs.Send, "CENTER")
					EM.UI.Display.Tabs.Send.Text:SetFontSize(14)
					EM.UI.Display.Tabs.Send.Text:SetFontColor(0.847,0.8,0.62,1)
					EM.UI.Display.Tabs.Send.Text:SetLayer(30)
					EM.UI.Display.Tabs.Send.Text:SetText("SEND")

			EM.UI.Display.Tabs.Archive = UI.CreateFrame("Frame", "emMailWindowTabs_Tabs_Archive", EM.UI.Display.Tabs)
				EM.UI.Display.Tabs.Archive:SetPoint("BOTTOMLEFT", EM.UI.Display.Tabs.Send, "BOTTOMRIGHT", 2, 0)
				EM.UI.Display.Tabs.Archive:SetWidth(104)
				EM.UI.Display.Tabs.Archive:SetHeight(29)
				EM.UI.Display.Tabs.Archive:SetLayer(20)
				EM.UI.Display.Tabs.Archive.Name = "Archive"

				EM.UI.Display.Tabs.Archive.Texture = UI.CreateFrame("Texture", "emMailWindowTabs_Tabs_Archive_Texture", EM.UI.Display.Tabs.Archive)
					EM.UI.Display.Tabs.Archive.Texture:SetPoint("TOPLEFT", EM.UI.Display.Tabs.Archive, "TOPLEFT")
					EM.UI.Display.Tabs.Archive.Texture:SetPoint("BOTTOMRIGHT", EM.UI.Display.Tabs.Archive, "BOTTOMRIGHT")
					EM.UI.Display.Tabs.Archive.Texture:SetTexture(AddonId, "gfx/tabPaneHorizInActive.png")
					EM.UI.Display.Tabs.Archive.Texture:SetLayer(25)

				EM.UI.Display.Tabs.Archive.Text = UI.CreateFrame("Text", "emMailWindowTabs_Tabs_Archive_Text", EM.UI.Display.Tabs.Archive)
					EM.UI.Display.Tabs.Archive.Text:SetPoint("CENTER", EM.UI.Display.Tabs.Archive, "CENTER")
					EM.UI.Display.Tabs.Archive.Text:SetFontSize(14)
					EM.UI.Display.Tabs.Archive.Text:SetFontColor(0.847,0.8,0.62,1)
					EM.UI.Display.Tabs.Archive.Text:SetLayer(30)
					EM.UI.Display.Tabs.Archive.Text:SetText("ARCHIVE")

			EM.UI.Display.Tabs.Settings = UI.CreateFrame("Frame", "emMailWindowTabs_Tabs_Settings", EM.UI.Display.Tabs)
				EM.UI.Display.Tabs.Settings:SetPoint("BOTTOMLEFT", EM.UI.Display.Tabs.Archive, "BOTTOMRIGHT", 3, 0)
				EM.UI.Display.Tabs.Settings:SetWidth(104)
				EM.UI.Display.Tabs.Settings:SetHeight(29)
				EM.UI.Display.Tabs.Settings:SetLayer(20)
				EM.UI.Display.Tabs.Settings.Name = "Settings"

				EM.UI.Display.Tabs.Settings.Texture = UI.CreateFrame("Texture", "emMailWindowTabs_Tabs_Settings_Texture", EM.UI.Display.Tabs.Settings)
					EM.UI.Display.Tabs.Settings.Texture:SetPoint("TOPLEFT", EM.UI.Display.Tabs.Settings, "TOPLEFT")
					EM.UI.Display.Tabs.Settings.Texture:SetPoint("BOTTOMRIGHT", EM.UI.Display.Tabs.Settings, "BOTTOMRIGHT")
					EM.UI.Display.Tabs.Settings.Texture:SetTexture(AddonId, "gfx/tabPaneHorizInActive.png")
					EM.UI.Display.Tabs.Settings.Texture:SetLayer(25)

				EM.UI.Display.Tabs.Settings.Text = UI.CreateFrame("Text", "emMailWindowTabs_Tabs_Settings_Text", EM.UI.Display.Tabs.Settings)
					EM.UI.Display.Tabs.Settings.Text:SetPoint("CENTER", EM.UI.Display.Tabs.Settings, "CENTER")
					EM.UI.Display.Tabs.Settings.Text:SetFontSize(14)
					EM.UI.Display.Tabs.Settings.Text:SetFontColor(0.847,0.8,0.62,1)
					EM.UI.Display.Tabs.Settings.Text:SetLayer(30)
					EM.UI.Display.Tabs.Settings.Text:SetText("SETTINGS")

		EM.UI.Display.Body = UI.CreateFrame("Texture", "emMailWindowTabs_Body", EM.UI.Display)
			EM.UI.Display.Body:SetPoint("TOPLEFT", EM.UI.Display.Tabs, "BOTTOMLEFT")
			EM.UI.Display.Body:SetPoint("BOTTOMRIGHT", EM.UI.Display, "BOTTOMRIGHT")
			EM.UI.Display.Body:SetTexture(AddonId, "gfx/tabGridBGShort.png")
			EM.UI.Display.Body:SetLayer(15)

			EM.UI.Inbox = UI.CreateFrame("Frame", "emMailInboxTab", EM.UI.Display.Body)
				EM.UI.Inbox:SetPoint("TOPLEFT", EM.UI.Display.Body, "TOPLEFT")
				EM.UI.Inbox:SetPoint("BOTTOMRIGHT", EM.UI.Display.Body, "BOTTOMRIGHT")
				EM.UI.Inbox:SetLayer(20)
				EM.UI.Inbox:SetVisible(true)

			EM.UI.Send = UI.CreateFrame("Frame", "emMailSendTab", EM.UI.Display.Body)
				EM.UI.Send:SetPoint("TOPLEFT", EM.UI.Display.Body, "TOPLEFT")
				EM.UI.Send:SetPoint("BOTTOMRIGHT", EM.UI.Display.Body, "BOTTOMRIGHT")
				EM.UI.Send:SetLayer(20)
				EM.UI.Send:SetVisible(false)

			EM.UI.Archive = UI.CreateFrame("Frame", "emMailArchiveTab", EM.UI.Display.Body)
				EM.UI.Archive:SetPoint("TOPLEFT", EM.UI.Display.Body, "TOPLEFT")
				EM.UI.Archive:SetPoint("BOTTOMRIGHT", EM.UI.Display.Body, "BOTTOMRIGHT")
				EM.UI.Archive:SetLayer(20)
				EM.UI.Archive:SetVisible(false)

			EM.UI.Settings = UI.CreateFrame("Frame", "emMailSettingsTab", EM.UI.Display.Body)
				EM.UI.Settings:SetPoint("TOPLEFT", EM.UI.Display.Body, "TOPLEFT")
				EM.UI.Settings:SetPoint("BOTTOMRIGHT", EM.UI.Display.Body, "BOTTOMRIGHT")
				EM.UI.Settings:SetLayer(20)
				EM.UI.Settings:SetVisible(false)

	EM.UI.Display.Tabs.Visible = "Inbox"

	EM.UI.Display.Tabs.Inbox:EventAttach(Event.UI.Input.Mouse.Cursor.In, EM.Event.MouseIn, "emMailWindowTabs_Tabs_Inbox_MouseIn")
	EM.UI.Display.Tabs.Send:EventAttach(Event.UI.Input.Mouse.Cursor.In, EM.Event.MouseIn, "emMailWindowTabs_Tabs_Send_MouseIn")
	EM.UI.Display.Tabs.Archive:EventAttach(Event.UI.Input.Mouse.Cursor.In, EM.Event.MouseIn, "emMailWindowTabs_Tabs_Archive_MouseIn")
	EM.UI.Display.Tabs.Settings:EventAttach(Event.UI.Input.Mouse.Cursor.In, EM.Event.MouseIn, "emMailWindowTabs_Tabs_Settings_MouseIn")
	EM.UI.Display.Tabs.Inbox:EventAttach(Event.UI.Input.Mouse.Cursor.Out, EM.Event.MouseOut, "emMailWindowTabs_Tabs_Inbox_MouseOut")
	EM.UI.Display.Tabs.Send:EventAttach(Event.UI.Input.Mouse.Cursor.Out, EM.Event.MouseOut, "emMailWindowTabs_Tabs_Send_MouseOut")
	EM.UI.Display.Tabs.Archive:EventAttach(Event.UI.Input.Mouse.Cursor.Out, EM.Event.MouseOut, "emMailWindowTabs_Tabs_Archive_MouseOut")
	EM.UI.Display.Tabs.Settings:EventAttach(Event.UI.Input.Mouse.Cursor.Out, EM.Event.MouseOut, "emMailWindowTabs_Tabs_Settings_MouseOut")
	EM.UI.Display.Tabs.Inbox:EventAttach(Event.UI.Input.Mouse.Left.Click, EM.Event.LeftClick, "emMailWindowTabs_Tabs_Inbox_LeftClick")
	EM.UI.Display.Tabs.Send:EventAttach(Event.UI.Input.Mouse.Left.Click, EM.Event.LeftClick, "emMailWindowTabs_Tabs_Send_LeftClick")
	EM.UI.Display.Tabs.Archive:EventAttach(Event.UI.Input.Mouse.Left.Click, EM.Event.LeftClick, "emMailWindowTabs_Tabs_Archive_LeftClick")
	EM.UI.Display.Tabs.Settings:EventAttach(Event.UI.Input.Mouse.Left.Click, EM.Event.LeftClick, "emMailWindowTabs_Tabs_Settings_LeftClick")

	EM.UI.Setup.Inbox()
	EM.UI.Setup.Settings()
end


function EM.Event.MouseIn(handle)
	if handle.Name ~= EM.UI.Display.Tabs.Visible then
		handle.Texture:SetTexture(AddonId, "gfx/tabPaneHorizActive.png")
	end
end

function EM.Event.MouseOut(handle)
	if handle.Name ~= EM.UI.Display.Tabs.Visible then
		handle.Texture:SetTexture(AddonId, "gfx/tabPaneHorizInActive.png")
	end
end

function EM.Event.LeftClick(handle)
	if handle.Name ~= EM.UI.Display.Tabs.Visible then
		local oldTab = EM.UI.Display.Tabs.Visible

		EM.UI[oldTab]:SetVisible(false)
		EM.UI.Display.Tabs[oldTab].Texture:SetTexture(AddonId, "gfx/tabPaneHorizInActive.png")

		EM.UI[handle.Name]:SetVisible(true)
		if not EM.Created[handle.Name] then
			EM.UI.Setup[handle.Name]()
		end
		if handle.Name == "Send" then
			for i=1,7,1 do
				local bag = "Bag" .. i
				EM.UI.Inventory.Bags.Loaded[bag]()
			end
			if EM.Settings and EM.Settings.Contacts and EM.Settings.Contacts.AutoOpen.status then
				if not EM.UI.Contacts.Window then
					EM.UI.Contacts.Create()
				else
					EM.UI.Contacts.Window:SetVisible(true)
					EM.UI.Contacts.Populate()
				end
			end
		else
			for child, st in pairs(EM.Context.Inventory:GetChildren()) do
				child:SetVisible(false)
			end
			if EM.UI.Contacts.Window and EM.UI.Contacts.Window:GetVisible() then
				EM.UI.Contacts.Window:SetVisible(false)
			end
		end
		handle.Texture:SetTexture(AddonId, "gfx/tabPaneHorizActive.png")

		EM.UI.Display.Tabs.Visible = handle.Name
	end
end

local function PopulateMail(messageId, t)
	local basic = Inspect.Mail.Detail(messageId)
	if basic then
		local message = EM.Mail.Create(messageId)
		for k,v in pairs(basic) do
			if k == "attachments" and message["attachments"] then
				message["attachments"] = nil
			end
			message[k] = v
		end
		message["expireUnix"] = message["expire"]
		message["expire"] = os.date("%x %X", message["expire"])
		message["deletedUnix"] = 9999999999
		message["deleted"] = os.date("%x %X", message["deletedUnix"])
		if not message["attachmentCount"] then
			if t == "basic" and message["attachments"] then
				message["attachmentCount"] = message["attachments"]
			elseif message["attachments"] then
				message["attachmentCount"] = #message["attachments"]
				message["attachmentsType"] = {}
				for n,i in pairs(message["attachments"]) do
					local typeID = Inspect.Item.Detail(i)
					if typeID.type then
						table.insert(message["attachmentsType"], typeID.type)
					elseif typeID.coin then
						table.insert(message["attachmentsType"], typeID.coin)
					end
				end
			end
		end
		if t == "detail" and message["cod"] then
			message["paidCoD"] = message["cod"]
		elseif t == "detail" and EM.Messages[messageId].paidCoD then
			message["paidCoD"] = EM.Messages[messageId].paidCoD
		end
		if t and message["subject"] == "Gift from a friend" then
			message["gift"] = true
		end
		if t == "detail" and EM.Messages[messageId].marked then
			message["marked"] = true
		else
			message["marked"] = false
		end
		return message
	else
		return nil
	end
end

function EM.Mail.Create(messageId)
	local message = {}
	setmetatable(message, self)
	message.id = messageId
	return message
end

local function MailAvailable(hEvent, mail)

	if not EM.UI.Created then return end
	EM.Mail.List.Original = {}
	local interaction = Inspect.Interaction()
	for id, t in pairs(mail) do
		table.insert(EM.Mail.List.Original, { id = id, status = t})
	end
	for count,data in ipairs(EM.Mail.List.Original) do
		if interaction.mail and EM.UI.MailWindow:GetVisible() then
			if data.status and not EM.Messages[data.id] then
				EM.Messages[data.id] = PopulateMail(data.id, data.status)
				EasyMailTable[EM.PlayerName].Inbox[data.id] = EM.Messages[data.id]
			elseif data.status == "detail" and EM.Messages[data.id] then
				EM.Messages[data.id] = PopulateMail(data.id, data.status)
				if EM.Messages[data.id]["attachmentCount"] ~= 0 then
					EasyMailTable[EM.PlayerName].Inbox[data.id] = EM.Messages[data.id]
				end
			elseif not data.status then
				EasyMailTable[EM.PlayerName].Inbox[data.id].deleted = os.date("%x %X", Inspect.Time.Server())
				EasyMailTable[EM.PlayerName].Inbox[data.id].deletedUnix = Inspect.Time.Server()
				EM.Messages[data.id] = nil
				EM.Event.Trigger.RefreshMail(data.id, data.status)
				EM.Utility.CreateNewDataTable()
				if EM.UI.Archive.Grid then
					EM.Archive.Populate(0)
				end
			end
		else
			return
		end
	end
	EM.Event.Trigger.RefreshMail()
end

local function ConsoleLoaded(handle)
	for id, test in pairs(Inspect.Console.List()) do
		if Inspect.Console.Detail(id).name ~= "*Combat" then
			Command.Console.Display(id, false, "<font color='#4BCDD6'>EasyMail Loaded</font>", true)
		end
	end
end

local function SavedDataLoaded(hEvent, id)
	if id == AddonId then
		if type(_G["EasyMail"]) == "string" then
			EasyMailTable = loadstring("return " .. zlib.inflate()(_G["EasyMail"]))
			EasyMailTable = EasyMailTable and EasyMailTable() or {}
		else
			EasyMailTable = {}
		end
		EasyMailTable[EM.PlayerName] = EasyMailTable[EM.PlayerName] or {PlayerID = Inspect.Unit.Lookup('player')}
		EasyMailTable[EM.PlayerName].Inbox = EasyMailTable[EM.PlayerName].Inbox or {}
		EasyMailTable[EM.PlayerName].Sent = EasyMailTable[EM.PlayerName].Sent or {}

		EasyMailCharacterTable = _G["EasyMailSettings"]

		if EasyMailCharacterTable and next(EasyMailCharacterTable) ~= nil then
			EM.Settings = EasyMailCharacterTable
		end
		EM.Utility.AddDeletedUnix()
		EM.Utility.CreateNewDataTable()
	end
end

local function SaveDataBegin(hEvent, id)
	if id == AddonId then
		_G["EasyMail"] = zlib.deflate(zlib.BEST_COMPRESSION)(Utility.Serialize.Inline(EasyMailTable), "finish")
		_G["EasyMailSettings"] = EM.Settings
	end
end

function EM.InteractionChanged(hEvent, interaction, state)
	if interaction == "mail" and state and not EM.UI.MailWindow then
		if Inspect.Unit.Detail("player").combat then
			for id, test in pairs(Inspect.Console.List()) do
				if Inspect.Console.Detail(id).name ~= "*Combat" then
					Command.Console.Display(id, false, "<font color='#4BCDD6'>EasyMail can not be loaded while in combat!</font>", true)
				end
			end
		else
			Command.System.Watchdog.Quiet()
			EM.UI.Create()
			EM.UI.Inbox.Grid:SetVisible(false)
		end
	elseif interaction == "mail" and state and EM.UI.MailWindow then
		EM.UI.MailWindow:SetVisible(true)
		EM.Context.Main:SetStrata("tutorial")
	elseif interaction == "mail" and not state and EM.UI.MailWindow then
		EM.Event.LeftClick(EM.UI.Display.Tabs.Inbox)
		if EM.UI.Contacts.Window then
			EM.UI.Contacts.Window:SetVisible(false)
		end
		EM.UI.MailWindow:SetVisible(false)
		if EM.UI.PopupWindow then
			EM.UI.PopupWindow:SetVisible(false)
		end
		for child, st in pairs(EM.Context.Inventory:GetChildren()) do
			child:SetVisible(false)
		end
		if EM.UI.MessageWindow then
			EM.UI.MessageWindow:SetVisible(false)
		end
		EM.UI.Inbox.btnTakeSelected:SetEnabled(false)
		EM.UI.Inbox.btnDeleteSelected:SetEnabled(false)
	end
end

UI.Native.Console1:EventAttach(Event.UI.Native.Loaded, ConsoleLoaded, AddonId .. "_Native_Console_Loaded")

Command.Event.Attach(Event.Interaction,						EM.InteractionChanged, AddonId .. "_InteractionChanged" )
Command.Event.Attach(Event.Mail, 							MailAvailable, AddonId .. "_MailAvailable" )
Command.Event.Attach(Event.Addon.SavedVariables.Load.End,	SavedDataLoaded, AddonId .. "_SavedDataLoaded" )
Command.Event.Attach(Event.Addon.SavedVariables.Save.Begin, SaveDataBegin, AddonId .. "_SavedDataLoaded" )
Command.Event.Attach(EM.Event.UICreated,					MailAvailable, AddonId .. "_UICreated" )