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

EM.Contacts = {}
EM.UI.Contacts = {}
EM.UI.Contacts.Grid = {}
EM.Contacts.List = {}
EM.Contacts.List.Guild = {}
EM.Contacts.List.Friends = {}
EM.Contacts.List.Grid = {}

EM.Context.Contacts = UI.CreateContext("emContextContacts")

local tableAdd = table.insert
local tableSort = table.sort

function EM.UI.Contacts.Create()

	local window = UI.CreateFrame("RiftWindow", "emContactsWindow", EM.Context.Contacts)
		window:SetPoint("TOPLEFT", EM.UI.MailWindow, "TOPRIGHT")
		window:SetWidth(597)
		window:SetHeight(456)
		window:SetLayer(5)
		window:SetTitle("CONTACTS")
		window:SetVisible(true)
		window.leftDown = false
		window:EventAttach(Event.UI.Input.Mouse.Left.Down.Dive, function(handle)
			if handle:GetName() == "emContactsWindowBorder" 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() == "emContactsWindowBorder" then
				window.leftDown = false
			end
		end, "LeftUp")
		window:EventAttach(Event.UI.Input.Mouse.Left.Upoutside.Dive, function(handle)
			if handle:GetName() == "emContactsWindowBorder" then
				window.leftDown = false
			end
		end, "LeftUpoutside")

	window.Close = UI.CreateFrame("RiftButton", "contacts_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)
		end, "LeftClick")

	window.Categories = UI.CreateFrame("Frame", "contacts_Categories", window)
		window.Categories:SetPoint("TOPLEFT", window, "TOPLEFT", 25, 60)
		window.Categories:SetHeight(25)
		window.Categories:SetWidth(548)
		window.Categories:SetBackgroundColor(0.2,0.2,0.2,1)
		window.Categories:SetVisible(true)

	window.Categories.back = UI.CreateFrame("Frame", "contacts_Categories_Guild_back", window.Categories)
		window.Categories.back:SetPoint("TOPLEFT", window.Categories, "TOPLEFT", 2, 2)
		window.Categories.back:SetHeight(21)
		window.Categories.back:SetWidth(133)
		window.Categories.back:SetBackgroundColor(0,0,0,1)
		window.Categories.back:SetVisible(true)

	window.Categories.Text = UI.CreateFrame("Text", "contacts_Categories_Text", window.Categories.back)
		window.Categories.Text:SetPoint("TOPLEFT", window.Categories.back, "TOPLEFT", 2, 0)
		window.Categories.Text:SetHeight(25)
		window.Categories.Text:SetWidth(137)
		window.Categories.Text:SetFontColor(1,1,1,1)
		window.Categories.Text:SetFontSize(14)
		window.Categories.Text:SetText("Categories:")
		window.Categories.Text:SetVisible(true)


		window.Categories.Guild = UI.CreateFrame("Frame", "contacts_Categories_Guild", window.Categories)
			window.Categories.Guild:SetPoint("TOPLEFT", window.Categories, "TOPLEFT", 137, 0)
			window.Categories.Guild:SetHeight(25)
			window.Categories.Guild:SetWidth(137)
			window.Categories.Guild:SetBackgroundColor(1,1,1,0)
			window.Categories.Guild:EventAttach(Event.UI.Input.Mouse.Cursor.In, function() window.Categories.Guild:SetBackgroundColor(0.6,0.6,0.6,1) end, "MouseIn")
			window.Categories.Guild:EventAttach(Event.UI.Input.Mouse.Cursor.Out, function()
				if window.Categories.Selected == "Guild" then
					window.Categories.Guild:SetBackgroundColor(1,1,1,1)
				else
					window.Categories.Guild:SetBackgroundColor(1,1,1,0)
				end
			end, "MouseOut")
			window.Categories.Guild:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
				if window.Categories.Selected == "Guild" then
					window.Categories.Selected = nil
					window.Categories.Guild:SetBackgroundColor(1,1,1,0)
					for children,info in pairs(EM.UI.Contacts.Window.Display.View:GetChildren()) do
						children:SetVisible(false)
					end
					EM.UI.Contacts.Window.Display.Scrollbar:SetEnabled(false)
					EM.UI.Contacts.Window.Display.Scrollbar:SetRange(1,1)
					EM.UI.Contacts.Window.Display.Scrollbar:SetPosition(1)
				else
					window.Categories.Selected = "Guild"
					window.Categories.Guild:SetBackgroundColor(1,1,1,1)
					window.Categories.Friends:SetBackgroundColor(1,1,1,0)
					EM.UI.Contacts.DisplayUpdate(1,64,"Guild")
					if table.maxn(EM.Contacts.List.Guild) > 64 then
						EM.UI.Contacts.Window.Display.Scrollbar:SetEnabled(true)
						EM.UI.Contacts.Window.Display.Scrollbar:SetRange(1,math.ceil(table.maxn(EM.Contacts.List.Guild)/4)-15)
					else
						EM.UI.Contacts.Window.Display.Scrollbar:SetEnabled(false)
						EM.UI.Contacts.Window.Display.Scrollbar:SetRange(1,1)
					end
				end
			end, "LeftClick")

			window.Categories.Guild.back = UI.CreateFrame("Frame", "contacts_Categories_Guild_back", window.Categories.Guild)
				window.Categories.Guild.back:SetPoint("TOPLEFT", window.Categories.Guild, "TOPLEFT", 2, 2)
				window.Categories.Guild.back:SetPoint("BOTTOMRIGHT", window.Categories.Guild, "BOTTOMRIGHT", -2, -2)
				window.Categories.Guild.back:SetBackgroundColor(0,0,0,1)
				window.Categories.Guild.back:SetVisible(true)

			window.Categories.Guild.text = UI.CreateFrame("Text", "contacts_Categories_Guild_back_text", window.Categories.Guild.back)
				window.Categories.Guild.text:SetFontColor(1,1,1,1)
				window.Categories.Guild.text:SetFontSize(14)
				window.Categories.Guild.text:SetText("Guild")
				window.Categories.Guild.text:SetVisible(true)
				window.Categories.Guild.text:SetPoint("CENTER", window.Categories.Guild.back, "CENTER", 2, 0)


		window.Categories.Friends = UI.CreateFrame("Frame", "contacts_Categories_Friends", window.Categories)
			window.Categories.Friends:SetPoint("TOPLEFT", window.Categories.Guild, "TOPRIGHT")
			window.Categories.Friends:SetHeight(25)
			window.Categories.Friends:SetWidth(137)
			window.Categories.Friends:SetBackgroundColor(1,1,1,0)
			window.Categories.Friends:EventAttach(Event.UI.Input.Mouse.Cursor.In, function() window.Categories.Friends:SetBackgroundColor(0.6,0.6,0.6,1) end, "MouseIn")
			window.Categories.Friends:EventAttach(Event.UI.Input.Mouse.Cursor.Out, function()
				if window.Categories.Selected == "Friends" then
					window.Categories.Friends:SetBackgroundColor(1,1,1,1)
				else
					window.Categories.Friends:SetBackgroundColor(1,1,1,0)
				end
			end, "MouseOut")
			window.Categories.Friends:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
				if window.Categories.Selected == "Friends" then
					window.Categories.Selected = nil
					window.Categories.Friends:SetBackgroundColor(1,1,1,0)
					for children,info in pairs(EM.UI.Contacts.Window.Display.View:GetChildren()) do
						children:SetVisible(false)
					end
					EM.UI.Contacts.Window.Display.Scrollbar:SetEnabled(false)
					EM.UI.Contacts.Window.Display.Scrollbar:SetRange(1,1)
					EM.UI.Contacts.Window.Display.Scrollbar:SetPosition(1)
				else
					window.Categories.Selected = "Friends"
					window.Categories.Guild:SetBackgroundColor(1,1,1,0)
					window.Categories.Friends:SetBackgroundColor(1,1,1,1)
					EM.UI.Contacts.DisplayUpdate(1,64,"Friends")
					if table.maxn(EM.Contacts.List.Friends) > 64 then
						EM.UI.Contacts.Window.Display.Scrollbar:SetEnabled(true)
						EM.UI.Contacts.Window.Display.Scrollbar:SetRange(1,math.ceil(table.maxn(EM.Contacts.List.Friends)/4)-15)
					else
						EM.UI.Contacts.Window.Display.Scrollbar:SetEnabled(false)
						EM.UI.Contacts.Window.Display.Scrollbar:SetRange(1,1)
					end
				end
			end, "LeftClick")

			window.Categories.Friends.back = UI.CreateFrame("Frame", "contacts_Categories_Friends_back", window.Categories.Friends)
				window.Categories.Friends.back:SetPoint("TOPLEFT", window.Categories.Friends, "TOPLEFT", 2, 2)
				window.Categories.Friends.back:SetPoint("BOTTOMRIGHT", window.Categories.Friends, "BOTTOMRIGHT", -2, -2)
				window.Categories.Friends.back:SetBackgroundColor(0,0,0,1)
				window.Categories.Friends.back:SetVisible(true)

			window.Categories.Friends.text = UI.CreateFrame("Text", "contacts_Categories_Friends_back_text", window.Categories.Friends.back)
				window.Categories.Friends.text:SetFontColor(1,1,1,1)
				window.Categories.Friends.text:SetFontSize(14)
				window.Categories.Friends.text:SetText("Friends")
				window.Categories.Friends.text:SetVisible(true)
				window.Categories.Friends.text:SetPoint("CENTER", window.Categories.Friends.back, "CENTER", 2, 0)


	window.Display = UI.CreateFrame("Frame", "contacts_Display", window)
		window.Display:SetPoint("TOPLEFT", window.Categories, "BOTTOMLEFT", 0, 5)
		window.Display:SetWidth(528)
		window.Display:SetHeight(320)
		window.Display:SetBackgroundColor(0.2,0.2,0.2,1)
		window.Display:SetVisible(true)
		window.Display:SetLayer(10)
		window.Display:EventAttach(Event.UI.Input.Mouse.Wheel.Forward, function()
			window.Display.Scrollbar:NudgeUp()
		end, "WheelForward")
		window.Display:EventAttach(Event.UI.Input.Mouse.Wheel.Back, function()
			window.Display.Scrollbar:NudgeDown()
		end, "WheelBack")

		window.Display.View = UI.CreateFrame("Frame", "contacts_Display_ScrollBar_view", window.Display)
			window.Display.View:SetPoint("TOPLEFT", window.Display, "TOPLEFT")
			window.Display.View:SetHeight(320)
			window.Display.View:SetWidth(528)

		window.Display.Scrollbar = UI.CreateFrame("RiftScrollbar", "contacts_Display_ScrollBar", window.Display)
			window.Display.Scrollbar:SetPoint("TOPLEFT", window.Display, "TOPRIGHT", 5, 0)
			window.Display.Scrollbar:SetPoint("BOTTOMLEFT", window.Display, "BOTTOMRIGHT", 5, 0)
			window.Display.Scrollbar:SetThickness(15)
			window.Display.Scrollbar:SetRange(1,1)
			window.Display.Scrollbar:SetVisible(true)
			window.Display.Scrollbar:SetEnabled(false)
			window.Display.Scrollbar:EventAttach(Event.UI.Input.Mouse.Wheel.Forward, function()
				window.Display.Scrollbar:NudgeUp()
			end, "WheelForward")
			window.Display.Scrollbar:EventAttach(Event.UI.Input.Mouse.Wheel.Back, function()
				window.Display.Scrollbar:NudgeDown()
			end, "WheelBack")
			window.Display.Scrollbar:EventAttach(Event.UI.Scrollbar.Change, function()
				local pos = math.floor(window.Display.Scrollbar:GetPosition())
				local lower = 1
				local upper = 64
				if pos > 1 then
					lower = (pos*4)+1
					upper = lower + 63
				end
				EM.UI.Contacts.DisplayUpdate(lower, upper, window.Categories.Selected)
			end, "ScrollbarChange")


	window.Text = UI.CreateFrame("Text", "contacts_Text", window)
		window.Text:SetPoint("TOPLEFT", window.Display, "BOTTOMLEFT", 5, 5)
		window.Text:SetFontColor(1,1,1,1)
		window.Text:SetFontSize(13)
		window.Text:SetText("Left click a contact to select (adds to list).")
		window.Text:SetVisible(true)

	window.Text2 = UI.CreateFrame("Text", "contacts_Text2", window)
		window.Text2:SetPoint("TOPRIGHT", window.Display, "BOTTOMRIGHT", -5, 5)
		window.Text2:SetFontColor(1,1,1,1)
		window.Text2:SetFontSize(13)
		window.Text2:SetText("Right click to replace To list.")
		window.Text2:SetVisible(true)

	EM.UI.Contacts.Window = window

	EM.UI.Contacts.addCell()

	EM.UI.Contacts.Populate()

