private void AddEpisodes(ITraktSyncCollectionPost syncCollectionPost)
        {
            if (syncCollectionPost.Episodes == null)
            {
                syncCollectionPost.Episodes = new List <ITraktSyncCollectionPostEpisode>();
            }

            foreach (ITraktEpisode episode in _episodes)
            {
                (syncCollectionPost.Episodes as List <ITraktSyncCollectionPostEpisode>).Add(CreateSyncCollectionPostEpisode(episode));
            }

            foreach (PostBuilderObjectWithMetadata <ITraktEpisode> episodeEntry in _episodesWithMetadata)
            {
                (syncCollectionPost.Episodes as List <ITraktSyncCollectionPostEpisode>).Add(CreateSyncCollectionPostEpisode(episodeEntry.Object, episodeEntry.Metadata, episodeEntry.CollectedAt));
            }

            foreach (PostBuilderCollectedObject <ITraktEpisode> episodeEntry in _collectedEpisodes.CollectedEpisodes)
            {
                (syncCollectionPost.Episodes as List <ITraktSyncCollectionPostEpisode>).Add(CreateSyncCollectionPostEpisode(episodeEntry.Object, null, episodeEntry.CollectedAt));
            }

            foreach (PostBuilderObjectWithMetadata <ITraktEpisode> episodeEntry in _episodesAndMetadata.EpisodesAndMetadata)
            {
                (syncCollectionPost.Episodes as List <ITraktSyncCollectionPostEpisode>).Add(CreateSyncCollectionPostEpisode(episodeEntry.Object, episodeEntry.Metadata, episodeEntry.CollectedAt));
            }
        }
        private void AddMovies(ITraktSyncCollectionPost syncCollectionPost)
        {
            if (syncCollectionPost.Movies == null)
            {
                syncCollectionPost.Movies = new List <ITraktSyncCollectionPostMovie>();
            }

            foreach (ITraktMovie movie in _movies)
            {
                (syncCollectionPost.Movies as List <ITraktSyncCollectionPostMovie>).Add(CreateSyncCollectionPostMovie(movie));
            }

            foreach (PostBuilderObjectWithMetadata <ITraktMovie> movieEntry in _moviesWithMetadata)
            {
                (syncCollectionPost.Movies as List <ITraktSyncCollectionPostMovie>).Add(CreateSyncCollectionPostMovie(movieEntry.Object, movieEntry.Metadata, movieEntry.CollectedAt));
            }

            foreach (PostBuilderCollectedObject <ITraktMovie> movieEntry in _collectedMovies.CollectedMovies)
            {
                (syncCollectionPost.Movies as List <ITraktSyncCollectionPostMovie>).Add(CreateSyncCollectionPostMovie(movieEntry.Object, null, movieEntry.CollectedAt));
            }

            foreach (PostBuilderObjectWithMetadata <ITraktMovie> movieEntry in _moviesAndMetadata.MoviesAndMetadata)
            {
                (syncCollectionPost.Movies as List <ITraktSyncCollectionPostMovie>).Add(CreateSyncCollectionPostMovie(movieEntry.Object, movieEntry.Metadata, movieEntry.CollectedAt));
            }
        }
        public ITraktSyncCollectionRemovePostResponse RemoveCollectionItems(ITraktSyncCollectionPost collectionRemovePost)
        {
            ITraktResponse <ITraktSyncCollectionRemovePostResponse> response = new TraktResponse <ITraktSyncCollectionRemovePostResponse>();

            try
            {
                response = Task.Run(() => base.Sync.RemoveCollectionItemsAsync(collectionRemovePost)).Result;
            }
            catch (AggregateException aggregateException)
            {
                UnwrapAggregateException(aggregateException);
            }
            return(response.Value);
        }
        private void ValidateCollectionPost(ITraktSyncCollectionPost collectionPost)
        {
            if (collectionPost == null)
            {
                throw new ArgumentNullException(nameof(collectionPost), "collection post must not be null");
            }

            IEnumerable <ITraktSyncCollectionPostMovie>   movies   = collectionPost.Movies;
            IEnumerable <ITraktSyncCollectionPostShow>    shows    = collectionPost.Shows;
            IEnumerable <ITraktSyncCollectionPostEpisode> episodes = collectionPost.Episodes;

            bool bHasNoMovies   = movies == null || !movies.Any();
            bool bHasNoShows    = shows == null || !shows.Any();
            bool bHasNoEpisodes = episodes == null || !episodes.Any();

            if (bHasNoMovies && bHasNoShows && bHasNoEpisodes)
            {
                throw new ArgumentException("no collection items set");
            }
        }
        public TraktSyncEpisodesResult SyncSeries()
        {
            _mediaPortalServices.GetLogger().Info("Trakt: start sync series");

            ValidateAuthorization();

            TraktSyncEpisodesResult  syncEpisodesResult     = new TraktSyncEpisodesResult();
            TraktEpisodes            traktEpisodes          = _traktCache.RefreshSeriesCache();
            IList <Episode>          traktUnWatchedEpisodes = traktEpisodes.UnWatched;
            IList <EpisodeWatched>   traktWatchedEpisodes   = traktEpisodes.Watched;
            IList <EpisodeCollected> traktCollectedEpisodes = traktEpisodes.Collected;

            Guid[] types =
            {
                MediaAspect.ASPECT_ID,            EpisodeAspect.ASPECT_ID, VideoAspect.ASPECT_ID, ImporterAspect.ASPECT_ID,
                ProviderResourceAspect.ASPECT_ID, ExternalIdentifierAspect.ASPECT_ID
            };
            var contentDirectory = _mediaPortalServices.GetServerConnectionManager().ContentDirectory;

            if (contentDirectory == null)
            {
                throw new MediaLibraryNotConnectedException("ML not connected");
            }

            Guid?           userProfile = null;
            IUserManagement userProfileDataManagement = _mediaPortalServices.GetUserManagement();

            if (userProfileDataManagement != null && userProfileDataManagement.IsValidUser)
            {
                userProfile = userProfileDataManagement.CurrentUser.ProfileId;
            }

            #region Get data from local database

            IList <MediaItem> localEpisodes = contentDirectory.SearchAsync(new MediaItemQuery(types, null, null), true, userProfile, false).Result;

            if (localEpisodes.Any())
            {
                syncEpisodesResult.CollectedInLibrary = localEpisodes.Count;
                _mediaPortalServices.GetLogger().Info("Trakt: found {0} total episodes in library", localEpisodes.Count);
            }

            List <MediaItem> localWatchedEpisodes = localEpisodes.Where(MediaItemAspectsUtl.IsWatched).ToList();

            if (localWatchedEpisodes.Any())
            {
                syncEpisodesResult.WatchedInLibrary = localWatchedEpisodes.Count;
                _mediaPortalServices.GetLogger().Info("Trakt: found {0} episodes watched in library", localWatchedEpisodes.Count);
            }

            #endregion

            #region Mark episodes as unwatched in local database

            _mediaPortalServices.GetLogger().Info("Trakt: start marking series episodes as unwatched in media library");
            if (traktUnWatchedEpisodes.Any())
            {
                // create a unique key to lookup and search for faster
                ILookup <string, MediaItem> localLookupEpisodes = localWatchedEpisodes.ToLookup(twe => CreateLookupKey(twe), twe => twe);

                foreach (var episode in traktUnWatchedEpisodes)
                {
                    string tvdbKey = CreateLookupKey(episode);

                    var watchedEpisode = localLookupEpisodes[tvdbKey].FirstOrDefault();
                    if (watchedEpisode != null)
                    {
                        _mediaPortalServices.GetLogger().Info(
                            "Marking episode as unwatched in library, episode is not watched on trakt. Title = '{0}', Year = '{1}', Season = '{2}', Episode = '{3}', Show TVDb ID = '{4}', Show IMDb ID = '{5}'",
                            episode.ShowTitle, episode.ShowYear.HasValue ? episode.ShowYear.ToString() : "<empty>", episode.Season,
                            episode.Number, episode.ShowTvdbId.HasValue ? episode.ShowTvdbId.ToString() : "<empty>",
                            episode.ShowImdbId ?? "<empty>");

                        if (_mediaPortalServices.MarkAsUnWatched(watchedEpisode).Result)
                        {
                            syncEpisodesResult.MarkedAsUnWatchedInLibrary++;
                        }

                        // update watched episodes
                        localWatchedEpisodes.Remove(watchedEpisode);
                    }
                }
            }

            #endregion

            #region Mark episodes as watched in local database

            _mediaPortalServices.GetLogger().Info("Trakt: start marking series episodes as watched in media library");
            if (traktWatchedEpisodes.Any())
            {
                // create a unique key to lookup and search for faster
                ILookup <string, EpisodeWatched> onlineEpisodes = traktWatchedEpisodes.ToLookup(twe => CreateLookupKey(twe), twe => twe);
                List <MediaItem> localUnWatchedEpisodes         = localEpisodes.Except(localWatchedEpisodes).ToList();
                foreach (var episode in localUnWatchedEpisodes)
                {
                    string tvdbKey = CreateLookupKey(episode);

                    var traktEpisode = onlineEpisodes[tvdbKey].FirstOrDefault();
                    if (traktEpisode != null)
                    {
                        _mediaPortalServices.GetLogger().Info(
                            "Marking episode as watched in library, episode is watched on trakt. Plays = '{0}', Title = '{1}', Year = '{2}', Season = '{3}', Episode = '{4}', Show TVDb ID = '{5}', Show IMDb ID = '{6}', Last Watched = '{7}'",
                            traktEpisode.Plays, traktEpisode.ShowTitle,
                            traktEpisode.ShowYear.HasValue ? traktEpisode.ShowYear.ToString() : "<empty>", traktEpisode.Season,
                            traktEpisode.Number, traktEpisode.ShowTvdbId.HasValue ? traktEpisode.ShowTvdbId.ToString() : "<empty>",
                            traktEpisode.ShowImdbId ?? "<empty>", traktEpisode.WatchedAt);

                        if (_mediaPortalServices.MarkAsWatched(episode).Result)
                        {
                            syncEpisodesResult.MarkedAsWatchedInLibrary++;
                        }
                    }
                }
            }

            #endregion

            #region Add episodes to watched history at trakt.tv

            ITraktSyncHistoryPost syncHistoryPost = GetWatchedShowsForSync(localWatchedEpisodes, traktWatchedEpisodes);
            if (syncHistoryPost.Shows != null && syncHistoryPost.Shows.Any())
            {
                _mediaPortalServices.GetLogger().Info("Trakt: trying to add {0} watched episodes to trakt watched history", syncHistoryPost.Shows.Count());
                ITraktSyncHistoryPostResponse response = _traktClient.AddWatchedHistoryItems(syncHistoryPost);
                syncEpisodesResult.AddedToTraktWatchedHistory = response.Added?.Episodes;
                _traktCache.ClearLastActivity(FileName.WatchedEpisodes.Value);

                if (response.Added?.Episodes != null)
                {
                    _mediaPortalServices.GetLogger().Info("Trakt: successfully added {0} watched episodes to trakt watched history", response.Added.Episodes.Value);
                }
            }

            #endregion

            #region Add episodes to collection at trakt.tv

            ITraktSyncCollectionPost syncCollectionPost = GetCollectedShowsForSync(localEpisodes, traktCollectedEpisodes);
            if (syncCollectionPost.Shows != null && syncCollectionPost.Shows.Any())
            {
                _mediaPortalServices.GetLogger().Info("Trakt: trying to add {0} collected episodes to trakt collection", syncCollectionPost.Shows.Count());
                ITraktSyncCollectionPostResponse response = _traktClient.AddCollectionItems(syncCollectionPost);
                syncEpisodesResult.AddedToTraktCollection = response.Added?.Episodes;
                _traktCache.ClearLastActivity(FileName.CollectedEpisodes.Value);

                if (response.Added?.Episodes != null)
                {
                    _mediaPortalServices.GetLogger().Info("Trakt: successfully added {0} collected episodes to trakt collection", response.Added.Episodes.Value);
                }
            }

            #endregion

            return(syncEpisodesResult);
        }
        /// <summary>
        /// Removes items from the user's collection. Accepts shows, seasons, episodes and movies.
        /// <para>OAuth authorization required.</para>
        /// <para>
        /// See <a href="http://docs.trakt.apiary.io/#reference/sync/remove-from-collection/remove-items-from-collection">"Trakt API Doc - Sync: Remove from Collection"</a> for more information.
        /// </para>
        /// <para>
        /// It is recommended to use the <see cref="ITraktSyncCollectionPostBuilder" /> to create an instance
        /// of the required <see cref="ITraktSyncCollectionPost" />.
        /// See also <seealso cref="TraktPost.NewSyncCollectionPost()" />.
        /// </para>
        /// </summary>
        /// <param name="collectionRemovePost">An <see cref="ITraktSyncCollectionPost" /> instance containing all shows, seasons, episodes and movies, which should be removed.</param>
        /// <param name="cancellationToken">
        /// Propagates notification that the request should be canceled.<para/>
        /// If provided, the exception <see cref="OperationCanceledException" /> should be catched.
        /// </param>
        /// <returns>An <see cref="ITraktSyncCollectionRemovePostResponse" /> instance, which contains information about which items were deleted and not found.</returns>
        /// <exception cref="TraktException">Thrown, if the request fails.</exception>
        /// <exception cref="ArgumentNullException">Thrown if the given collection remove post is null.</exception>
        /// <exception cref="ArgumentException">Thrown, if the given collection remove post is empty.</exception>
        public Task <TraktResponse <ITraktSyncCollectionRemovePostResponse> > RemoveCollectionItemsAsync(ITraktSyncCollectionPost collectionRemovePost,
                                                                                                         CancellationToken cancellationToken = default)
        {
            ValidateCollectionPost(collectionRemovePost);
            var requestHandler = new RequestHandler(Client);

            return(requestHandler.ExecuteSingleItemRequestAsync(new SyncCollectionRemoveRequest
            {
                RequestBody = collectionRemovePost
            },
                                                                cancellationToken));
        }
        private void AddShows(ITraktSyncCollectionPost syncCollectionPost)
        {
            if (syncCollectionPost.Shows == null)
            {
                syncCollectionPost.Shows = new List <ITraktSyncCollectionPostShow>();
            }

            foreach (ITraktShow show in _shows)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShow(show));
            }

            foreach (PostBuilderObjectWithMetadata <ITraktShow> showEntry in _showsWithMetadata)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShow(showEntry.Object, showEntry.Metadata, showEntry.CollectedAt));
            }

            foreach (PostBuilderObjectWithMetadataAndSeasons <ITraktShow, PostSeasons> showEntry in _showsWithMetadataAndSeasonsCollection)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShowWithSeasonsCollection(showEntry.Object, showEntry.Metadata, showEntry.CollectedAt, showEntry.Seasons));
            }

            foreach (PostBuilderCollectedObject <ITraktShow> showEntry in _collectedShows.CollectedShows)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShow(showEntry.Object, null, showEntry.CollectedAt));
            }

            foreach (PostBuilderCollectedObjectWithSeasons <ITraktShow, IEnumerable <int> > showEntry in _collectedShowsWithSeasons.CollectedShowsWithSeasons)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShowWithSeasons(showEntry.Object, null, showEntry.CollectedAt, showEntry.Seasons));
            }

            foreach (PostBuilderCollectedObjectWithSeasons <ITraktShow, PostSeasons> showEntry in _collectedShowsWithSeasonsCollection.CollectedShowsWithSeasonsCollection)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShowWithSeasonsCollection(showEntry.Object, null, showEntry.CollectedAt, showEntry.Seasons));
            }

            foreach (PostBuilderObjectWithMetadata <ITraktShow> showEntry in _showsAndMetadata.ShowsAndMetadata)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShow(showEntry.Object, showEntry.Metadata, showEntry.CollectedAt));
            }

            foreach (PostBuilderObjectWithMetadataAndSeasons <ITraktShow, IEnumerable <int> > showEntry in _showsAndMetadataWithSeasons.ShowsAndMetadataWithSeasons)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShowWithSeasons(showEntry.Object, showEntry.Metadata, showEntry.CollectedAt, showEntry.Seasons));
            }

            foreach (PostBuilderObjectWithMetadataAndSeasons <ITraktShow, PostSeasons> showEntry in _showsAndMetadataWithSeasonsCollection.ShowsAndMetadataWithSeasonsCollection)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShowWithSeasonsCollection(showEntry.Object, showEntry.Metadata, showEntry.CollectedAt, showEntry.Seasons));
            }

            foreach (PostBuilderObjectWithSeasons <ITraktShow, IEnumerable <int> > showEntry in _showsWithSeasons.ShowsWithSeasons)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShowWithSeasons(showEntry.Object, null, null, showEntry.Seasons));
            }

            foreach (PostBuilderObjectWithSeasons <ITraktShow, PostSeasons> showEntry in _showsWithSeasonsCollection.ShowsWithSeasonsCollection)
            {
                (syncCollectionPost.Shows as List <ITraktSyncCollectionPostShow>).Add(CreateSyncCollectionPostShowWithSeasonsCollection(showEntry.Object, null, null, showEntry.Seasons));
            }
        }