using log4net.Config;
using log4net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System.IO;
using System.Reflection;
using Ropin.Inspection.Common.Helper;
using InitQ;
using Microsoft.EntityFrameworkCore;
using Ropin.Inspection.Model.Entities;
using System;
using Ropin.Environmentally.AlarmService.Service;
using Ropin.Environmentally.AlarmService.Subscribe;
using System.Collections.Generic;
using Ropin.Environmentally.AlarmService.Model;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Common.Accessor;
using Microsoft.AspNetCore.Http;
using Autofac;
using Ropin.Core.Extensions.ServiceExtensions;
using Ropin.Environmentally.AlarmService.Helper;

namespace Ropin.Environmentally.AlarmService
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
            XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddSingleton(new Appsettings(Configuration));
            services.AddHttpClient();
            services.AddControllersWithViews();
            services.AddControllers();

            var cofingModel = Configuration.GetSection("CofingSet").Get<CofingSetModel>();
            services.AddSingleton<CofingSetModel>(cofingModel);
            //services.AddTransient<Idev_DevOpeAccountConfigRepository, dev_DevOpeAccountConfigRepository>();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton<IPrincipalAccessor, PrincipalAccessor>();
            services.AddSingleton<IClaimsAccessor, ClaimsAccessor>();
            //services.AddTransient<IPushMsgService, PushMsgService>();
            //services.AddRazorPages();
            //services.AddScoped<IPushMsgService, PushMsgService>();
            services.AddAutoMapper(typeof(AutoMapperProfile));
            //services.AddScoped<Filters.AuthorExistFilterAttribute>();
            services.AddHostedService<RabbitMQReceiveService>();
            //services.AddInitQ(m =>
            //{
            //    m.SuspendTime = 1000;
            //    m.ConnectionString = Configuration["ConnectionSetting:RedisConnection"];
            //    m.ListSubscribe = new List<Type>() { typeof(RedisSubscribeBoxno), typeof(RedisIntervalSubscribeBoxno) };
            //    m.ShowLog = false;
            //});
            services.AddNodeServices();
            services.AddSignalR();
            //services.AddTransient<DevEventListener>();
            var connectionString = Configuration["ConnectionSetting:MySqlConnection"];
            ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
            services.AddDbContext<InspectionDbContext>(options =>
                options.UseMySql(connectionString, serverVersion));
            //���ܶೡ����Scoped�ֲ����������ǣ���ʱ�����Ҫע��һ��Singleton��DBContext�Ĺ�������
            //services.AddSingleton<Func<InspectionDbContext>>(() =>
            //{
            //    var optionsBuilder = new DbContextOptionsBuilder<InspectionDbContext>();
            //    optionsBuilder.UseMySql(connectionString, serverVersion);
            //    //��������������Ļ�������ͨ��provider.GetService<XX>()����ȡ
            //    return new InspectionDbContext(optionsBuilder.Options);
            //});


            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Ropin.Environmentally.AlarmService", Version = "v1" });
            });
        }
        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterModule(new AutofacModuleRegister());
            //builder.RegisterModule<AutofacPropertityModuleReg>();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "Ropin.Environmentally.AlarmService v1"));
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}