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

namespace ZionRobot.Models.Items
{
	// Token: 0x02000032 RID: 50
	internal class Folder : Folder
	{
		// Token: 0x0600017E RID: 382 RVA: 0x00008BB0 File Offset: 0x00006DB0
		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: 0x0600017F RID: 383 RVA: 0x00008BF8 File Offset: 0x00006DF8
		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: 0x06000180 RID: 384 RVA: 0x00008C54 File Offset: 0x00006E54
		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: 0x06000181 RID: 385 RVA: 0x00008C8C File Offset: 0x00006E8C
		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: 0x17000095 RID: 149
		// (get) Token: 0x06000182 RID: 386 RVA: 0x00008D04 File Offset: 0x00006F04
		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: 0x17000096 RID: 150
		// (get) Token: 0x06000183 RID: 387 RVA: 0x00008DA0 File Offset: 0x00006FA0
		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: 0x06000184 RID: 388 RVA: 0x00008E3C File Offset: 0x0000703C
		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: 0x06000185 RID: 389 RVA: 0x00008E88 File Offset: 0x00007088
		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: 0x06000186 RID: 390 RVA: 0x00008EDC File Offset: 0x000070DC
		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: 0x06000187 RID: 391 RVA: 0x00008F64 File Offset: 0x00007164
		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: 0x04000091 RID: 145
		private FolderMonitoring FolderMonitor;
	}
}
