
namespace DiscUtils
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using DiscUtils.Vfs;

    /// <summary>
    /// FileSystemManager determines which file systems are present on a volume.
    /// </summary>
    /// <remarks>
    /// The static detection methods detect default file systems.  To plug in additional
    /// file systems, create an instance of this class and call RegisterFileSystems.
    /// </remarks>
    public sealed class FileSystemManager
    {
        private static List<VfsFileSystemFactory> s_detectedFactories;
        private static FileSystemManager s_defaultInstance = new FileSystemManager();

        private List<VfsFileSystemFactory> _factories;

        /// <summary>
        /// Initializes a new instance of the FileSystemManager class.
        /// </summary>
        public FileSystemManager()
        {
            _factories = new List<VfsFileSystemFactory>(DetectedFactories);
        }

        private static List<VfsFileSystemFactory> DetectedFactories
        {
            get
            {
                if (s_detectedFactories == null)
                {
                    s_detectedFactories = DetectFactories(typeof(FileSystemManager).Assembly);
                }

                return s_detectedFactories;
            }
        }

        /// <summary>
        /// Detect which file systems are present on a volume.
        /// </summary>
        /// <param name="volume">The volume to inspect.</param>
        /// <returns>The list of file systems detected.</returns>
        public static FileSystemInfo[] DetectDefaultFileSystems(VolumeInfo volume)
        {
            return s_defaultInstance.DetectFileSystems(volume);
        }

        /// <summary>
        /// Detect which file systems are present in a stream.
        /// </summary>
        /// <param name="stream">The stream to inspect.</param>
        /// <returns>The list of file systems detected.</returns>
        public static FileSystemInfo[] DetectDefaultFileSystems(Stream stream)
        {
            return s_defaultInstance.DetectFileSystems(stream);
        }

        /// <summary>
        /// Registers new file systems with an instance of this class.
        /// </summary>
        /// <param name="factory">The detector for the new file systems.</param>
        public void RegisterFileSystems(VfsFileSystemFactory factory)
        {
            _factories.Add(factory);
        }

        /// <summary>
        /// Registers new file systems detected in an assembly.
        /// </summary>
        /// <param name="assembly">The assembly to inspect.</param>
        /// <remarks>
        /// To be detected, the <c>VfsFileSystemFactory</c> instances must be marked with the
        /// <c>VfsFileSystemFactoryAttribute</c>> attribute.
        /// </remarks>
        public void RegisterFileSystems(Assembly assembly)
        {
            _factories.AddRange(DetectFactories(assembly));
        }

        /// <summary>
        /// Detect which file systems are present on a volume.
        /// </summary>
        /// <param name="volume">The volume to inspect.</param>
        /// <returns>The list of file systems detected.</returns>
        public FileSystemInfo[] DetectFileSystems(VolumeInfo volume)
        {
            using (Stream s = volume.Open())
            {
                return DoDetect(s, volume);
            }
        }

        /// <summary>
        /// Detect which file systems are present in a stream.
        /// </summary>
        /// <param name="stream">The stream to inspect.</param>
        /// <returns>The list of file systems detected.</returns>
        public FileSystemInfo[] DetectFileSystems(Stream stream)
        {
            return DoDetect(stream, null);
        }

        private static List<VfsFileSystemFactory> DetectFactories(Assembly assembly)
        {
            List<VfsFileSystemFactory> factories = new List<VfsFileSystemFactory>();

            foreach (var type in assembly.GetTypes())
            {
                foreach (VfsFileSystemFactoryAttribute attr in Attribute.GetCustomAttributes(type, typeof(VfsFileSystemFactoryAttribute), false))
                {
                    factories.Add((VfsFileSystemFactory)Activator.CreateInstance(type));
                }
            }

            return factories;
        }

        private FileSystemInfo[] DoDetect(Stream stream, VolumeInfo volume)
        {
            BufferedStream detectStream = new BufferedStream(stream);
            List<FileSystemInfo> detected = new List<FileSystemInfo>();

            foreach (var factory in _factories)
            {
                detected.AddRange(factory.Detect(detectStream, volume));
            }

            return detected.ToArray();
        }
    }
}
