using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;

namespace ZionRobot.WebService
{
	// Token: 0x02000017 RID: 23
	internal class ZionOutlookManager
	{
		// Token: 0x06000089 RID: 137 RVA: 0x000055CC File Offset: 0x000037CC
		public ZionOutlookManager(string userName, string appClientId, string apiKey, string appTenentId, string scopeUrl = null, string exchangeServiceUrl = null)
		{
			this.UserName = userName;
			this.AppClientId = appClientId;
			this.ApiKey = apiKey;
			this.AppTenentId = appTenentId;
			this.ScopeUrl = (scopeUrl ?? "https://outlook.office365.com/.default");
			this.ExchangeServiceUrl = (exchangeServiceUrl ?? "https://outlook.office365.com/EWS/Exchange.asmx");
		}

		// Token: 0x0600008A RID: 138 RVA: 0x00005620 File Offset: 0x00003820
		public void blah()
		{
			ExchangeService service = this.makeConnectionApiKey().Result;
			SearchFilter searchFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
			FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, new ItemView(100)
			{
				OrderBy = 
				{
					{
						ItemSchema.DateTimeReceived,
						SortDirection.Descending
					}
				}
			});
			foreach (Item item in findResults)
			{
				EmailMessage email = (EmailMessage)item;
				email.Load(new PropertySet(new PropertyDefinitionBase[]
				{
					ItemSchema.Id
				}));
			}
		}

		// Token: 0x0600008B RID: 139 RVA: 0x000056D0 File Offset: 0x000038D0
		public void SendEmail(string to, string subject, string body, string saveEmlDir = "")
		{
			Task<string> sendTask = this.Send(to, "Teste", "testetetetetetetete");
			sendTask.Wait();
			string result = sendTask.Result;
			bool flag = !string.IsNullOrEmpty(saveEmlDir);
			if (flag)
			{
				ExchangeService service = this.makeConnectionApiKey().Result;
				bool flag2 = service != null;
				if (flag2)
				{
					Directory.CreateDirectory(saveEmlDir);
					string bar = saveEmlDir.Contains("/") ? "/" : "\\";
					saveEmlDir = (saveEmlDir.EndsWith(bar) ? saveEmlDir : (saveEmlDir + bar));
					FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.SentItems, new ItemView(20)
					{
						PropertySet = new PropertySet(BasePropertySet.IdOnly)
					});
					foreach (Item item in findResults)
					{
						string i = item.Id.UniqueId;
						bool flag3 = i == result;
						if (flag3)
						{
							PropertySet prop = new PropertySet(new PropertyDefinitionBase[]
							{
								ItemSchema.MimeContent
							});
							item.Load(prop);
						}
					}
					string emailId = result;
					EmailMessage email = EmailMessage.Bind(service, emailId);
					email.Load(new PropertySet(new PropertyDefinitionBase[]
					{
						ItemSchema.MimeContent
					}));
					MimeContent mimeContent = email.MimeContent;
					using (FileStream fileStream = new FileStream(saveEmlDir + subject + ".eml", FileMode.Create))
					{
						fileStream.Write(mimeContent.Content, 0, mimeContent.Content.Length);
					}
				}
			}
		}

		// Token: 0x0600008C RID: 140 RVA: 0x0000588C File Offset: 0x00003A8C
		[DebuggerStepThrough]
		private Task<ExchangeService> makeConnectionApiKey()
		{
			ZionOutlookManager.<makeConnectionApiKey>d__9 <makeConnectionApiKey>d__ = new ZionOutlookManager.<makeConnectionApiKey>d__9();
			<makeConnectionApiKey>d__.<>t__builder = AsyncTaskMethodBuilder<ExchangeService>.Create();
			<makeConnectionApiKey>d__.<>4__this = this;
			<makeConnectionApiKey>d__.<>1__state = -1;
			<makeConnectionApiKey>d__.<>t__builder.Start<ZionOutlookManager.<makeConnectionApiKey>d__9>(ref <makeConnectionApiKey>d__);
			return <makeConnectionApiKey>d__.<>t__builder.Task;
		}

		// Token: 0x0600008D RID: 141 RVA: 0x000058D0 File Offset: 0x00003AD0
		[DebuggerStepThrough]
		private Task<string> Send(string to, string subject, string body)
		{
			ZionOutlookManager.<Send>d__10 <Send>d__ = new ZionOutlookManager.<Send>d__10();
			<Send>d__.<>t__builder = AsyncTaskMethodBuilder<string>.Create();
			<Send>d__.<>4__this = this;
			<Send>d__.to = to;
			<Send>d__.subject = subject;
			<Send>d__.body = body;
			<Send>d__.<>1__state = -1;
			<Send>d__.<>t__builder.Start<ZionOutlookManager.<Send>d__10>(ref <Send>d__);
			return <Send>d__.<>t__builder.Task;
		}

		// Token: 0x04000033 RID: 51
		public string UserName;

		// Token: 0x04000034 RID: 52
		public string AppClientId;

		// Token: 0x04000035 RID: 53
		public string ApiKey;

		// Token: 0x04000036 RID: 54
		public string AppTenentId;

		// Token: 0x04000037 RID: 55
		public string ScopeUrl;

		// Token: 0x04000038 RID: 56
		public string ExchangeServiceUrl;
	}
}
