using Autofac;
using Autofac.Extras.DynamicProxy;
using IRaCIS.Core.Application;
using IRaCIS.Core.Application.AOP;
using IRaCIS.Core.Application.BackGroundJob;
using IRaCIS.Core.Infra.EFCore;
using Microsoft.AspNetCore.Http;
using Panda.DynamicWebApi;
using System;
using System.Linq;
using System.Reflection;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Domain.Share;
using MediatR;
using IRaCIS.Application.Services;
using IRaCIS.Application.Interfaces;
using AutoMapper;
using Quartz;

namespace IRaCIS.Core.API
{
    // ReSharper disable once IdentifierTypo
    public class AutofacModuleSetup : Autofac.Module
    {
        protected override void Load(ContainerBuilder containerBuilder)
        {

            #region  byzhouhang  20210917  此处注册泛型仓储 可以减少Domain层 和Infra.EFcore 两层 空的仓储接口定义和 仓储文件定义

            containerBuilder.RegisterGeneric(typeof(Repository<>))
                .As(typeof(IRepository<>)).InstancePerLifetimeScope();//注册泛型仓储

            containerBuilder.RegisterType<Repository>().As<IRepository>().InstancePerLifetimeScope();

            //containerBuilder.RegisterType<Mapper>().As<IMapper>().InstancePerLifetimeScope();

            //containerBuilder.RegisterGeneric(typeof(EFUnitOfWork<>))
            //    .As(typeof(IEFUnitOfWork<>)).InstancePerLifetimeScope();//注册仓储

            #endregion

            #region 指定控制器也由autofac 来进行实例获取 https://www.cnblogs.com/xwhqwer/p/15320838.html

            //获取所有控制器类型并使用属性注入
            containerBuilder.RegisterAssemblyTypes(typeof(BaseService).Assembly)
                .Where(type => typeof(IDynamicWebApi).IsAssignableFrom(type))
                .PropertiesAutowired();



            #endregion


            //containerBuilder.RegisterType<BaseService>().As<IBaseService>().PropertiesAutowired().InstancePerLifetimeScope();
            //containerBuilder.RegisterType<DictionaryService>().As<IDictionaryService>().InstancePerLifetimeScope();



            Assembly application = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "IRaCIS.Core.Application.dll");
            containerBuilder.RegisterAssemblyTypes(application).Where(t => t.FullName.Contains("Service"))
                .PropertiesAutowired().AsImplementedInterfaces().EnableClassInterceptors();

            //Assembly infrastructure = Assembly.Load("IRaCIS.Core.Infra.EFCore");
            //containerBuilder.RegisterAssemblyTypes(infrastructure).AsImplementedInterfaces();

            containerBuilder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
            containerBuilder.RegisterType<UserInfo>().As<IUserInfo>().InstancePerLifetimeScope();

            //containerBuilder.RegisterType<Dictionary>().InstancePerLifetimeScope();
            //Autofac 注册拦截器  需要注意的是生成api上服务上的动态代理AOP失效    间接掉用不影响
            //containerBuilder.RegisterType<TrialStatusAutofacAOP>();
            //containerBuilder.RegisterType<UserAddAOP>();
            //containerBuilder.RegisterType<QANoticeAOP>();
            //containerBuilder.RegisterType<LogService>().As<ILogService>().SingleInstance();


            //注册hangfire任务  依赖注入
            containerBuilder.RegisterType<ObtainTaskAutoCancelJob>().As<IObtainTaskAutoCancelJob>().InstancePerDependency();




        }
    }
}