private QueryResult <BaseItem> GetQueryResult(BaseReportRequest request, User user)
        {
            // all report queries currently need this because it's not being specified
            request.Recursive = true;

            BaseItem item = null;

            if (!string.IsNullOrEmpty(request.ParentId))
            {
                item = _libraryManager.GetItemById(request.ParentId);
            }

            if (string.Equals(request.IncludeItemTypes, "Playlist", StringComparison.OrdinalIgnoreCase))
            {
                //item = user == null ? _libraryManager.RootFolder : user.RootFolder;
            }
            else if (string.Equals(request.IncludeItemTypes, "BoxSet", StringComparison.OrdinalIgnoreCase))
            {
                item = _libraryManager.GetUserRootFolder();
            }

            // Default list type = children

            Folder folder = item as Folder;

            if (folder is null)
            {
                folder = _libraryManager.GetUserRootFolder();
            }

            if (!string.IsNullOrEmpty(request.Ids))
            {
                request.Recursive = true;
                var query  = GetItemsQuery(request, user);
                var result = folder.GetItems(query);

                if (string.IsNullOrWhiteSpace(request.SortBy))
                {
                    var ids = query.ItemIds.ToList();

                    // Try to preserve order
                    result.Items = result.Items.OrderBy(i => ids.IndexOf(i.Id)).ToArray();
                }

                return(result);
            }

            if (request.Recursive)
            {
                return(folder.GetItems(GetItemsQuery(request, user)));
            }

            if (user == null)
            {
                return(folder.GetItems(GetItemsQuery(request, null)));
            }

            var userRoot = item as UserRootFolder;

            if (userRoot == null)
            {
                return(folder.GetItems(GetItemsQuery(request, user)));
            }

            IEnumerable <BaseItem> items = folder.GetChildren(user, true);

            var itemsArray = items.ToArray();

            return(new QueryResult <BaseItem>
            {
                Items = itemsArray,
                TotalRecordCount = itemsArray.Length
            });
        }
        private InternalItemsQuery GetItemsQuery(BaseReportRequest request, User user)
        {
            var query = new InternalItemsQuery(user)
            {
                IsPlayed                = request.IsPlayed,
                MediaTypes              = request.GetMediaTypes(),
                IncludeItemTypes        = request.GetIncludeItemTypes(),
                ExcludeItemTypes        = request.GetExcludeItemTypes(),
                Recursive               = request.Recursive,
                OrderBy                 = request.GetOrderBy(),
                IsFavorite              = request.IsFavorite,
                StartIndex              = request.StartIndex,
                IsMissing               = request.IsMissing,
                IsUnaired               = request.IsUnaired,
                CollapseBoxSetItems     = request.CollapseBoxSetItems,
                NameLessThan            = request.NameLessThan,
                NameStartsWith          = request.NameStartsWith,
                NameStartsWithOrGreater = request.NameStartsWithOrGreater,
                HasImdbId               = request.HasImdbId,
                IsPlaceHolder           = request.IsPlaceHolder,
                IsLocked                = request.IsLocked,
                IsHD                   = request.IsHD,
                Is3D                   = request.Is3D,
                HasTvdbId              = request.HasTvdbId,
                HasTmdbId              = request.HasTmdbId,
                HasOverview            = request.HasOverview,
                HasOfficialRating      = request.HasOfficialRating,
                HasParentalRating      = request.HasParentalRating,
                HasSpecialFeature      = request.HasSpecialFeature,
                HasSubtitles           = request.HasSubtitles,
                HasThemeSong           = request.HasThemeSong,
                HasThemeVideo          = request.HasThemeVideo,
                HasTrailer             = request.HasTrailer,
                Tags                   = request.GetTags(),
                OfficialRatings        = request.GetOfficialRatings(),
                Genres                 = request.GetGenres(),
                GenreIds               = request.GetGenreIds(),
                StudioIds              = request.GetStudioIds(),
                Person                 = request.Person,
                PersonIds              = request.GetPersonIds(),
                PersonTypes            = request.GetPersonTypes(),
                Years                  = request.GetYears(),
                ImageTypes             = request.GetImageTypes().ToArray(),
                VideoTypes             = request.GetVideoTypes().ToArray(),
                AdjacentTo             = request.AdjacentTo,
                ItemIds                = request.GetItemIds(),
                MinCommunityRating     = request.MinCommunityRating,
                MinCriticRating        = request.MinCriticRating,
                ParentId               = string.IsNullOrWhiteSpace(request.ParentId) ? Guid.Empty : new Guid(request.ParentId),
                ParentIndexNumber      = request.ParentIndexNumber,
                EnableTotalRecordCount = request.EnableTotalRecordCount
            };

            if (request.Limit == -1)
            {
                query.Limit = null;
            }

            if (!string.IsNullOrWhiteSpace(request.Ids))
            {
                query.CollapseBoxSetItems = false;
            }

            query.IsFavorite = null;
            if (request.IsFavorite == true)
            {
                query.IsFavorite = true;
            }
            else if (request.IsNotFavorite == true)
            {
                query.IsFavorite = false;
            }

            foreach (var filter in request.GetFilters())
            {
                switch (filter)
                {
                case ItemFilter.Dislikes:
                    query.IsLiked = false;
                    break;

                case ItemFilter.IsFavoriteOrLikes:
                    query.IsFavoriteOrLiked = true;
                    break;

                case ItemFilter.IsFolder:
                    query.IsFolder = true;
                    break;

                case ItemFilter.IsNotFolder:
                    query.IsFolder = false;
                    break;

                case ItemFilter.IsPlayed:
                    query.IsPlayed = true;
                    break;

                case ItemFilter.IsResumable:
                    query.IsResumable = true;
                    break;

                case ItemFilter.IsUnplayed:
                    query.IsPlayed = false;
                    break;

                case ItemFilter.Likes:
                    query.IsLiked = true;
                    break;
                }
            }

            if (!string.IsNullOrEmpty(request.MinPremiereDate))
            {
                query.MinPremiereDate = DateTime.Parse(request.MinPremiereDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
            }

            if (!string.IsNullOrEmpty(request.MaxPremiereDate))
            {
                query.MaxPremiereDate = DateTime.Parse(request.MaxPremiereDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
            }

            // Filter by Series Status
            if (!string.IsNullOrEmpty(request.SeriesStatus))
            {
                query.SeriesStatuses = request.SeriesStatus.Split(',').Select(d => (SeriesStatus)Enum.Parse(typeof(SeriesStatus), d, true)).ToArray();
            }

            // ExcludeLocationTypes
            if (!string.IsNullOrEmpty(request.ExcludeLocationTypes))
            {
                var excludeLocationTypes = request.ExcludeLocationTypes.Split(',').Select(d => (LocationType)Enum.Parse(typeof(LocationType), d, true)).ToArray();
                if (excludeLocationTypes.Contains(LocationType.Virtual))
                {
                    query.IsVirtualItem = false;
                }
            }

            // Min official rating
            if (!string.IsNullOrWhiteSpace(request.MinOfficialRating))
            {
                query.MinParentalRating = _localization.GetRatingLevel(request.MinOfficialRating);
            }

            // Max official rating
            if (!string.IsNullOrWhiteSpace(request.MaxOfficialRating))
            {
                query.MaxParentalRating = _localization.GetRatingLevel(request.MaxOfficialRating);
            }

            query.CollapseBoxSetItems = false;

            if (request.Limit > -1 && request.Limit < int.MaxValue)
            {
                query.Limit = request.Limit;
            }

            return(query);
        }