TraktShowsTrending GetTrendingShows(int page)
        {
            TraktShowsTrending trendingShows = null;

            if (TrendingShowPages == null || LastRequest < DateTime.UtcNow.Subtract(new TimeSpan(0, TraktSettings.WebRequestCacheMinutes, 0)))
            {
                // get the first page
                trendingShows = TraktAPI.TraktAPI.GetTrendingShows(1, TraktSettings.MaxTrendingShowsRequest);

                // reset to defaults
                LastRequest           = DateTime.UtcNow;
                CurrentPage           = 1;
                PreviousSelectedIndex = 0;

                // clear the cache
                if (TrendingShowPages == null)
                {
                    TrendingShowPages = new Dictionary <int, TraktShowsTrending>();
                }
                else
                {
                    TrendingShowPages.Clear();
                }

                // add page to cache
                TrendingShowPages.Add(1, trendingShows);
            }
            else
            {
                // get page from cache if it exists
                if (TrendingShowPages.TryGetValue(page, out trendingShows))
                {
                    return(trendingShows);
                }

                // request next page
                trendingShows = TraktAPI.TraktAPI.GetTrendingShows(page, TraktSettings.MaxTrendingShowsRequest);
                if (trendingShows != null && trendingShows.Shows != null)
                {
                    // add to cache
                    TrendingShowPages.Add(page, trendingShows);
                }
            }
            return(trendingShows);
        }
        private void SendTrendingShowsToFacade(TraktShowsTrending trendingItems)
        {
            // clear facade
            GUIControl.ClearControl(GetID, Facade.GetID);

            if (trendingItems == null)
            {
                GUIUtils.ShowNotifyDialog(Translation.Error, Translation.ErrorGeneral);
                GUIWindowManager.ShowPreviousWindow();
                TrendingShowPages = null;
                return;
            }

            if (trendingItems.Shows.Count() == 0)
            {
                GUIUtils.ShowNotifyDialog(GUIUtils.PluginName(), Translation.NoTrendingShows);
                GUIWindowManager.ShowPreviousWindow();
                TrendingShowPages = null;
                return;
            }

            // filter shows
            var filteredTrendingList = GUICommon.FilterTrendingShows(trendingItems.Shows).Where(s => !string.IsNullOrEmpty(s.Show.Title)).ToList();

            // sort shows
            filteredTrendingList.Sort(new GUIListItemShowSorter(TraktSettings.SortByTrendingShows.Field, TraktSettings.SortByTrendingShows.Direction));

            int itemId     = 0;
            var showImages = new List <GUITmdbImage>();

            // Add Previous Page Button
            if (trendingItems.CurrentPage != 1)
            {
                var prevPageItem = new GUIShowListItem(Translation.PreviousPage, (int)TraktGUIWindows.TrendingShows);
                prevPageItem.IsPrevPageItem  = true;
                prevPageItem.IconImage       = "traktPreviousPage.png";
                prevPageItem.IconImageBig    = "traktPreviousPage.png";
                prevPageItem.ThumbnailImage  = "traktPreviousPage.png";
                prevPageItem.OnItemSelected += OnPreviousPageSelected;
                prevPageItem.IsFolder        = true;
                Facade.Add(prevPageItem);
                itemId++;
            }

            foreach (var trendingItem in filteredTrendingList)
            {
                var item = new GUIShowListItem(trendingItem.Show.Title, (int)TraktGUIWindows.TrendingShows);

                // add image for download
                var image = new GUITmdbImage {
                    ShowImages = new TmdbShowImages {
                        Id = trendingItem.Show.Ids.Tmdb
                    }
                };
                showImages.Add(image);

                item.Label2          = trendingItem.Show.Year.ToString();
                item.TVTag           = trendingItem;
                item.Show            = trendingItem.Show;
                item.Images          = image;
                item.ItemId          = Int32.MaxValue - itemId;
                item.IconImage       = GUIImageHandler.GetDefaultPoster(false);
                item.IconImageBig    = GUIImageHandler.GetDefaultPoster();
                item.ThumbnailImage  = GUIImageHandler.GetDefaultPoster();
                item.OnItemSelected += OnShowSelected;
                Utils.SetDefaultIcons(item);
                Facade.Add(item);
                itemId++;
            }

            // Add Next Page Button
            if (trendingItems.CurrentPage != trendingItems.TotalPages)
            {
                var nextPageItem = new GUIShowListItem(Translation.NextPage, (int)TraktGUIWindows.TrendingShows);
                nextPageItem.IsNextPageItem  = true;
                nextPageItem.IconImage       = "traktNextPage.png";
                nextPageItem.IconImageBig    = "traktNextPage.png";
                nextPageItem.ThumbnailImage  = "traktNextPage.png";
                nextPageItem.OnItemSelected += OnNextPageSelected;
                nextPageItem.IsFolder        = true;
                Facade.Add(nextPageItem);
                itemId++;
            }

            // Set Facade Layout
            Facade.CurrentLayout = CurrentLayout;
            GUIControl.FocusControl(GetID, Facade.GetID);

            Facade.SelectIndex(PreviousSelectedIndex);

            // set facade properties
            GUIUtils.SetProperty("#itemcount", filteredTrendingList.Count().ToString());
            GUIUtils.SetProperty("#Trakt.Items", string.Format("{0} {1}", filteredTrendingList.Count(), filteredTrendingList.Count() > 1 ? Translation.SeriesPlural : Translation.Series));

            GUIUtils.SetProperty("#Trakt.Trending.PeopleCount", trendingItems.TotalWatchers.ToString());
            GUIUtils.SetProperty("#Trakt.Trending.Description", string.Format(Translation.TrendingTVShowsPeople, trendingItems.TotalWatchers.ToString(), trendingItems.TotalItems.ToString()));

            // Page Properties
            GUIUtils.SetProperty("#Trakt.Facade.CurrentPage", trendingItems.CurrentPage.ToString());
            GUIUtils.SetProperty("#Trakt.Facade.TotalPages", trendingItems.TotalPages.ToString());
            GUIUtils.SetProperty("#Trakt.Facade.TotalItemsPerPage", TraktSettings.MaxTrendingShowsRequest.ToString());

            // Download show images Async and set to facade
            GUIShowListItem.GetImages(showImages);
        }