using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace MediaBrowser.Controller.Entities.TV
{
///
/// Class Episode
///
public class Episode : Video, IHasLookupInfo, IHasSeries
{
///
/// Gets the season in which it aired.
///
/// The aired season.
public int? AirsBeforeSeasonNumber { get; set; }
public int? AirsAfterSeasonNumber { get; set; }
public int? AirsBeforeEpisodeNumber { get; set; }
///
/// Gets or sets the DVD season number.
///
/// The DVD season number.
public int? DvdSeasonNumber { get; set; }
///
/// Gets or sets the DVD episode number.
///
/// The DVD episode number.
public float? DvdEpisodeNumber { get; set; }
///
/// Gets or sets the absolute episode number.
///
/// The absolute episode number.
public int? AbsoluteEpisodeNumber { get; set; }
///
/// This is the ending episode number for double episodes.
///
/// The index number.
public int? IndexNumberEnd { get; set; }
///
/// We want to group into series not show individually in an index
///
/// true if [group in index]; otherwise, false.
[IgnoreDataMember]
public override bool GroupInIndex
{
get { return true; }
}
[IgnoreDataMember]
public int? AiredSeasonNumber
{
get
{
return AirsAfterSeasonNumber ?? AirsBeforeSeasonNumber ?? PhysicalSeasonNumber;
}
}
[IgnoreDataMember]
public int? PhysicalSeasonNumber
{
get
{
var value = ParentIndexNumber;
if (value.HasValue)
{
return value;
}
var season = Season;
return season != null ? season.IndexNumber : null;
}
}
///
/// We roll up into series
///
/// The index container.
[IgnoreDataMember]
public override Folder IndexContainer
{
get
{
return Season;
}
}
[IgnoreDataMember]
public override Folder LatestItemsIndexContainer
{
get
{
return Series;
}
}
[IgnoreDataMember]
public override BaseItem DisplayParent
{
get
{
return Season ?? Parent;
}
}
///
/// Gets the user data key.
///
/// System.String.
public override string GetUserDataKey()
{
var series = Series;
if (series != null && ParentIndexNumber.HasValue && IndexNumber.HasValue)
{
return series.GetUserDataKey() + ParentIndexNumber.Value.ToString("000") + IndexNumber.Value.ToString("000");
}
return base.GetUserDataKey();
}
///
/// Our rating comes from our series
///
[IgnoreDataMember]
public override string OfficialRatingForComparison
{
get
{
var series = Series;
return series != null ? series.OfficialRatingForComparison : base.OfficialRatingForComparison;
}
}
///
/// This Episode's Series Instance
///
/// The series.
[IgnoreDataMember]
public Series Series
{
get { return FindParent(); }
}
[IgnoreDataMember]
public Season Season
{
get
{
var season = FindParent();
// Episodes directly in series folder
if (season == null)
{
var series = Series;
if (ParentIndexNumber.HasValue)
{
var findNumber = ParentIndexNumber.Value;
season = series.Children
.OfType()
.FirstOrDefault(i => i.IndexNumber.HasValue && i.IndexNumber.Value == findNumber);
}
}
return season;
}
}
[IgnoreDataMember]
public string SeriesName
{
get
{
var series = Series;
return series == null ? null : series.Name;
}
}
///
/// Creates the name of the sort.
///
/// System.String.
protected override string CreateSortName()
{
return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("000-") : "")
+ (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
}
///
/// Determines whether [contains episode number] [the specified number].
///
/// The number.
/// true if [contains episode number] [the specified number]; otherwise, false.
public bool ContainsEpisodeNumber(int number)
{
if (IndexNumber.HasValue)
{
if (IndexNumberEnd.HasValue)
{
return number >= IndexNumber.Value && number <= IndexNumberEnd.Value;
}
return IndexNumber.Value == number;
}
return false;
}
[IgnoreDataMember]
public override bool SupportsRemoteImageDownloading
{
get
{
if (IsMissingEpisode)
{
return false;
}
return true;
}
}
[IgnoreDataMember]
public bool IsMissingEpisode
{
get
{
return LocationType == LocationType.Virtual && !IsUnaired;
}
}
[IgnoreDataMember]
public bool IsUnaired
{
get { return PremiereDate.HasValue && PremiereDate.Value.ToLocalTime().Date >= DateTime.Now.Date; }
}
[IgnoreDataMember]
public bool IsVirtualUnaired
{
get { return LocationType == LocationType.Virtual && IsUnaired; }
}
[IgnoreDataMember]
public Guid? SeasonId
{
get
{
// First see if the parent is a Season
var season = Season;
if (season != null)
{
return season.Id;
}
return null;
}
}
public override IEnumerable GetDeletePaths()
{
return new[] { Path };
}
protected override bool GetBlockUnratedValue(UserConfiguration config)
{
return config.BlockUnratedItems.Contains(UnratedItem.Series);
}
public EpisodeInfo GetLookupInfo()
{
var id = GetItemLookupInfo();
var series = Series;
if (series != null)
{
id.SeriesProviderIds = series.ProviderIds;
id.AnimeSeriesIndex = series.AnimeSeriesIndex;
}
id.IndexNumberEnd = IndexNumberEnd;
return id;
}
public override bool BeforeMetadataRefresh()
{
var hasChanges = base.BeforeMetadataRefresh();
var locationType = LocationType;
if (locationType == LocationType.FileSystem || locationType == LocationType.Offline)
{
if (!IndexNumber.HasValue && !string.IsNullOrEmpty(Path))
{
IndexNumber = TVUtils.GetEpisodeNumberFromFile(Path, true);
// If a change was made record it
if (IndexNumber.HasValue)
{
hasChanges = true;
}
}
if (!IndexNumberEnd.HasValue && !string.IsNullOrEmpty(Path))
{
IndexNumberEnd = TVUtils.GetEndingEpisodeNumberFromFile(Path);
// If a change was made record it
if (IndexNumberEnd.HasValue)
{
hasChanges = true;
}
}
}
if (!ParentIndexNumber.HasValue)
{
var season = Season;
if (season != null)
{
ParentIndexNumber = season.IndexNumber;
}
if (!ParentIndexNumber.HasValue && !string.IsNullOrEmpty(Path))
{
ParentIndexNumber = TVUtils.GetSeasonNumberFromEpisodeFile(Path);
}
// If a change was made record it
if (ParentIndexNumber.HasValue)
{
hasChanges = true;
}
}
return hasChanges;
}
}
}