using System;

namespace Quadarax.Foundation.Core.Reflection.Extensions
{
    public static class ActivatorExt
    {
        #region *** Private Fields ***
        #endregion
        #region *** Public Properties ***
        #endregion
        #region *** Constructors ***
        #endregion
        #region *** Public operations & overrides ***
        public static object? CreateInstance(string sClassOrInterfaceQualifiedName, bool bLoadAssemblyIfNeeded, params object[] aConstructorParams)
        {
            var oType = Type.GetType(sClassOrInterfaceQualifiedName, false) ?? TypeUtils.GetRemoteType(sClassOrInterfaceQualifiedName);
            if (oType == null) return null;

            if (oType.IsInterface)
            {
                throw new ArgumentException($"Input class name '{sClassOrInterfaceQualifiedName}' cannot be an interface.",nameof(sClassOrInterfaceQualifiedName));
            }
            return Activator.CreateInstance(oType, aConstructorParams);
        }
        public static T CreateInstance<T>(string sClassOrInterfaceQualifiedName, bool bLoadAssemblyIfNeeded, params object[] aConstructorParams)
        {
            return (T) CreateInstance(sClassOrInterfaceQualifiedName, bLoadAssemblyIfNeeded, aConstructorParams)!;
        }
        #endregion
        #region *** Private operations & overrides ***
        #endregion        
    }
}
