public bool TryGetCollectionId(string collectionName, out int collectionId)
        {
            MovieCollectionMatch match = _collectionStorage.GetMatches().Find(m => m.ItemName == collectionName);

            collectionId = match == null ? 0 : match.Id;
            return(collectionId != 0);
        }
        public bool TryGetCollectionId(string collectionName, out int collectionId)
        {
            MovieCollectionMatch match = _collectionStorage.GetMatches().Find(m => string.Equals(m.ItemName, collectionName, StringComparison.OrdinalIgnoreCase));

            collectionId = match == null ? 0 : match.Id;
            return(collectionId != 0);
        }
        private void SaveMatchToPersistentCache(Movie movieDetails, string movieName)
        {
            var onlineMatch = new MovieMatch
            {
                Id          = movieDetails.Id,
                ItemName    = movieName,
                MovieDBName = movieDetails.Title
            };

            _storage.TryAddMatch(onlineMatch);

            // Save collection mapping, if available
            if (movieDetails.Collection != null)
            {
                var collectionMatch = new MovieCollectionMatch
                {
                    Id       = movieDetails.Collection.Id,
                    ItemName = movieDetails.Collection.Name
                };
                _collectionStorage.TryAddMatch(collectionMatch);
            }
        }
        protected bool TryMatch(string movieName, int year, string language, bool cacheOnly, out Movie movieDetail)
        {
            movieDetail = null;
            try
            {
                // Prefer memory cache
                CheckCacheAndRefresh();
                if (_memoryCache.TryGetValue(movieName, out movieDetail))
                {
                    return(true);
                }

                // Load cache or create new list
                List <MovieMatch> matches = _storage.GetMatches();

                // Init empty
                movieDetail = null;

                // Use cached values before doing online query
                MovieMatch match = matches.Find(m =>
                                                string.Equals(m.ItemName, movieName, StringComparison.OrdinalIgnoreCase) ||
                                                string.Equals(m.MovieDBName, movieName, StringComparison.OrdinalIgnoreCase));
                ServiceRegistration.Get <ILogger>().Debug("MovieTheMovieDbMatcher: Try to lookup movie \"{0}\" from cache: {1}", movieName, match != null && match.Id != 0);

                // Try online lookup
                if (!Init())
                {
                    return(false);
                }

                // If this is a known movie, only return the movie details.
                if (match != null)
                {
                    return(match.Id != 0 && _movieDb.GetMovie(match.Id, out movieDetail));
                }

                if (cacheOnly)
                {
                    return(false);
                }

                List <MovieSearchResult> movies;
                if (_movieDb.SearchMovieUnique(movieName, year, language, out movies))
                {
                    MovieSearchResult movieResult = movies[0];
                    ServiceRegistration.Get <ILogger>().Debug("MovieTheMovieDbMatcher: Found unique online match for \"{0}\": \"{1}\"", movieName, movieResult.Title);
                    if (_movieDb.GetMovie(movies[0].Id, out movieDetail))
                    {
                        // Add this match to cache
                        MovieMatch onlineMatch = new MovieMatch
                        {
                            Id          = movieDetail.Id,
                            ItemName    = movieName,
                            MovieDBName = movieDetail.Title
                        };

                        // Save collection mapping, if available
                        if (movieDetail.Collection != null)
                        {
                            MovieCollectionMatch collectionMatch = new MovieCollectionMatch
                            {
                                Id       = movieDetail.Collection.Id,
                                ItemName = movieDetail.Collection.Name
                            };
                            _collectionStorage.TryAddMatch(collectionMatch);
                        }

                        // Save cache
                        _storage.TryAddMatch(onlineMatch);
                    }
                    return(true);
                }
                ServiceRegistration.Get <ILogger>().Debug("MovieTheMovieDbMatcher: No unique match found for \"{0}\"", movieName);
                // Also save "non matches" to avoid retrying
                _storage.TryAddMatch(new MovieMatch {
                    ItemName = movieName
                });
                return(false);
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Debug("MovieTheMovieDbMatcher: Exception while processing movie {0}", ex, movieName);
                return(false);
            }
            finally
            {
                if (movieDetail != null)
                {
                    _memoryCache.TryAdd(movieName, movieDetail);
                }
            }
        }