using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Security;
using System.Text.RegularExpressions;
using Microsoft.SharePoint.Client;

namespace ZionRobot.ZionExchangeManagement
{
	// Token: 0x02000011 RID: 17
	internal class ZionSharePointManager
	{
		// Token: 0x06000059 RID: 89 RVA: 0x000041AB File Offset: 0x000023AB
		public ZionSharePointManager(string userName, string userPassword)
		{
			this.UserName = userName;
			this.UserPassword = userPassword;
		}

		// Token: 0x0600005A RID: 90 RVA: 0x000041C4 File Offset: 0x000023C4
		public void ConnectToSharePointOnline(string siteUrl)
		{
			try
			{
				bool flag = this.context != null;
				if (flag)
				{
					this.context.Dispose();
				}
				this.context = new ClientContext(siteUrl);
				SecureString secureString = new SecureString();
				this.UserPassword.ToList<char>().ForEach(new Action<char>(secureString.AppendChar));
				this.context.Credentials = new SharePointOnlineCredentials(this.UserName, secureString);
				this.site = this.context.Site;
				this.context.Load<Site>(this.site, Array.Empty<Expression<Func<Site, object>>>());
				this.context.ExecuteQuery();
				Console.ForegroundColor = ConsoleColor.Green;
				Console.WriteLine("Connected to " + siteUrl);
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		// Token: 0x0600005B RID: 91 RVA: 0x00004298 File Offset: 0x00002498
		private void CreateFolder(Folder RootFolder, string FolderName)
		{
			this.context.Load<FolderCollection>(RootFolder.Folders, Array.Empty<Expression<Func<FolderCollection, object>>>());
			RootFolder.Folders.Add(FolderName);
			RootFolder.Update();
			this.context.ExecuteQuery();
		}

		// Token: 0x0600005C RID: 92 RVA: 0x000042D2 File Offset: 0x000024D2
		private void LoadFolders(Folder RootFolder)
		{
			this.context.Load<FolderCollection>(RootFolder.Folders, Array.Empty<Expression<Func<FolderCollection, object>>>());
			this.context.ExecuteQuery();
		}

		// Token: 0x0600005D RID: 93 RVA: 0x000042F8 File Offset: 0x000024F8
		public List<List> GetCollections()
		{
			this.context.Load<ListCollection>(this.context.Web.Lists, Array.Empty<Expression<Func<ListCollection, object>>>());
			this.context.ExecuteQuery();
			return this.context.Web.Lists.ToList<List>();
		}

		// Token: 0x0600005E RID: 94 RVA: 0x0000434C File Offset: 0x0000254C
		public List GetCollection(string title)
		{
			this.context.Load<ListCollection>(this.context.Web.Lists, Array.Empty<Expression<Func<ListCollection, object>>>());
			this.context.ExecuteQuery();
			return (from item in this.context.Web.Lists.ToList<List>()
			where item.Title == title
			select item).FirstOrDefault<List>();
		}

		// Token: 0x0600005F RID: 95 RVA: 0x000043C4 File Offset: 0x000025C4
		public Folder GetFolder(List list, string folder)
		{
			List<string> folders = folder.Split(new char[]
			{
				'/'
			}).ToList<string>();
			Folder currentFolder = null;
			using (List<string>.Enumerator enumerator = folders.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.Current;
					bool flag = currentFolder == null;
					if (flag)
					{
						this.context.Load<FolderCollection>(list.RootFolder.Folders, Array.Empty<Expression<Func<FolderCollection, object>>>());
						this.context.ExecuteQuery();
						currentFolder = (from f in list.RootFolder.Folders
						where Regex.IsMatch(f.Name, current)
						select f).FirstOrDefault<Folder>();
						this.LoadFolders(currentFolder);
					}
					else
					{
						Folder x = null;
						currentFolder.Folders.ToList<Folder>().ForEach(delegate(Folder f)
						{
							bool flag3 = Regex.IsMatch(f.Name, current);
							if (flag3)
							{
								x = f;
							}
						});
						Folder temp = (from f in currentFolder.Folders
						where Regex.IsMatch(f.Name, current) || f.Name.StartsWith(current)
						select f).FirstOrDefault<Folder>();
						bool flag2 = temp != null;
						if (!flag2)
						{
							break;
						}
						currentFolder = temp;
						this.LoadFolders(currentFolder);
					}
				}
			}
			return currentFolder;
		}

		// Token: 0x06000060 RID: 96 RVA: 0x0000467C File Offset: 0x0000287C
		public Folder BuildFolder(List list, string folder)
		{
			List<string> folders = folder.Split(new char[]
			{
				'/'
			}).ToList<string>();
			Folder currentFolder = null;
			for (int i = 0; i < folders.Count; i++)
			{
				char[] charsToTrim = new char[]
				{
					',',
					'.',
					' ',
					'#',
					'%',
					'*',
					':',
					'<',
					'>',
					'?',
					'/',
					'|'
				};
				string current = folders[i].TrimEnd(charsToTrim);
				bool flag = currentFolder == null;
				if (flag)
				{
					this.context.Load<FolderCollection>(list.RootFolder.Folders, Array.Empty<Expression<Func<FolderCollection, object>>>());
					this.context.ExecuteQuery();
					currentFolder = (from f in list.RootFolder.Folders
					where Regex.IsMatch(f.Name, current)
					select f).FirstOrDefault<Folder>();
					this.LoadFolders(currentFolder);
				}
				else
				{
					Folder temp = (from f in currentFolder.Folders
					where Regex.IsMatch(f.Name, current) || f.Name.StartsWith(current)
					select f).FirstOrDefault<Folder>();
					bool flag2 = temp != null;
					if (flag2)
					{
						currentFolder = temp;
						this.LoadFolders(currentFolder);
					}
					else
					{
						string tmp = current;
						bool flag3 = folders.Last<string>() == current;
						if (flag3)
						{
							tmp = current;
						}
						this.CreateFolder(currentFolder, tmp);
						this.LoadFolders(currentFolder);
						i--;
					}
				}
			}
			return currentFolder;
		}

		// Token: 0x06000061 RID: 97 RVA: 0x00004910 File Offset: 0x00002B10
		public Microsoft.SharePoint.Client.File UploadFile(Folder folder, string fileName, string path)
		{
			Microsoft.SharePoint.Client.File result;
			try
			{
				this.context.Load<FileCollection>(folder.Files, Array.Empty<Expression<Func<FileCollection, object>>>());
				this.context.ExecuteQuery();
				fileName = Regex.Replace(fileName, "[^0-9a-zA-Z._ ]", string.Empty);
				string uploadedFileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
				string uploadedFileExtension = Path.GetExtension(fileName);
				int index = 1;
				string newFileName = uploadedFileNameWithoutExtension;
				while ((from f in folder.Files
				where f.Name == newFileName + uploadedFileExtension
				select f).Count<Microsoft.SharePoint.Client.File>() > 0)
				{
					newFileName = uploadedFileNameWithoutExtension + string.Format(" ({0})", index);
					index++;
				}
				newFileName += uploadedFileExtension;
				bool flag = System.IO.File.Exists(path);
				byte[] FileContent;
				if (flag)
				{
					FileContent = System.IO.File.ReadAllBytes(path);
				}
				else
				{
					FileContent = Convert.FromBase64String(path);
				}
				FileCreationInformation fileCreationInformation = new FileCreationInformation();
				fileCreationInformation.ContentStream = new MemoryStream(FileContent);
				fileCreationInformation.Url = newFileName;
				Microsoft.SharePoint.Client.File file = folder.Files.Add(fileCreationInformation);
				this.context.Load<Microsoft.SharePoint.Client.File>(file, Array.Empty<Expression<Func<Microsoft.SharePoint.Client.File, object>>>());
				this.context.ExecuteQuery();
				result = file;
			}
			catch (Exception ex)
			{
				result = null;
			}
			return result;
		}

		// Token: 0x06000062 RID: 98 RVA: 0x00004B0C File Offset: 0x00002D0C
		public string DownloadFile(Folder folder, string name, string downloadTo = "./")
		{
			string result;
			try
			{
				this.context.Load<FileCollection>(folder.Files, Array.Empty<Expression<Func<FileCollection, object>>>());
				this.context.ExecuteQuery();
				Microsoft.SharePoint.Client.File file = (from f in folder.Files
				where f.Name == name
				select f).FirstOrDefault<Microsoft.SharePoint.Client.File>();
				Console.ForegroundColor = ConsoleColor.Yellow;
				Console.WriteLine(file.ServerRelativeUrl);
				FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(this.context, file.ServerRelativeUrl);
				string fileName = Path.Combine("./", file.Name);
				using (FileStream fileStream = System.IO.File.Create(fileName))
				{
					fileInfo.Stream.CopyTo(fileStream);
				}
				result = fileName;
			}
			catch
			{
				result = string.Empty;
			}
			return result;
		}

		// Token: 0x0400001D RID: 29
		private ClientContext context;

		// Token: 0x0400001E RID: 30
		private Site site;

		// Token: 0x0400001F RID: 31
		public string UserName;

		// Token: 0x04000020 RID: 32
		public string UserPassword;
	}
}
