#nullable disable
using System.Globalization;
using Emby.Naming.Common;
using Emby.Naming.TV;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Globalization;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.Library.Resolvers.TV
{
///
/// Class SeasonResolver.
///
public class SeasonResolver : GenericFolderResolver
{
private readonly ILocalizationManager _localization;
private readonly ILogger _logger;
private readonly NamingOptions _namingOptions;
///
/// Initializes a new instance of the class.
///
/// The naming options.
/// The localization.
/// The logger.
public SeasonResolver(
NamingOptions namingOptions,
ILocalizationManager localization,
ILogger logger)
{
_namingOptions = namingOptions;
_localization = localization;
_logger = logger;
}
///
/// Resolves the specified args.
///
/// The args.
/// Season.
protected override Season Resolve(ItemResolveArgs args)
{
if (args.Parent is Series series && args.IsDirectory)
{
var namingOptions = _namingOptions;
var path = args.Path;
var seasonParserResult = SeasonPathParser.Parse(path, true, true);
var season = new Season
{
IndexNumber = seasonParserResult.SeasonNumber,
SeriesId = series.Id,
SeriesName = series.Name
};
if (!season.IndexNumber.HasValue || !seasonParserResult.IsSeasonFolder)
{
var resolver = new Naming.TV.EpisodeResolver(namingOptions);
var folderName = System.IO.Path.GetFileName(path);
var testPath = "\\\\test\\" + folderName;
var episodeInfo = resolver.Resolve(testPath, true);
if (episodeInfo?.EpisodeNumber is not null && episodeInfo.SeasonNumber.HasValue)
{
_logger.LogDebug(
"Found folder underneath series with episode number: {0}. Season {1}. Episode {2}",
path,
episodeInfo.SeasonNumber.Value,
episodeInfo.EpisodeNumber.Value);
return null;
}
}
if (season.IndexNumber.HasValue)
{
var seasonNumber = season.IndexNumber.Value;
if (string.IsNullOrEmpty(season.Name))
{
var seasonNames = series.SeasonNames;
if (seasonNames.TryGetValue(seasonNumber, out var seasonName))
{
season.Name = seasonName;
}
else
{
season.Name = seasonNumber == 0 ?
args.LibraryOptions.SeasonZeroDisplayName :
string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("NameSeasonNumber"),
seasonNumber,
args.LibraryOptions.PreferredMetadataLanguage);
}
}
}
return season;
}
return null;
}
}
}