/// <summary>
 ///     Returns true or false depending upon whether the first or second file name starts with the given filter.
 /// </summary>
 /// <param name="fileComparisonViewModel">The file comparison object to looking into</param>
 /// <param name="filter">The filter</param>
 /// <returns>True if the name exists. False otherwise</returns>
 private static bool HasMatchingFileName(FileComparisonViewModel fileComparisonViewModel, string filter)
 {
     return
         (fileComparisonViewModel.FirstFileDisplayName.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
          fileComparisonViewModel.SecondFileDisplayName.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0);
 }
        /// <summary>
        ///     Initializes the Shelveset Comparison View Model
        /// </summary>
        /// <param name="firstShelveset">The first shelveset.</param>
        /// <param name="secondShelveset">The second shelveset</param>
        /// <exception cref="ArgumentNullException">
        ///     The <paramref name="firstShelveset"/>
        ///     or the <paramref name="secondShelveset"/> is <see langword="null" />.
        /// </exception>
        public void Initialize(Shelveset firstShelveset, Shelveset secondShelveset)
        {
            if (firstShelveset == null)
            {
                throw new ArgumentNullException(nameof(firstShelveset));
            }

            if (secondShelveset == null)
            {
                throw new ArgumentNullException(nameof(secondShelveset));
            }

            var tfcontextManager = this.GetService <ITeamFoundationContextManager>();
            var vcs = tfcontextManager.CurrentContext.TeamProjectCollection.GetService <VersionControlServer>();

            if (vcs == null)
            {
                this.SummaryText = Resources.ConnectionErrorMessage;
                return;
            }

            this.FirstShelvesetName  = firstShelveset.Name;
            this.SecondShelvesetName = secondShelveset.Name;

            this.files.Clear();
            var firstShelvesetChanges  = vcs.QueryShelvedChanges(firstShelveset)[0].PendingChanges;
            var secondShelvesetChanges = vcs.QueryShelvedChanges(secondShelveset)[0].PendingChanges;
            var orderedCollection      = new SortedList <string, FileComparisonViewModel>();

            var sameContentFileCount = 0;
            var commonFilesCount     = 0;

            foreach (var pendingChange in firstShelvesetChanges)
            {
                var matchingFile = secondShelvesetChanges.FirstOrDefault(s => s.ItemId == pendingChange.ItemId) ??
                                   secondShelvesetChanges.FirstOrDefault(s => s.LocalOrServerItem == pendingChange.LocalOrServerItem);

                var sameContent    = matchingFile != null && AreFilesInPendingChangesSame(pendingChange, matchingFile);
                var comparisonItem = new FileComparisonViewModel
                {
                    FirstFile  = pendingChange,
                    SecondFile = matchingFile,
                    Color      =
                        sameContent
                            ? ColorMatchingFiles
                            : (matchingFile != null) ? ColorDifferentFiles : ColorNoMatchingFile
                };

                orderedCollection.Add(pendingChange.LocalOrServerFolder + "/" + pendingChange.FileName, comparisonItem);
                if (sameContent)
                {
                    sameContentFileCount++;
                }

                if (matchingFile != null)
                {
                    commonFilesCount++;
                }
            }

            foreach (var pendingChange in secondShelvesetChanges)
            {
                if (orderedCollection.ContainsKey(pendingChange.LocalOrServerFolder + "/" + pendingChange.FileName))
                {
                    continue;
                }

                var isThereAreNamedFile = FindItemWithSameItemId(orderedCollection, pendingChange.ItemId);
                if (isThereAreNamedFile != null)
                {
                    continue;
                }

                var comparisonItem = new FileComparisonViewModel
                {
                    SecondFile = pendingChange,
                    Color      = ColorNoMatchingFile
                };

                orderedCollection.Add(pendingChange.LocalOrServerFolder + "/" + pendingChange.FileName, comparisonItem);
            }

            foreach (var item in orderedCollection.Keys)
            {
                this.files.Add(orderedCollection[item]);
            }

            if (firstShelveset.Name == secondShelveset.Name && firstShelveset.OwnerName == secondShelveset.OwnerName)
            {
                this.SummaryText            = Resources.SameShelvesetMessage;
                this.TotalNumberOfFiles     = firstShelvesetChanges.Length;
                this.NumberOfDifferentFiles = 0;
                this.NumberOfMatchingFiles  = firstShelvesetChanges.Length;
            }
            else
            {
                this.SummaryText = string.Format(CultureInfo.CurrentCulture, Resources.SummaryMessage, commonFilesCount,
                                                 sameContentFileCount, orderedCollection.Count - sameContentFileCount);
                this.TotalNumberOfFiles     = commonFilesCount;
                this.NumberOfMatchingFiles  = sameContentFileCount;
                this.NumberOfDifferentFiles = orderedCollection.Count - sameContentFileCount;
            }
        }