using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ZionFoundation.Components;
using ZionFoundation.GlobalTasks;

namespace ZionRobot.Models.Items
{
	// Token: 0x0200002A RID: 42
	internal class Folder : Folder
	{
		// Token: 0x06000138 RID: 312 RVA: 0x00007940 File Offset: 0x00005B40
		private void StartMonitoring()
		{
			this.FolderMonitor = new FolderMonitoring(base.FullPath, new FileSystemEventHandler(this.Monitor_Trigger));
			bool monitor = base.Monitor;
			if (monitor)
			{
				this.FolderMonitor.Initilize(true);
			}
		}

		// Token: 0x06000139 RID: 313 RVA: 0x00007988 File Offset: 0x00005B88
		public override void Monitor_Trigger(object sender, FileSystemEventArgs e)
		{
			bool flag = !base.ExtensionsToIgnore.Contains(new FileInfo(e.FullPath).Extension);
			if (flag)
			{
				bool flag2 = System.IO.File.Exists(e.FullPath);
				if (flag2)
				{
					base.Append(this.Build(e.FullPath, base.Monitor));
				}
			}
		}

		// Token: 0x0600013A RID: 314 RVA: 0x000079E4 File Offset: 0x00005BE4
		public IComponent Build(string path, bool monitor = true)
		{
			FileAttributes attr = System.IO.File.GetAttributes(path);
			bool flag = (attr & FileAttributes.Directory) == FileAttributes.Directory;
			IComponent result;
			if (flag)
			{
				result = new Folder(path, monitor);
			}
			else
			{
				result = new ZionRobot.Models.Items.File(path);
			}
			return result;
		}

		// Token: 0x0600013B RID: 315 RVA: 0x00007A1C File Offset: 0x00005C1C
		public Folder(string path, bool monitor = true) : base(path, monitor)
		{
			base.Status = (monitor ? "1" : "0");
			base.Monitor = monitor;
			base.ExtensionsToIgnore = new string[]
			{
				".xml",
				".ztd",
				".vrf",
				".db",
				".~tmp"
			};
			this.StartMonitoring();
			base.LoadContent();
		}

		// Token: 0x17000076 RID: 118
		// (get) Token: 0x0600013C RID: 316 RVA: 0x00007A94 File Offset: 0x00005C94
		public new List<ZionRobot.Models.Items.File> Files
		{
			get
			{
				bool flag = this.neasted == null;
				if (flag)
				{
					this.neasted = new List<IComponent>();
				}
				this.CheckFiles();
				return (from item in this.neasted.ToList<IComponent>()
				where item.GetType() != base.GetType()
				select (ZionRobot.Models.Items.File)item into x
				orderby x.Name
				select x).ToList<ZionRobot.Models.Items.File>();
			}
		}

		// Token: 0x17000077 RID: 119
		// (get) Token: 0x0600013D RID: 317 RVA: 0x00007B30 File Offset: 0x00005D30
		public new List<Folder> Folders
		{
			get
			{
				bool flag = this.neasted == null;
				if (flag)
				{
					this.neasted = new List<IComponent>();
				}
				this.CheckFolders();
				return (from item in this.neasted.ToList<IComponent>()
				where item.GetType() == base.GetType()
				select (Folder)item into x
				orderby x.Name
				select x).ToList<Folder>();
			}
		}

		// Token: 0x0600013E RID: 318 RVA: 0x00007BCC File Offset: 0x00005DCC
		public override List<IComponent> LoadFiles()
		{
			List<IComponent> components = new List<IComponent>();
			System.IO.Directory.GetFiles(base.FullPath).ToList<string>().ForEach(delegate(string x)
			{
				bool flag = !x.EndsWith(".xml") && !x.EndsWith(".ztd") && !x.EndsWith(".vrf") && !x.EndsWith("Thumbs.db") && !x.EndsWith(".~tmp");
				if (flag)
				{
					components.Add(new ZionRobot.Models.Items.File(x));
				}
			});
			return components;
		}

		// Token: 0x0600013F RID: 319 RVA: 0x00007C18 File Offset: 0x00005E18
		public override List<IComponent> LoadFolders()
		{
			List<IComponent> components = new List<IComponent>();
			System.IO.Directory.GetDirectories(base.FullPath).ToList<string>().ForEach(delegate(string x)
			{
				components.Add(new Folder(x, this.Monitor));
			});
			return components;
		}

		// Token: 0x06000140 RID: 320 RVA: 0x00007C6C File Offset: 0x00005E6C
		private void CheckFiles()
		{
			List<ZionRobot.Models.Items.File> existingFiles = (from child in this.neasted.ToList<IComponent>()
			where child.GetType() != this.GetType()
			select child into s
			select (ZionRobot.Models.Items.File)s).ToList<ZionRobot.Models.Items.File>();
			System.IO.Directory.GetFiles(base.FullPath).ToList<string>().ForEach(delegate(string x)
			{
				bool flag = !x.EndsWith(".xml") && !x.EndsWith(".ztd") && !x.EndsWith(".vrf") && !x.EndsWith("Thumbs.db") && !x.EndsWith(".~tmp");
				if (flag)
				{
					bool flag2 = (from p in existingFiles
					where p.FullPath == x
					select p).Count<ZionRobot.Models.Items.File>() == 0;
					if (flag2)
					{
						this.neasted.Add(new ZionRobot.Models.Items.File(x));
					}
				}
			});
		}

		// Token: 0x06000141 RID: 321 RVA: 0x00007CF4 File Offset: 0x00005EF4
		private void CheckFolders()
		{
			List<Folder> existingFolders = (from child in this.neasted
			where child.GetType() == this.GetType()
			select child into s
			select (Folder)s).ToList<Folder>();
			System.IO.Directory.GetDirectories(base.FullPath).ToList<string>().ForEach(delegate(string x)
			{
				bool flag = (from p in existingFolders
				where p.FullPath == x
				select p).Count<Folder>() == 0;
				if (flag)
				{
					this.neasted.Add(new Folder(x, this.Monitor));
				}
			});
		}

		// Token: 0x0400007D RID: 125
		private FolderMonitoring FolderMonitor;
	}
}
