using Microsoft.Extensions.Logging;

namespace BarContracts.DI
{
	public class DependencyManager
	{
		private readonly IDependencyContainer _dependencyContainer;
		private static DependencyManager? _manager;

		private static readonly object _lock = new();

		private DependencyManager()
		{
			_dependencyContainer = new ServiceDependencyContainer();
		}

		public static DependencyManager Instance 
		{
			get 
			{ 
				if (_manager == null) 
				{
					lock (_lock) { _manager = new DependencyManager(); } 
				} 
				return _manager; 
			} 
		}

		public static void InitDependency()
		{
			var Ext = ServiceProviderLoader.GetImplementationExtension();
			if (Ext == null)
			{
				throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
			}

			Ext.RegisterServices();
		}

		public void AddLogging(Action<ILoggingBuilder> Configure) => _dependencyContainer.AddLogging(Configure);

		public void RegisterType<T, U>(bool IsSingleton = false) where U : class, T where T : class => _dependencyContainer.RegisterType<T, U>(IsSingleton);

		public void RegisterType<T>(bool IsSingleton = false) where T : class => _dependencyContainer.RegisterType<T>(IsSingleton);

		public T Resolve<T>() => _dependencyContainer.Resolve<T>();
	}
}
