// <copyright file="AudioBookResolver.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace Emby.Naming.AudioBook
{
    using System;
    using System.IO;
    using Emby.Naming.Common;
    using Jellyfin.Extensions;

    /// <summary>
    /// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
    /// </summary>
    public class AudioBookResolver
    {
        private readonly NamingOptions _options;

        /// <summary>
        /// Initializes a new instance of the <see cref="AudioBookResolver"/> class.
        /// </summary>
        /// <param name="options"><see cref="NamingOptions"/> containing AudioFileExtensions and also used to pass to AudioBookFilePathParser.</param>
        public AudioBookResolver(NamingOptions options)
        {
            this._options = options;
        }

        /// <summary>
        /// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
        /// </summary>
        /// <param name="path">Path to audiobook file.</param>
        /// <returns>Returns <see cref="AudioBookResolver"/> object.</returns>
        public AudioBookFileInfo? Resolve(string path)
        {
            if (path.Length == 0 || Path.GetFileNameWithoutExtension(path).Length == 0)
            {
                // Return null to indicate this path will not be used, instead of stopping whole process with exception
                return null;
            }

            var extension = Path.GetExtension(path);

            // Check supported extensions
            if (!this._options.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
            {
                return null;
            }

            var container = extension.TrimStart('.');

            var parsingResult = new AudioBookFilePathParser(this._options).Parse(path);

            return new AudioBookFileInfo(
                path,
                container,
                chapterNumber: parsingResult.ChapterNumber,
                partNumber: parsingResult.PartNumber);
        }
    }
}
