using System.Collections.Generic;
using System.Linq;

namespace ZionRobot.Models.Items
{
    class Folder : ZionFoundation.Components.Folder
    {
        private ZionFoundation.GlobalTasks.FolderMonitoring FolderMonitor;
        private void StartMonitoring()
        {
            FolderMonitor = new ZionFoundation.GlobalTasks.FolderMonitoring(FullPath, Monitor_Trigger);
            if (this.Monitor)
            {
                FolderMonitor.Initilize();
            }
        }
        public override void Monitor_Trigger(object sender, System.IO.FileSystemEventArgs e)
        {
            if (!this.ExtensionsToIgnore.Contains(new System.IO.FileInfo(e.FullPath).Extension))
            {
                if (System.IO.File.Exists(e.FullPath))
                    Append(this.Build(e.FullPath, this.Monitor));
            }
        }

        public ZionFoundation.Components.IComponent Build(string path, bool monitor = true)
        {

            System.IO.FileAttributes attr = System.IO.File.GetAttributes(path);

            if ((attr & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory)
                return new Models.Items.Folder(path, monitor);
            else
                return new Models.Items.File(path);
        }

        public Folder(string path, bool monitor = true) : base(path, monitor)
        {
            this.Status = monitor ? "1" : "0";
            this.Monitor = monitor;
            ExtensionsToIgnore = new string[] { ".xml", ".ztd", ".vrf", ".db", ".~tmp" };
            StartMonitoring();
            LoadContent();
        }
        public new List<File> Files
        {
            get
            {
                if (neasted == null)
                    neasted = new List<ZionFoundation.Components.IComponent>();

                CheckFiles();

                return (from item in neasted.ToList()
                        where item.GetType() != GetType()
                        select (Models.Items.File)item).OrderBy(x => x.Name).ToList();
            }
        }
        public new List<Folder> Folders
        {
            get
            {
                if (neasted == null)
                    neasted = new List<ZionFoundation.Components.IComponent>();

                CheckFolders();

                return (from item in neasted.ToList()
                        where item.GetType() == GetType()
                        select (Models.Items.Folder)item).OrderBy(x => x.Name).ToList();
            }
        }
        public override List<ZionFoundation.Components.IComponent> LoadFiles()
        {
            List<ZionFoundation.Components.IComponent> components = new List<ZionFoundation.Components.IComponent>();
            System.IO.Directory.GetFiles(FullPath).ToList().ForEach(x =>
            {
                if (!x.EndsWith(".xml") && !x.EndsWith(".ztd") && !x.EndsWith(".vrf") && !x.EndsWith("Thumbs.db") && !x.EndsWith(".~tmp"))
                    components.Add(new Models.Items.File(x));
            });
            return components;
        }
        public override List<ZionFoundation.Components.IComponent> LoadFolders()
        {
            List<ZionFoundation.Components.IComponent> components = new List<ZionFoundation.Components.IComponent>();
            System.IO.Directory.GetDirectories(FullPath).ToList().ForEach(x =>
            {
                components.Add(new Models.Items.Folder(x, Monitor));
            });
            return components;
        }

        private void CheckFiles()
        {
            List<File> existingFiles = neasted.ToList().Where(child => child.GetType() != GetType()).Select(s => (File)s).ToList();

            System.IO.Directory.GetFiles(FullPath).ToList().ForEach(x =>
            {
                if (!x.EndsWith(".xml") && !x.EndsWith(".ztd") && !x.EndsWith(".vrf") && !x.EndsWith("Thumbs.db") && !x.EndsWith(".~tmp"))
                {
                    if (existingFiles.Where(p => p.FullPath == x).Count() == 0)
                    {
                        neasted.Add(new Models.Items.File(x));
                    }
                }
            });
        }

        private void CheckFolders()
        {
            List<Folder> existingFolders = neasted.Where(child => child.GetType() == GetType()).Select(s => (Folder)s).ToList();

            System.IO.Directory.GetDirectories(FullPath).ToList().ForEach(x =>
            {
                if (existingFolders.Where(p => p.FullPath == x).Count() == 0)
                {
                    neasted.Add(new Models.Items.Folder(x, Monitor));
                }
            });
        }
    }
}
