using Edge.Core.Processor.Dispatcher;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edge.Core.UniversalApi.Auditing
{
    public static class AuditingStoreFactory
    {
        public static IEnumerable<IAuditingStore> Create(IServiceProvider services)
        {
            var auditingStoreTypes = new List<Type>();
            foreach (var ass in ObjectInstanceCreator.CurrentDomainAssemblies)
            {
                try
                {
                    //var ass = Assembly.LoadFrom(ai.FullName);
                    var t = ass.GetTypes().Where(t => typeof(IAuditingStore).IsAssignableFrom(t)
                        && !t.IsInterface);
                    if (t != null && t.Any()) auditingStoreTypes.AddRange(t);
                }
                catch
                { }
            }

            var auditStores = new List<IAuditingStore>();
            foreach (var pType in auditingStoreTypes)
            {
                //here assume ICommunicationProvider must have 1 parameter constructor of IServiceProvider, need refine.
                //var providerInstance =
                //    Activator.CreateInstance(pType, new[] { services.BuildServiceProvider() }) as ICommunicationProvider;
                var instance = ObjectInstanceCreator.Create(pType.FullName, "", new Type[] { }, new object[] { services });
                //Activator.CreateInstance(pType, null) as ICommunicationProvider;
                auditStores.Add((IAuditingStore)instance);
            }

            return auditStores;
        }
    }
}
