using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Quadarax.Foundation.Core.Reflection.Extensions
{
    public static class AssemblyExt
    {
        #region *** Private Fields ***
        #endregion
        #region *** Public Properties ***
        #endregion
        #region *** Constructors ***
        #endregion
        #region *** Public operations & overrides ***
        public static Version? GetVersion(this Assembly oAssembly)
        {
            if (oAssembly == null)
                throw new ArgumentNullException(nameof(oAssembly));

            return oAssembly.GetName().Version;
        }
        public static string GetDescription(this Assembly oAssembly)
        {
            var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), true).FirstOrDefault();
            if (oAttribute == null)
                return string.Empty;
            return ((AssemblyDescriptionAttribute)oAttribute).Description;
        }
        public static string GetProduct(this Assembly oAssembly)
        {
            var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyProductAttribute), true).FirstOrDefault();
            if (oAttribute == null)
                return string.Empty;
            return ((AssemblyProductAttribute)oAttribute).Product;
        }
        public static string GetCompany(this Assembly oAssembly)
        {
            var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true).FirstOrDefault();
            if (oAttribute == null)
                return string.Empty;
            return ((AssemblyCompanyAttribute)oAttribute).Company;
        }
        public static string GetCopyright(this Assembly oAssembly)
        {
            var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), true).FirstOrDefault();
            if (oAttribute == null)
                return string.Empty;
            return ((AssemblyCopyrightAttribute)oAttribute).Copyright;
        }
        public static string GetTrademark(this Assembly oAssembly)
        {
            var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyTrademarkAttribute), true).FirstOrDefault();
            if (oAttribute == null)
                return string.Empty;
            return ((AssemblyTrademarkAttribute)oAttribute).Trademark;
        }
        public static string GetAssemblyPath(this Assembly oAssembly)
        {
            if (oAssembly == null)
                throw new ArgumentNullException(nameof(oAssembly));

            return Path.GetDirectoryName(new Uri(oAssembly.Location)?.LocalPath) + Path.DirectorySeparatorChar;
        }
        public static IEnumerable<Type> GetTypesWithAttribute<TAttribute>(this Assembly oAssembly, bool bInherit)where TAttribute : System.Attribute
        {
            var types = oAssembly.GetTypes();
            return types.Where(x => x.IsDefined(typeof(TAttribute), bInherit)).ToArray();
        }

        #endregion
        #region *** Private operations & overrides ***
        #endregion


    }
}
