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

EM.UI.Message = {}
EM.Message = {}

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

function EM.Message.Window(messageID,mailType)

	EM.Message.Data = EasyMailTable[EM.PlayerName][mailType][messageID]

	if not EM.UI.MessageWindow then
		local window = UI.CreateFrame("SimpleWindow", "emMessageWindow", EM.Context.Message)
		window:SetCloseButtonVisible(true)
		window:SetPoint("TOPLEFT", EM.UI.MailWindow, "TOPRIGHT")
		window:SetWidth(450)
		window:SetHeight(461)
		window:SetLayer(0)
		window:SetTitle("")
		window:SetVisible(true)

		window.texture2 = UI.CreateFrame("Texture", "MessageWindow2", window)
			window.texture2:SetTexture("EasyMail", "gfx/messageWindow2.png")
			window.texture2:SetLayer(5)
			window.texture2:SetPoint("BOTTOMCENTER", window, "BOTTOMCENTER", 0, -18)

		window.texture1 = UI.CreateFrame("Texture", "MessageWindow1", window)
			window.texture1:SetTexture("Rift", "Mail_SubjectHeader.png.dds")
			window.texture1:SetWidth(566)
			window.texture1:SetLayer(5)
			window.texture1:SetPoint("BOTTOMCENTER", window.texture2, "TOPCENTER", -2, 27)

		window.letterIcon = UI.CreateFrame("Texture", "MessageLetterIcon", window.texture1)
			window.letterIcon:SetTexture("Rift", "collection_of_love_letters_a.dds")
			window.letterIcon:SetLayer(10)
			window.letterIcon:SetPoint("CENTER", window.texture1, "CENTER", -172, 0)

		window.letterIconBorder = UI.CreateFrame("Texture", "MessageLetterIconBorder", window.texture1)
			window.letterIconBorder:SetTexture("Rift", "icon_border.dds")
			window.letterIconBorder:SetLayer(20)
			window.letterIconBorder:SetPoint("CENTER", window.texture1, "CENTER", -172, 0)

		window.fromText = UI.CreateFrame("Text", "MessageFromText", window.texture1)
			window.fromText:SetPoint("TOPLEFT", window.letterIconBorder, "TOPRIGHT", 12, 11)
			window.fromText:SetLayer(15)
			window.fromText:SetWidth(170)
			window.fromText:SetFontSize(14)
			window.fromText:SetFontColor(0,0,0,1)
			if EM.Message.Data.from then
				window.fromText:SetText(EM.Message.Data.from)
			elseif EM.Message.Data.to then
				window.fromText:SetText(EM.Message.Data.to)
			end

		window.fromText2 = UI.CreateFrame("Text", "MessageFromText2", window.texture1)
			window.fromText2:SetPoint("TOPLEFT", window.letterIconBorder, "TOPRIGHT", 10, 9)
			window.fromText2:SetLayer(20)
			window.fromText2:SetWidth(170)
			window.fromText2:SetFontSize(14)
			if EM.Message.Data.from then
				window.fromText2:SetText(EM.Message.Data.from)
			elseif EM.Message.Data.to then
				window.fromText2:SetText(EM.Message.Data.to)
			end

		window.subjectText = UI.CreateFrame("Text", "MessageSubjectText", window.texture1)
			window.subjectText:SetPoint("BOTTOMLEFT", window.letterIconBorder, "BOTTOMRIGHT", 12, -10)
			window.subjectText:SetLayer(15)
			window.subjectText:SetWidth(250)
			window.subjectText:SetFontSize(14)
			window.subjectText:SetFontColor(0,0,0,1)
			window.subjectText:SetText(EM.Message.Data.subject)

		window.subjectText2 = UI.CreateFrame("Text", "MessageSubjectText2", window.texture1)
			window.subjectText2:SetPoint("BOTTOMLEFT", window.letterIconBorder, "BOTTOMRIGHT", 10, -12)
			window.subjectText2:SetLayer(20)
			window.subjectText2:SetWidth(250)
			window.subjectText2:SetFontSize(14)
			window.subjectText2:SetFontColor(0.84,0.8,0.62,1)
			window.subjectText2:SetText(EM.Message.Data.subject)

		window.dateText = UI.CreateFrame("Text", "MessageDateText", window.texture1)
			window.dateText:SetPoint("TOPRIGHT", window.letterIconBorder, "TOPRIGHT", 332, 11)
			window.dateText:SetLayer(15)
			window.dateText:SetFontSize(14)
			window.dateText:SetFontColor(0,0,0,1)
			if EM.Message.Data.expire then
				window.dateText:SetText(EM.Message.Data.expire)
			elseif EM.Message.Data.timesent then
				window.dateText:SetText(EM.Message.Data.timesent)
			end

		window.dateText2 = UI.CreateFrame("Text", "MessageDateText2", window.texture1)
			window.dateText2:SetPoint("TOPRIGHT", window.letterIconBorder, "TOPRIGHT", 330, 9)
			window.dateText2:SetLayer(20)
			window.dateText2:SetFontSize(14)
			if EM.Message.Data.expire then
				window.dateText2:SetText(EM.Message.Data.expire)
			elseif EM.Message.Data.timesent then
				window.dateText2:SetText(EM.Message.Data.timesent)
			end

		window.codText = UI.CreateFrame("Text", "MessageCoDText", window.texture1)
			window.codText:SetPoint("BOTTOMRIGHT", window.letterIconBorder, "BOTTOMRIGHT", 332, -10)
			window.codText:SetLayer(15)
			window.codText:SetText("C.O.D.")
			window.codText:SetFontSize(12)
			window.codText:SetFontColor(0,0,0,1)
			if EM.Message.Data.cod then
				window.codText:SetVisible(true)
			else
				window.codText:SetVisible(false)
			end

		window.codText2 = UI.CreateFrame("Text", "MessageCoDText2", window.texture1)
			window.codText2:SetPoint("BOTTOMRIGHT", window.letterIconBorder, "BOTTOMRIGHT", 330, -12)
			window.codText2:SetLayer(20)
			window.codText2:SetText("C.O.D.")
			window.codText2:SetFontSize(12)
			window.codText2:SetFontColor(0.75,0.06,0.1,1)
			if EM.Message.Data.cod then
				window.codText2:SetVisible(true)
			else
				window.codText2:SetVisible(false)
			end

		window.bodyText = UI.CreateFrame("Text", "MessageBodyText", window.texture2)
			window.bodyText:SetPoint("TOPLEFT", window.letterIconBorder, "BOTTOMLEFT", 11, 17)
			window.bodyText:SetLayer(20)
			window.bodyText:SetWidth(376)
			window.bodyText:SetHeight(174)
			if EM.Message.Data.body and string.len(EM.Message.Data.body) > 0 then
				window.bodyText:SetText(EM.Message.Data.body)
			end

		window.btnReply = UI.CreateFrame("Texture", "MessageReplyBtn", window)
			window.btnReply:SetTexture("Rift", "btn_ReplyMail_(normal).png.dds")
			window.btnReply:SetPoint("CENTER", window.texture1, "TOPCENTER", 0, 5)
			window.btnReply:SetHeight(48)
			window.btnReply:SetWidth(48)
			window.btnReply:SetLayer(5)
			window.btnReply.Event.MouseIn = function()
				window.btnReply:SetTexture("Rift", "btn_ReplyMail_(over).png.dds")
			end
			window.btnReply.Event.MouseOut = function()
				window.btnReply:SetTexture("Rift", "btn_ReplyMail_(normal).png.dds")
			end
			window.btnReply.Event.LeftDown = function()
				window.btnReply:SetTexture("Rift", "btn_ReplyMail_(click).png.dds")
			end
			window.btnReply.Event.LeftUp = function()
				window.btnReply:SetTexture("Rift", "btn_ReplyMail_(normal).png.dds")
				EM.UI.Tabs:setActivePane(2)
				EM.UI.Send:getElement().ToField:SetText(EM.Message.Data.from)
				EM.UI.Send:getElement().SubjectField:SetText("RE: " .. EM.Message.Data.subject)
				EM.UI.Send:getElement().btnSend:SetEnabled(true)
			end

		window.itemBorder4 = UI.CreateFrame("Texture", "MessageItemBorder4", window.texture2)
			window.itemBorder4:SetTexture("Rift", "icon_border.dds")
			window.itemBorder4:SetLayer(20)
			window.itemBorder4:SetPoint("BOTTOMCENTER", window.texture2, "BOTTOMCENTER", 0, -7)

		window.itemBorder3 = UI.CreateFrame("Texture", "MessageItemBorder3", window.texture2)
			window.itemBorder3:SetTexture("Rift", "icon_border.dds")
			window.itemBorder3:SetLayer(20)
			window.itemBorder3:SetPoint("CENTERRIGHT", window.itemBorder4, "CENTERLEFT", 8, 0)

		window.itemBorder2 = UI.CreateFrame("Texture", "MessageItemBorder2", window.texture2)
			window.itemBorder2:SetTexture("Rift", "icon_border.dds")
			window.itemBorder2:SetLayer(20)
			window.itemBorder2:SetPoint("CENTERRIGHT", window.itemBorder3, "CENTERLEFT", 8, 0)

		window.itemBorder1 = UI.CreateFrame("Texture", "MessageItemBorder1", window.texture2)
			window.itemBorder1:SetTexture("Rift", "icon_border.dds")
			window.itemBorder1:SetLayer(20)
			window.itemBorder1:SetPoint("CENTERRIGHT", window.itemBorder2, "CENTERLEFT", 8, 0)

		window.itemBorder5 = UI.CreateFrame("Texture", "MessageItemBorder5", window.texture2)
			window.itemBorder5:SetTexture("Rift", "icon_border.dds")
			window.itemBorder5:SetLayer(20)
			window.itemBorder5:SetPoint("CENTERLEFT", window.itemBorder4, "CENTERRIGHT", -8, 0)

		window.itemBorder6 = UI.CreateFrame("Texture", "MessageItemBorder6", window.texture2)
			window.itemBorder6:SetTexture("Rift", "icon_border.dds")
			window.itemBorder6:SetLayer(20)
			window.itemBorder6:SetPoint("CENTERLEFT", window.itemBorder5, "CENTERRIGHT", -8, 0)

		window.itemBorder7 = UI.CreateFrame("Texture", "MessageItemBorder7", window.texture2)
			window.itemBorder7:SetTexture("Rift", "icon_border.dds")
			window.itemBorder7:SetLayer(20)
			window.itemBorder7:SetPoint("CENTERLEFT", window.itemBorder6, "CENTERRIGHT", -8, 0)

		window.item1 = UI.CreateFrame("Texture", "MessageItem1", window.texture2)
			window.item1:SetLayer(10)
			window.item1:SetPoint("CENTER", window.itemBorder1, "CENTER")

		window.item2 = UI.CreateFrame("Texture", "MessageItem2", window.texture2)
			window.item2:SetLayer(10)
			window.item2:SetPoint("CENTER", window.itemBorder2, "CENTER")

		window.item3 = UI.CreateFrame("Texture", "MessageItem3", window.texture2)
			window.item3:SetLayer(10)
			window.item3:SetPoint("CENTER", window.itemBorder3, "CENTER")

		window.item4 = UI.CreateFrame("Texture", "MessageItem4", window.texture2)
			window.item4:SetLayer(10)
			window.item4:SetPoint("CENTER", window.itemBorder4, "CENTER")

		window.item5 = UI.CreateFrame("Texture", "MessageItem5", window.texture2)
			window.item5:SetLayer(10)
			window.item5:SetPoint("CENTER", window.itemBorder5, "CENTER")

		window.item6 = UI.CreateFrame("Texture", "MessageItem5", window.texture2)
			window.item6:SetLayer(10)
			window.item6:SetPoint("CENTER", window.itemBorder6, "CENTER")

		window.item7 = UI.CreateFrame("Texture", "MessageItem6", window.texture2)
			window.item7:SetLayer(10)
			window.item7:SetPoint("CENTER", window.itemBorder7, "CENTER")

		for i=1,7,1 do
			local ut = "item"..i
			if EM.Message.Data.attachmentsType and type(EM.Message.Data.attachmentsType)=="table" and EM.Message.Data.attachmentsType[i] then
				local item = {}
				if type(EM.Message.Data.attachmentsType[i])=="string" then
					item = Inspect.Item.Detail(EM.Message.Data.attachmentsType[i])
				else
					item.coin = EM.Message.Data.attachmentsType[i]
				end
				if item and item.icon then
					window[ut]:SetTexture("Rift", item.icon)

					window[ut].Event.MouseIn = function()
						Command.Tooltip(item.id)
					end

					window[ut].Event.MouseOut = function()
						Command.Tooltip(nil)
					end

				elseif item and item.coin then
					if item.coin <= 99 then
						window[ut]:SetTexture("Rift", "loot_silver_coins.dds")
					elseif item.coin <= 999 and item.coin > 99 then
						window[ut]:SetTexture("Rift", "loot_gold_coins.dds")
					elseif item.coin > 999 then
						window[ut]:SetTexture("Rift", "loot_platinum_coins.dds")
					end
				else
					window[ut]:SetTexture("Rift","")
				end
			else
				window[ut]:SetTexture("Rift","")
			end
		end

		EM.UI.MessageWindow = window
	else
		window = EM.UI.MessageWindow
		window:SetVisible(true)
		if EM.Message.Data.from then
			window.fromText:SetText(EM.Message.Data.from)
			window.fromText2:SetText(EM.Message.Data.from)
		elseif EM.Message.Data.to then
			window.fromText:SetText(EM.Message.Data.to)
			window.fromText2:SetText(EM.Message.Data.to)
		end
		window.subjectText:SetText(EM.Message.Data.subject)
		window.subjectText2:SetText(EM.Message.Data.subject)
		if EM.Message.Data.expire then
			window.dateText:SetText(EM.Message.Data.expire)
			window.dateText2:SetText(EM.Message.Data.expire)
		elseif EM.Message.Data.timesent then
			window.dateText:SetText(EM.Message.Data.timesent)
			window.dateText2:SetText(EM.Message.Data.timesent)
		end
		if EM.Message.Data.cod then
			window.codText:SetVisible(true)
			window.codText2:SetVisible(true)
		else
			window.codText:SetVisible(false)
			window.codText2:SetVisible(false)
		end
		if EM.Message.Data.body and string.len(EM.Message.Data.body) > 0 then
			window.bodyText:SetText(EM.Message.Data.body)
		else
			window.bodyText:SetText("")
		end
		for i=1,7,1 do
			local ut = "item"..i
			if EM.Message.Data.attachmentsType and type(EM.Message.Data.attachmentsType)=="table" and EM.Message.Data.attachmentsType[i] then
				local item = {}
				if type(EM.Message.Data.attachmentsType[i])=="string" then
					item = Inspect.Item.Detail(EM.Message.Data.attachmentsType[i])
				else
					item.coin = EM.Message.Data.attachmentsType[i]
				end
				if item and item.icon then
					window[ut]:SetTexture("Rift", item.icon)

					window[ut].Event.MouseIn = function()
						Command.Tooltip(item.id)
					end

					window[ut].Event.MouseOut = function()
						Command.Tooltip(nil)
					end

				elseif item and item.coin then
					if item.coin <= 99 then
						window[ut]:SetTexture("Rift", "loot_silver_coins.dds")
					elseif item.coin <= 999 and item.coin > 99 then
						window[ut]:SetTexture("Rift", "loot_gold_coins.dds")
					elseif item.coin > 999 then
						window[ut]:SetTexture("Rift", "loot_platinum_coins.dds")
					end
					window[ut].Event.MouseIn = function()
						Command.Tooltip(nil)
					end
				else
					window[ut]:SetTexture("Rift","")
					window[ut].Event.MouseIn = function()
						Command.Tooltip(nil)
					end
				end
			else
				window[ut]:SetTexture("Rift","")
				window[ut].Event.MouseIn = function()
					Command.Tooltip(nil)
				end
			end
		end
	end
end