using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.Helpers;
using Jellyfin.Api.ModelBinders;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using Jellyfin.Extensions;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
    /// 
    /// Years controller.
    /// 
    [Authorize(Policy = Policies.DefaultAuthorization)]
    public class YearsController : BaseJellyfinApiController
    {
        private readonly ILibraryManager _libraryManager;
        private readonly IUserManager _userManager;
        private readonly IDtoService _dtoService;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        public YearsController(
            ILibraryManager libraryManager,
            IUserManager userManager,
            IDtoService dtoService)
        {
            _libraryManager = libraryManager;
            _userManager = userManager;
            _dtoService = dtoService;
        }
        /// 
        /// Get years.
        /// 
        /// Skips over a given number of items within the results. Use for paging.
        /// Optional. The maximum number of records to return.
        /// Sort Order - Ascending,Descending.
        /// Specify this to localize the search to a specific item or folder. Omit to use the root.
        /// Optional. Specify additional fields of information to return in the output.
        /// Optional. If specified, results will be excluded based on item type. This allows multiple, comma delimited.
        /// Optional. If specified, results will be included based on item type. This allows multiple, comma delimited.
        /// Optional. Filter by MediaType. Allows multiple, comma delimited.
        /// Optional. Specify one or more sort orders, comma delimited. Options: Album, AlbumArtist, Artist, Budget, CommunityRating, CriticRating, DateCreated, DatePlayed, PlayCount, PremiereDate, ProductionYear, SortName, Random, Revenue, Runtime.
        /// Optional. Include user data.
        /// Optional. The max number of images to return, per image type.
        /// Optional. The image types to include in the output.
        /// User Id.
        /// Search recursively.
        /// Optional. Include image information in output.
        /// Year query returned.
        ///  A  containing the year result.
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult> GetYears(
            [FromQuery] int? startIndex,
            [FromQuery] int? limit,
            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] SortOrder[] sortOrder,
            [FromQuery] Guid? parentId,
            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,
            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] mediaTypes,
            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] sortBy,
            [FromQuery] bool? enableUserData,
            [FromQuery] int? imageTypeLimit,
            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
            [FromQuery] Guid? userId,
            [FromQuery] bool recursive = true,
            [FromQuery] bool? enableImages = true)
        {
            var dtoOptions = new DtoOptions { Fields = fields }
                .AddClientFields(Request)
                .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
            User? user = null;
            BaseItem parentItem = _libraryManager.GetParentItem(parentId, userId);
            if (userId.HasValue && !userId.Equals(Guid.Empty))
            {
                user = _userManager.GetUserById(userId.Value);
            }
            IList items;
            var query = new InternalItemsQuery(user)
            {
                ExcludeItemTypes = excludeItemTypes,
                IncludeItemTypes = includeItemTypes,
                MediaTypes = mediaTypes,
                DtoOptions = dtoOptions
            };
            bool Filter(BaseItem i) => FilterItem(i, excludeItemTypes, includeItemTypes, mediaTypes);
            if (parentItem.IsFolder)
            {
                var folder = (Folder)parentItem;
                if (!userId.Equals(Guid.Empty))
                {
                    items = recursive ? folder.GetRecursiveChildren(user, query).ToList() : folder.GetChildren(user, true).Where(Filter).ToList();
                }
                else
                {
                    items = recursive ? folder.GetRecursiveChildren(Filter) : folder.Children.Where(Filter).ToList();
                }
            }
            else
            {
                items = new[] { parentItem }.Where(Filter).ToList();
            }
            var extractedItems = GetAllItems(items);
            var filteredItems = _libraryManager.Sort(extractedItems, user, RequestHelpers.GetOrderBy(sortBy, sortOrder));
            var ibnItemsArray = filteredItems.ToList();
            IEnumerable ibnItems = ibnItemsArray;
            if (startIndex.HasValue || limit.HasValue)
            {
                if (startIndex.HasValue)
                {
                    ibnItems = ibnItems.Skip(startIndex.Value);
                }
                if (limit.HasValue)
                {
                    ibnItems = ibnItems.Take(limit.Value);
                }
            }
            var tuples = ibnItems.Select(i => new Tuple>(i, new List()));
            var dtos = tuples.Select(i => _dtoService.GetItemByNameDto(i.Item1, dtoOptions, i.Item2, user));
            var result = new QueryResult(
                startIndex,
                ibnItemsArray.Count,
                dtos.Where(i => i != null).ToArray());
            return result;
        }
        /// 
        /// Gets a year.
        /// 
        /// The year.
        /// Optional. Filter by user id, and attach user data.
        /// Year returned.
        /// Year not found.
        /// 
        /// An  containing the year,
        /// or a  if year not found.
        /// 
        [HttpGet("{year}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public ActionResult GetYear([FromRoute, Required] int year, [FromQuery] Guid? userId)
        {
            var item = _libraryManager.GetYear(year);
            if (item == null)
            {
                return NotFound();
            }
            var dtoOptions = new DtoOptions()
                .AddClientFields(Request);
            if (userId.HasValue && !userId.Equals(Guid.Empty))
            {
                var user = _userManager.GetUserById(userId.Value);
                return _dtoService.GetBaseItemDto(item, dtoOptions, user);
            }
            return _dtoService.GetBaseItemDto(item, dtoOptions);
        }
        private bool FilterItem(BaseItem f, IReadOnlyCollection excludeItemTypes, IReadOnlyCollection includeItemTypes, IReadOnlyCollection mediaTypes)
        {
            var baseItemKind = f.GetBaseItemKind();
            // Exclude item types
            if (excludeItemTypes.Count > 0 && excludeItemTypes.Contains(baseItemKind))
            {
                return false;
            }
            // Include item types
            if (includeItemTypes.Count > 0 && !includeItemTypes.Contains(baseItemKind))
            {
                return false;
            }
            // Include MediaTypes
            if (mediaTypes.Count > 0 && !mediaTypes.Contains(f.MediaType ?? string.Empty, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            return true;
        }
        private IEnumerable GetAllItems(IEnumerable items)
        {
            return items
                .Select(i => i.ProductionYear ?? 0)
                .Where(i => i > 0)
                .Distinct()
                .Select(year => _libraryManager.GetYear(year));
        }
    }
}