using System.IO;
using System.Linq;
using BDInfo.IO;
using MediaBrowser.Model.IO;
namespace MediaBrowser.MediaEncoding.BdInfo;
/// 
/// Class BdInfoDirectoryInfo.
/// 
public class BdInfoDirectoryInfo : IDirectoryInfo
{
    private readonly IFileSystem _fileSystem;
    private readonly FileSystemMetadata _impl;
    /// 
    /// Initializes a new instance of the  class.
    /// 
    /// The filesystem.
    /// The path.
    public BdInfoDirectoryInfo(IFileSystem fileSystem, string path)
    {
        _fileSystem = fileSystem;
        _impl = _fileSystem.GetDirectoryInfo(path);
    }
    private BdInfoDirectoryInfo(IFileSystem fileSystem, FileSystemMetadata impl)
    {
        _fileSystem = fileSystem;
        _impl = impl;
    }
    /// 
    /// Gets the name.
    /// 
    public string Name => _impl.Name;
    /// 
    /// Gets the full name.
    /// 
    public string FullName => _impl.FullName;
    /// 
    /// Gets the parent directory information.
    /// 
    public IDirectoryInfo? Parent
    {
        get
        {
            var parentFolder = Path.GetDirectoryName(_impl.FullName);
            if (parentFolder is not null)
            {
                return new BdInfoDirectoryInfo(_fileSystem, parentFolder);
            }
            return null;
        }
    }
    /// 
    /// Gets the directories.
    /// 
    /// An array with all directories.
    public IDirectoryInfo[] GetDirectories()
    {
        return _fileSystem.GetDirectories(_impl.FullName)
            .Select(x => new BdInfoDirectoryInfo(_fileSystem, x))
            .ToArray();
    }
    /// 
    /// Gets the files.
    /// 
    /// All files of the directory.
    public IFileInfo[] GetFiles()
    {
        return _fileSystem.GetFiles(_impl.FullName)
            .Select(x => new BdInfoFileInfo(x))
            .ToArray();
    }
    /// 
    /// Gets the files matching a pattern.
    /// 
    /// The search pattern.
    /// All files of the directory matchign the search pattern.
    public IFileInfo[] GetFiles(string searchPattern)
    {
        return _fileSystem.GetFiles(_impl.FullName, new[] { searchPattern }, false, false)
            .Select(x => new BdInfoFileInfo(x))
            .ToArray();
    }
    /// 
    /// Gets the files matching a pattern and search options.
    /// 
    /// The search pattern.
    /// The search optin.
    /// All files of the directory matchign the search pattern and options.
    public IFileInfo[] GetFiles(string searchPattern, SearchOption searchOption)
    {
        return _fileSystem.GetFiles(
                _impl.FullName,
                new[] { searchPattern },
                false,
                searchOption == SearchOption.AllDirectories)
            .Select(x => new BdInfoFileInfo(x))
            .ToArray();
    }
    /// 
    /// Gets the bdinfo of a file system path.
    /// 
    /// The file system.
    /// The path.
    /// The BD directory information of the path on the file system.
    public static IDirectoryInfo FromFileSystemPath(IFileSystem fs, string path)
    {
        return new BdInfoDirectoryInfo(fs, path);
    }
}