/// <summary>
 /// Action performed when RemoveItemsCommand is called
 /// </summary>
 private void RemoveItemsAction()
 {
     for (int i = 0; i < WorkingItemsList.Count; i++)
     {
         if (WorkingItemsList[i].IsSelected)
         {
             WorkingItemsList.Remove(WorkingItemsList[i]);
             i--;
         }
     }
 }
        /// <summary>
        /// Adds item to the working items list
        /// </summary>
        /// <param name="item"></param>
        public void AddItemToWorkingList(WorkingItemVm item)
        {
            // Return if the item already exists
            if (WorkingItemsList.ToList().Find(x => x.DisplayName == item.DisplayName) != null)
            {
                return;
            }

            // Add it to and re-order list (TODO: change this, it's ugly and potentially slow)
            WorkingItemsList.Add(item);
            WorkingItemsList = new ObservableImmutableList <WorkingItemVm>(WorkingItemsList.OrderBy(x => x.DisplayName));
        }
        /// <summary>
        /// Processes profile's Working directory
        /// </summary>
        public void ProcessWorkingDirectory()
        {
            // Return if null or no profile is selected
            if (MainWindowVm.Instance == null || MainWindowVm.Instance.SelectedWorkingProfile == null)
            {
                return;
            }

            WorkingItemsList.Clear();

            try
            {
                // Loop through all files
                foreach (var item in IOHelper.GetAllFilesFromDir(MainWindowVm.Instance.SelectedWorkingProfile.WorkingDirectory)
                         .Where(t => String.Equals(t.Extension, MainWindowVm.Instance.SelectedWorkingProfile.IndexExtension, StringComparison.CurrentCultureIgnoreCase))
                         .Select(file => new WorkingItemVm(Path.GetFileNameWithoutExtension(file.Name), file.Name, file.FullName, file.DirectoryName)))
                {
                    // Add it to the list
                    AddItemToWorkingList(item);
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                WindowLog.Error("DIR_NOT_FOUND", null, MainWindowVm.Instance.SelectedWorkingProfile.WorkingDirectory);
            }

            try
            {
                // Loop through all the directories (since there may be files which are unpacked but not existing in the pack dir)
                foreach (var item in IOHelper.GetAllFirstDirectories(MainWindowVm.Instance.SelectedWorkingProfile.UnpackDirectory))
                {
                    if (WorkingItemsList.FirstOrDefault(x => String.Equals(x.DisplayName, item.Name, StringComparison.CurrentCultureIgnoreCase)) == null)
                    {
                        AddItemToWorkingList(new WorkingItemVm(
                                                 item.Name,
                                                 item.Name + MainWindowVm.Instance.SelectedWorkingProfile.IndexExtension,
                                                 item.FullName + MainWindowVm.Instance.SelectedWorkingProfile.IndexExtension,
                                                 item.FullName));
                    }
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                WindowLog.Error("DIR_NOT_FOUND", null, MainWindowVm.Instance.SelectedWorkingProfile.UnpackDirectory);
            }

            WorkingItemsList = new ObservableImmutableList <WorkingItemVm>(WorkingItemsList.OrderBy(x => x.DisplayName));
        }
 /// <summary>
 /// Filters item by string
 /// </summary>
 private void FilterItemsBySearchText()
 {
     if (!String.IsNullOrWhiteSpace(SearchText))
     {
         foreach (var item in WorkingItemsList.Where(x => !x.DisplayName.ToLower().Contains(SearchText.ToLower())))
         {
             item.IsVisible = false;
         }
     }
     else
     {
         foreach (var item in WorkingItemsList.Where(x => !x.IsVisible))
         {
             item.IsVisible = true;
         }
     }
 }
        /// <summary>
        /// On file renamed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FswOnRenamed(object sender, RenamedEventArgs e)
        {
            var obj =
                WorkingItemsList.FirstOrDefault(
                    x => String.Equals(x.Filename, e.OldName, StringComparison.CurrentCultureIgnoreCase));

            if (obj == null)
            {
                return;
            }

            var fileInfo = new FileInfo(e.FullPath);

            obj.DisplayName   = StringHelpers.TrimExtension(e.Name);
            obj.Filename      = e.Name;
            obj.FullPath      = e.FullPath;
            obj.DirectoryPath = fileInfo.DirectoryName;
        }
        /// <summary>
        /// On file deleted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FswOnDeleted(object sender, FileSystemEventArgs e)
        {
            var item =
                WorkingItemsList.FirstOrDefault(x => String.Equals(x.Filename, e.Name, StringComparison.CurrentCultureIgnoreCase));

            if (item != null)
            {
                if (item.PackFileCommand.CanExecute(null))
                {
                    return;
                }

                lock (Obj)
                {
                    WorkingItemsList.Remove(item);
                }
            }
        }
        /// <summary>
        /// On file created
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="fileSystemEventArgs"></param>
        private void FswOnCreated(object sender, FileSystemEventArgs e)
        {
            var fileInfo = new FileInfo(e.FullPath);

            if (!String.Equals(fileInfo.Extension, MainWindowVm.Instance.SelectedWorkingProfile.IndexExtension,
                               StringComparison.CurrentCultureIgnoreCase))
            {
                return;
            }

            if (WorkingItemsList.FirstOrDefault(x => x.DisplayName == Path.GetFileNameWithoutExtension(fileInfo.Name)) == null)
            {
                lock (Obj)
                {
                    WorkingItemsList.Add(new WorkingItemVm(StringHelpers.TrimExtension(fileInfo.Name), fileInfo.Name, fileInfo.FullName, fileInfo.DirectoryName));
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        public void OnFileDropped(object data)
        {
            var dataObject = (System.Windows.IDataObject)data;
            var items      = (string[])dataObject.GetData(DataFormats.FileDrop);

            foreach (var item in items)
            {
                if (File.Exists(item)) // File, then unpack it
                {
                    // File info object
                    var fi = new FileInfo(item);

                    if (fi.Extension != MainWindowVm.Instance.SelectedWorkingProfile.IndexExtension)
                    {
                        UserInput.ShowMessage("DROP_EIX_OR_DIR");
                        continue;
                    }

                    var workingItem = WorkingItemsList.FirstOrDefault(x => x.Filename == fi.Name);

                    // Create working item
                    if (workingItem == null)
                    {
                        workingItem = new WorkingItemVm(Path.GetFileNameWithoutExtension(fi.Name), fi.Name, fi.FullName,
                                                        fi.DirectoryName);

                        // Add it to the list
                        AddItemToWorkingList(workingItem);
                    }

                    // Unpack it
                    if (workingItem.UnpackFileCommand.CanExecute(null))
                    {
                        workingItem.UnpackFileCommand.Execute(null);
                    }
                }
                else if (Directory.Exists(item)) // Directory, then pack it
                {
                    // Dir info
                    var di = new DirectoryInfo(item);

                    var workingItem = WorkingItemsList.FirstOrDefault(x => x.DisplayName == di.Name);

                    // Create working item
                    if (workingItem == null)
                    {
                        workingItem = new WorkingItemVm(di.Name,
                                                        di.Name + MainWindowVm.Instance.SelectedWorkingProfile.IndexExtension, di.FullName + MainWindowVm.Instance.SelectedWorkingProfile.IndexExtension, di.FullName);

                        // Add it to the list
                        AddItemToWorkingList(workingItem);
                    }

                    // Pack it
                    if (workingItem.PackFileCommand.CanExecute(null))
                    {
                        workingItem.PackFileCommand.Execute(null);
                    }
                }
            }
        }