using System;
using System.IO;
using System.Reflection;

namespace Quadarax.Foundation.Core.Resource.Extensions
{
    public static class AssemblyExt
    {
        public static string GetEmbeddedFileContentAsString(this Assembly owner, string fileName)
        {
            if (owner==null)
                throw new ArgumentNullException(nameof(owner));
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException(nameof(fileName));


            var resourceName = fileName;
            using var fs = owner.GetManifestResourceStream(resourceName);
            if (fs == null)
                throw new FileNotFoundException($"Cannot find embedded resource file '{resourceName}' is assembly '{owner.GetName().Name}'", resourceName);
            using var reader = new StreamReader(fs);
            return reader.ReadToEnd();
        }
    }
}