end

function EM.UI.Contacts.DisplayUpdate(lower, upper, category)
	local count = 1
	for i = lower, upper , 1 do
		EM.Contacts.List.Grid[count] = EM.Contacts.List[category][i]
		count = count + 1
	end
	for t = 1, 64, 1 do
		if EM.Contacts.List.Grid[t] then
			EM.UI.Contacts.Grid[t]:SetVisible(true)
			EM.UI.Contacts.Grid[t].text:SetText(EM.Contacts.List.Grid[t])
		else
			EM.UI.Contacts.Grid[t]:SetVisible(false)
			EM.UI.Contacts.Grid[t].text:SetText("")
		end
	end
end

function EM.UI.Contacts.Populate(hEvent)

	if EM.UI.Contacts.Window and EM.UI.Contacts.Window:GetVisible() then
		EM.Contacts.List.Guild = {}
		EM.Contacts.List.Friends = {}
		if Inspect.Unit.Detail("player").guild then
			for name,status in pairs(Inspect.Guild.Roster.List()) do
				tableAdd(EM.Contacts.List.Guild, name)
			end
			tableSort(EM.Contacts.List.Guild, function(a,b) return a<b end)
		end

		for name,status in pairs(Inspect.Social.Friend.List()) do
			tableAdd(EM.Contacts.List.Friends, name)
		end
		tableSort(EM.Contacts.List.Friends, function(a,b) return a<b end)
	end
