using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Users;
namespace MediaBrowser.Controller.Entities.Movies
{
///
/// Class BoxSet
///
public class BoxSet : Folder, IHasTrailers, IHasDisplayOrder, IHasLookupInfo
{
public BoxSet()
{
RemoteTrailers = EmptyMediaUrlArray;
LocalTrailerIds = Array.Empty();
RemoteTrailerIds = Array.Empty();
DisplayOrder = ItemSortBy.PremiereDate;
}
[IgnoreDataMember]
protected override bool FilterLinkedChildrenPerUser => true;
[IgnoreDataMember]
public override bool SupportsInheritedParentImages => false;
[IgnoreDataMember]
public override bool SupportsPeople => true;
public Guid[] LocalTrailerIds { get; set; }
public Guid[] RemoteTrailerIds { get; set; }
///
/// Gets or sets the display order.
///
/// The display order.
public string DisplayOrder { get; set; }
protected override bool GetBlockUnratedValue(UserPolicy config)
{
return config.BlockUnratedItems.Contains(UnratedItem.Movie);
}
public override double GetDefaultPrimaryImageAspectRatio()
=> 2.0 / 3;
public override UnratedItem GetBlockUnratedType()
{
return UnratedItem.Movie;
}
protected override IEnumerable GetNonCachedChildren(IDirectoryService directoryService)
{
if (IsLegacyBoxSet)
{
return base.GetNonCachedChildren(directoryService);
}
return new List();
}
protected override List LoadChildren()
{
if (IsLegacyBoxSet)
{
return base.LoadChildren();
}
// Save a trip to the database
return new List();
}
[IgnoreDataMember]
private bool IsLegacyBoxSet
{
get
{
if (string.IsNullOrEmpty(Path))
{
return false;
}
if (LinkedChildren.Length > 0)
{
return false;
}
return !FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, Path);
}
}
[IgnoreDataMember]
public override bool IsPreSorted => true;
public override bool IsAuthorizedToDelete(User user, List allCollectionFolders)
{
return true;
}
public override bool IsSaveLocalMetadataEnabled()
{
return true;
}
public override List GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery query)
{
var children = base.GetChildren(user, includeLinkedChildren, query);
if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
{
// Sort by name
return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
}
if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
{
// Sort by release date
return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending).ToList();
}
// Default sorting
return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending).ToList();
}
public override IEnumerable GetRecursiveChildren(User user, InternalItemsQuery query)
{
var children = base.GetRecursiveChildren(user, query);
if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
{
// Sort by release date
return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending).ToList();
}
return children;
}
public BoxSetInfo GetLookupInfo()
{
return GetItemLookupInfo();
}
public override bool IsVisible(User user)
{
if (IsLegacyBoxSet)
{
return base.IsVisible(user);
}
if (base.IsVisible(user))
{
if (LinkedChildren.Length == 0)
{
return true;
}
var userLibraryFolderIds = GetLibraryFolderIds(user);
var libraryFolderIds = LibraryFolderIds ?? GetLibraryFolderIds();
if (libraryFolderIds.Length == 0)
{
return true;
}
return userLibraryFolderIds.Any(i => libraryFolderIds.Contains(i));
}
return false;
}
public override bool IsVisibleStandalone(User user)
{
if (IsLegacyBoxSet)
{
return base.IsVisibleStandalone(user);
}
return IsVisible(user);
}
public Guid[] LibraryFolderIds { get; set; }
private Guid[] GetLibraryFolderIds(User user)
{
return LibraryManager.GetUserRootFolder().GetChildren(user, true)
.Select(i => i.Id)
.ToArray();
}
public Guid[] GetLibraryFolderIds()
{
var expandedFolders = new List() { };
return FlattenItems(this, expandedFolders)
.SelectMany(i => LibraryManager.GetCollectionFolders(i))
.Select(i => i.Id)
.Distinct()
.ToArray();
}
private IEnumerable FlattenItems(IEnumerable items, List expandedFolders)
{
return items
.SelectMany(i => FlattenItems(i, expandedFolders));
}
private IEnumerable FlattenItems(BaseItem item, List expandedFolders)
{
var boxset = item as BoxSet;
if (boxset != null)
{
if (!expandedFolders.Contains(item.Id))
{
expandedFolders.Add(item.Id);
return FlattenItems(boxset.GetLinkedChildren(), expandedFolders);
}
return Array.Empty();
}
return new[] { item };
}
}
}