using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using QC.QTMInterface;

namespace QC.QSPRSchedulerWrapper;

public class MefPluginLoader
{
	private CompositionContainer typeContainer = new CompositionContainer();

	public void Load(string directoryPath)
	{
		if (Directory.Exists(directoryPath))
		{
			AggregateCatalog aggregateCatalog = new AggregateCatalog();
			aggregateCatalog.Catalogs.Add(new DirectoryCatalog(directoryPath));
			typeContainer = new CompositionContainer(aggregateCatalog);
		}
	}

	public IEnumerable<T> GetExportedValues<T>(List<string> pluginNamesToLoad = null)
	{
		List<T> list = new List<T>();
		IEnumerable<Lazy<T>> exports = typeContainer.GetExports<T>();
		foreach (Lazy<T> item in exports)
		{
			try
			{
				list.Add(item.Value);
				if (pluginNamesToLoad != null)
				{
					BasePlugin basePlugin = item.Value as BasePlugin;
					if (!pluginNamesToLoad.Contains(basePlugin.Name))
					{
						list.Remove(item.Value);
						typeContainer.ReleaseExport(item);
						basePlugin.Dispose();
					}
				}
			}
			catch (Exception)
			{
			}
		}
		return list;
	}

	public void ReleaseExport<T>(T export)
	{
	}
}
