<commit_before>﻿using MediaBrowser.Common;
using MediaBrowser.Model.ApiClient;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
using MediaBrowser.Plugins.DefaultTheme.Details;
using MediaBrowser.Plugins.DefaultTheme.Home;
using MediaBrowser.Plugins.DefaultTheme.ListPage;
using MediaBrowser.Plugins.DefaultTheme.Search;
using MediaBrowser.Theater.Interfaces;
using MediaBrowser.Theater.Interfaces.Configuration;
using MediaBrowser.Theater.Interfaces.Navigation;
using MediaBrowser.Theater.Interfaces.Playback;
using MediaBrowser.Theater.Interfaces.Presentation;
using MediaBrowser.Theater.Interfaces.Session;
using MediaBrowser.Theater.Interfaces.Theming;
using MediaBrowser.Theater.Interfaces.UserInput;
using MediaBrowser.Theater.Interfaces.ViewModels;
using MediaBrowser.Theater.Presentation.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace MediaBrowser.Plugins.DefaultTheme
{
    /// <summary>
    /// Class Theme
    /// </summary>
    public class DefaultTheme : ITheme
    {
        /// <summary>
        /// The _playback manager
        /// </summary>
        private readonly IPlaybackManager _playbackManager;
        /// <summary>
        /// The _api client
        /// </summary>
        private readonly IApiClient _apiClient;
        /// <summary>
        /// The _image manager
        /// </summary>
        private readonly IImageManager _imageManager;
        /// <summary>
        /// The _nav service
        /// </summary>
        private readonly INavigationService _navService;
        /// <summary>
        /// The _session manager
        /// </summary>
        private readonly ISessionManager _sessionManager;
        /// <summary>
        /// The _app window
        /// </summary>
        private readonly IPresentationManager _presentationManager;
        /// <summary>
        /// The _logger
        /// </summary>
        private readonly ILogger _logger;
        private readonly IServerEvents _serverEvents;
        private readonly ITheaterApplicationHost _appHost;
        private readonly ITheaterConfigurationManager _config;

        public static DefaultTheme Current;

        public DefaultTheme(IPlaybackManager playbackManager, IImageManager imageManager, IApiClient apiClient, INavigationService navService, ISessionManager sessionManager, IPresentationManager presentationManager, ILogManager logManager, IServerEvents serverEvents, ITheaterApplicationHost appHost, ITheaterConfigurationManager config)
        {
            Current = this;

            _playbackManager = playbackManager;
            _imageManager = imageManager;
            _apiClient = apiClient;
            _navService = navService;
            _sessionManager = sessionManager;
            _presentationManager = presentationManager;
            _serverEvents = serverEvents;
            _appHost = appHost;
            _config = config;
            _logger = logManager.GetLogger(GetType().Name);
        }

        /// <summary>
        /// Gets the global resources.
        /// </summary>
        /// <returns>IEnumerable{ResourceDictionary}.</returns>
        public IEnumerable<ResourceDictionary> GetResources()
        {
            var namespaceName = GetType().Namespace;

            return new[] { "Merged" }.Select(i => new ResourceDictionary
            {
                Source = new Uri("pack://application:,,,/" + namespaceName + ";component/Resources/" + i + ".xaml", UriKind.Absolute)

            });
        }

        public Page GetSearchPage(BaseItemDto item)
        {
            return new SearchPage(item, _apiClient, _sessionManager, _imageManager, _presentationManager, _navService, _playbackManager, _logger, _serverEvents);
        }

        /// <summary>
        /// Gets the item page.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="context">The context.</param>
        /// <returns>Page.</returns>
        public Page GetItemPage(BaseItemDto item, ViewType context)
        {
            var itemViewModel = new ItemViewModel(_apiClient, _imageManager, _playbackManager, _presentationManager, _logger, _serverEvents)
            {
                Item = item,
                ImageWidth = 550,
                PreferredImageTypes = new[] { ImageType.Primary, ImageType.Thumb }
            };

            return new DetailPage(itemViewModel, _presentationManager)
            {
                DataContext = new DetailPageViewModel(itemViewModel, _apiClient, _sessionManager, _imageManager, _presentationManager, _playbackManager, _navService, _logger, _serverEvents, context)
            };
        }

        private readonly string[] _folderTypesWithDetailPages = new[] { "series", "musicalbum", "musicartist" };

        /// <summary>
        /// Gets the folder page.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="context">The context.</param>
        /// <param name="displayPreferences">The display preferences.</param>
        /// <returns>Page.</returns>
        public Page GetFolderPage(BaseItemDto item, ViewType context, DisplayPreferences displayPreferences)
        {
            if (context == ViewType.Folders || !_folderTypesWithDetailPages.Contains(item.Type, StringComparer.OrdinalIgnoreCase))
            {
                var options = GetListPageConfig(item, context);

                return new FolderPage(item, displayPreferences, _apiClient, _imageManager, _presentationManager, _navService, _playbackManager, _logger, _serverEvents, options);
            }

            return GetItemPage(item, context);
        }

        private ListPageConfig GetListPageConfig(BaseItemDto item, ViewType context)
        {
            var config = new ListPageConfig();

            if (context == ViewType.Tv || item.IsType("season"))
            {
                TvViewModel.SetDefaults(config);

                if (item.IsType("season"))
                {
                    config.DefaultViewType = ListViewTypes.List;

                    config.PosterImageWidth = 480;
                    config.PosterStripImageWidth = 592;
                    config.ThumbImageWidth = 592;
                }
            }
            else if (context == ViewType.Movies)
            {
                MoviesViewModel.SetDefaults(config);
            }
            else if (context == ViewType.Games)
            {
                GamesViewModel.SetDefaults(config, item.GameSystem);
            }

            if (item.IsFolder)
            {
                config.CustomItemQuery = (vm, displayPreferences) =>
                {
                    if (item.IsType("series"))
                    {
                        return _apiClient.GetSeasonsAsync(new SeasonQuery
                        {
                            UserId = _sessionManager.CurrentUser.Id,
                            SeriesId = item.SeriesId,
                            Fields = FolderPage.QueryFields
                        });
                    }

                    if (item.IsType("season"))
                    {
                        return _apiClient.GetEpisodesAsync(new EpisodeQuery
                        {
                            UserId = _sessionManager.CurrentUser.Id,<commit_msg>fixed issue 153 - Can't Access Seasons in Folders - build 052011654<commit_after>﻿using MediaBrowser.Common;
using MediaBrowser.Model.ApiClient;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
using MediaBrowser.Plugins.DefaultTheme.Details;
using MediaBrowser.Plugins.DefaultTheme.Home;
using MediaBrowser.Plugins.DefaultTheme.ListPage;
using MediaBrowser.Plugins.DefaultTheme.Search;
using MediaBrowser.Theater.Interfaces;
using MediaBrowser.Theater.Interfaces.Configuration;
using MediaBrowser.Theater.Interfaces.Navigation;
using MediaBrowser.Theater.Interfaces.Playback;
using MediaBrowser.Theater.Interfaces.Presentation;
using MediaBrowser.Theater.Interfaces.Session;
using MediaBrowser.Theater.Interfaces.Theming;
using MediaBrowser.Theater.Interfaces.UserInput;
using MediaBrowser.Theater.Interfaces.ViewModels;
using MediaBrowser.Theater.Presentation.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace MediaBrowser.Plugins.DefaultTheme
{
    /// <summary>
    /// Class Theme
    /// </summary>
    public class DefaultTheme : ITheme
    {
        /// <summary>
        /// The _playback manager
        /// </summary>
        private readonly IPlaybackManager _playbackManager;
        /// <summary>
        /// The _api client
        /// </summary>
        private readonly IApiClient _apiClient;
        /// <summary>
        /// The _image manager
        /// </summary>
        private readonly IImageManager _imageManager;
        /// <summary>
        /// The _nav service
        /// </summary>
        private readonly INavigationService _navService;
        /// <summary>
        /// The _session manager
        /// </summary>
        private readonly ISessionManager _sessionManager;
        /// <summary>
        /// The _app window
        /// </summary>
        private readonly IPresentationManager _presentationManager;
        /// <summary>
        /// The _logger
        /// </summary>
        private readonly ILogger _logger;
        private readonly IServerEvents _serverEvents;
        private readonly ITheaterApplicationHost _appHost;
        private readonly ITheaterConfigurationManager _config;

        public static DefaultTheme Current;

        public DefaultTheme(IPlaybackManager playbackManager, IImageManager imageManager, IApiClient apiClient, INavigationService navService, ISessionManager sessionManager, IPresentationManager presentationManager, ILogManager logManager, IServerEvents serverEvents, ITheaterApplicationHost appHost, ITheaterConfigurationManager config)
        {
            Current = this;

            _playbackManager = playbackManager;
            _imageManager = imageManager;
            _apiClient = apiClient;
            _navService = navService;
            _sessionManager = sessionManager;
            _presentationManager = presentationManager;
            _serverEvents = serverEvents;
            _appHost = appHost;
            _config = config;
            _logger = logManager.GetLogger(GetType().Name);
        }

        /// <summary>
        /// Gets the global resources.
        /// </summary>
        /// <returns>IEnumerable{ResourceDictionary}.</returns>
        public IEnumerable<ResourceDictionary> GetResources()
        {
            var namespaceName = GetType().Namespace;

            return new[] { "Merged" }.Select(i => new ResourceDictionary
            {
                Source = new Uri("pack://application:,,,/" + namespaceName + ";component/Resources/" + i + ".xaml", UriKind.Absolute)

            });
        }

        public Page GetSearchPage(BaseItemDto item)
        {
            return new SearchPage(item, _apiClient, _sessionManager, _imageManager, _presentationManager, _navService, _playbackManager, _logger, _serverEvents);
        }

        /// <summary>
        /// Gets the item page.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="context">The context.</param>
        /// <returns>Page.</returns>
        public Page GetItemPage(BaseItemDto item, ViewType context)
        {
            var itemViewModel = new ItemViewModel(_apiClient, _imageManager, _playbackManager, _presentationManager, _logger, _serverEvents)
            {
                Item = item,
                ImageWidth = 550,
                PreferredImageTypes = new[] { ImageType.Primary, ImageType.Thumb }
            };

            return new DetailPage(itemViewModel, _presentationManager)
            {
                DataContext = new DetailPageViewModel(itemViewModel, _apiClient, _sessionManager, _imageManager, _presentationManager, _playbackManager, _navService, _logger, _serverEvents, context)
            };
        }

        private readonly string[] _folderTypesWithDetailPages = new[] { "series", "musicalbum", "musicartist" };

        /// <summary>
        /// Gets the folder page.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="context">The context.</param>
        /// <param name="displayPreferences">The display preferences.</param>
        /// <returns>Page.</returns>
        public Page GetFolderPage(BaseItemDto item, ViewType context, DisplayPreferences displayPreferences)
        {
            if (context == ViewType.Folders || !_folderTypesWithDetailPages.Contains(item.Type, StringComparer.OrdinalIgnoreCase))
            {
                var options = GetListPageConfig(item, context);

                return new FolderPage(item, displayPreferences, _apiClient, _imageManager, _presentationManager, _navService, _playbackManager, _logger, _serverEvents, options);
            }

            return GetItemPage(item, context);
        }

        private ListPageConfig GetListPageConfig(BaseItemDto item, ViewType context)
        {
            var config = new ListPageConfig();

            if (context == ViewType.Tv || item.IsType("season"))
            {
                TvViewModel.SetDefaults(config);

                if (item.IsType("season"))
                {
                    config.DefaultViewType = ListViewTypes.List;

                    config.PosterImageWidth = 480;
                    config.PosterStripImageWidth = 592;
                    config.ThumbImageWidth = 592;
                }
            }
            else if (context == ViewType.Movies)
            {
                MoviesViewModel.SetDefaults(config);
            }
            else if (context == ViewType.Games)
            {
                GamesViewModel.SetDefaults(config, item.GameSystem);
            }

            if (item.IsFolder)
            {
                config.CustomItemQuery = (vm, displayPreferences) =>
                {
                   
                    if (item.IsType("series"))
                    {
                        return _apiClient.GetSeasonsAsync(new SeasonQuery
                        {
                            UserId = _sessionManager.CurrentUser.Id,
                            SeriesId = item.Id,
                            Fields = FolderPage.QueryFields
                        });
                    }
                 

                    if (item.IsType("season"))
                    {
                        return _apiClient.GetEpisodesAsync(new EpisodeQuery
                        {
                            UserId = _sessionManager.CurrentUser.Id,
{'fraction_non_alphanumeric': 0.09180262102166355, 'fraction_numerical': 0.0024070607114201658, 'mean_word_length': 2.0130942788074133, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '364668', 'n_tokens_mistral': 3720, 'n_tokens_neox': 3566, 'n_words': 790}