using System.Globalization;
using System.Text.RegularExpressions;
using Emby.Naming.Common;
namespace Emby.Naming.AudioBook
{
///
/// Helper class to retrieve name and year from audiobook previously retrieved name.
///
public class AudioBookNameParser
{
private readonly NamingOptions _options;
///
/// Initializes a new instance of the class.
///
/// Naming options containing AudioBookNamesExpressions.
public AudioBookNameParser(NamingOptions options)
{
_options = options;
}
///
/// Parse name and year from previously determined name of audiobook.
///
/// Name of the audiobook.
/// Returns object.
public AudioBookNameParserResult Parse(string name)
{
AudioBookNameParserResult result = default;
foreach (var expression in _options.AudioBookNamesExpressions)
{
var match = new Regex(expression, RegexOptions.IgnoreCase).Match(name);
if (match.Success)
{
if (result.Name == null)
{
var value = match.Groups["name"];
if (value.Success)
{
result.Name = value.Value;
}
}
if (!result.Year.HasValue)
{
var value = match.Groups["year"];
if (value.Success)
{
if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
{
result.Year = intValue;
}
}
}
}
}
if (string.IsNullOrEmpty(result.Name))
{
result.Name = name;
}
return result;
}
}
}