using BPASmartClient.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.Business
{
    public class Plugin : Singleton<Plugin>, IDisposable
    {
        private List<IPlugin> plugins = new List<IPlugin>();

        public void Dispose()
        {
            plugins.ForEach(p => p.Dispose());
        }

        public void Init()
        {
            this.GetType().Assembly.GetTypes().ToList().ForEach(plugin =>
            {
                if (plugin.GetInterfaces().Contains(typeof(IPlugin)))
                {
                    plugins.Add((IPlugin)Activator.CreateInstance(plugin));
                }
            });

            plugins.ForEach(p => {
                p.Initialize();
                p.Start();
            });
        }

        public T GetPlugin<T>() where T : IPlugin
        {
            return (T)plugins.FirstOrDefault(p => p.GetType().Equals(typeof(T)));
        }
    }
}