end

function EM.UI.Contacts.addCell()

	local count = 1
	for r = 0, 15, 1 do
		for c = 0, 3, 1 do
			local cell = UI.CreateFrame("Frame", "contacts_Display_" .. (r+1) .. "_" .. (c+1), EM.UI.Contacts.Window.Display.View)
				cell:SetPoint("TOPLEFT", EM.UI.Contacts.Window.Display.View, "TOPLEFT", c*132, r*20)
				cell.number = count
				cell:SetHeight(18)
				cell:SetWidth(130)
				cell:SetBackgroundColor(0,0,0,1)
				cell:SetVisible(false)
				cell:SetLayer(15)
				cell:EventAttach(Event.UI.Input.Mouse.Cursor.In, function()
					cell:SetBackgroundColor(0.6,0.6,0.6,1)
					cell.text:SetFontColor(0,0,0,1)
				end, "MouseIn")
				cell:EventAttach(Event.UI.Input.Mouse.Cursor.Out, function()
						cell:SetBackgroundColor(0,0,0,1)
						cell.text:SetFontColor(1,1,1,1)
				end, "MouseOut")
				cell:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
					if EM.UI.Send.ToField:GetText() == (nil or "") then
						EM.UI.Send.ToField:SetText(EM.Contacts.List.Grid[cell.number])
					else
						local current = EM.UI.Send.ToField:GetText()
						EM.UI.Send.ToField:SetText(current .. "; " .. EM.Contacts.List.Grid[cell.number])
					end
					if (EM.UI.Send.ToField:GetText() ~= (nil or "")) and (EM.UI.Send.SubjectField:GetText() ~= (nil or "")) then
						EM.UI.Send.btnSend:SetEnabled(true)
					else
						EM.UI.Send.btnSend:SetEnabled(false)
					end
				end, "LeftClick")
				cell:EventAttach(Event.UI.Input.Mouse.Right.Click, function()
					EM.UI.Send.ToField:SetText(EM.Contacts.List.Grid[cell.number])
					if (EM.UI.Send.ToField:GetText() ~= (nil or "")) and (EM.UI.Send.SubjectField:GetText() ~= (nil or "")) then
						EM.UI.Send.btnSend:SetEnabled(true)
					else
						EM.UI.Send.btnSend:SetEnabled(false)
					end
				end, "RightClick")

			cell.text = UI.CreateFrame("Text", "contacts_Display_" .. (r+1) .. "_" .. (c+1) .. "_text", cell)
				cell.text:SetPoint("CENTERLEFT", cell, "CENTERLEFT", 1, 0)
				cell.text:SetWidth(130)
				cell.text:SetText("")
				cell.text:SetFontSize(14)
				cell.text:SetFontColor(1,1,1,1)
				cell.text:SetVisible(true)
				cell.text:SetLayer(30)

			EM.UI.Contacts.Grid[cell.number] = cell
			count = count + 1
		end
	end
end


Command.Event.Attach(Event.Guild.Roster.Add, 		EM.UI.Contacts.Populate, AddonId .. "_GuildAdd" )
Command.Event.Attach(Event.Guild.Roster.Remove, 	EM.UI.Contacts.Populate, AddonId .. "_GuildRemove" )
Command.Event.Attach(Event.Social.Friend, 			EM.UI.Contacts.Populate, AddonId .. "_FriendChange" )