using log4net.Config;
using log4net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Ropin.Inspection.Common.Helper;
using Ropin.Environmentally.DcsService.AppSetingModel;
using InitQ;
using Microsoft.EntityFrameworkCore;
using Ropin.Inspection.Model.Entities;
using Ropin.Environmentally.DcsService.Subscribe;
using Ropin.Inspection.Repository.DEV.Interface;
using Ropin.Inspection.Repository.DEV;
using Ropin.Inspection.Repository;
using Ropin.Environmentally.DcsService.Service;
using Ropin.Inspection.Repository.LGS.Interface;
using Ropin.Inspection.Repository.LGS;

namespace Ropin.Environmentally.DcsService
{
    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();
            #region �����ļ�
            var rabbitMQ = Configuration.GetSection("RabbitMQ").Get<RabbitMQModel>();
            services.AddSingleton<RabbitMQModel>(rabbitMQ);
            var IniInfluxData = Configuration.GetSection("IniInflux").Get<IniInfluxDB>();
            services.AddSingleton<IniInfluxDB>(IniInfluxData);
            var cmd = Configuration.GetSection("Cmd").Get<CmdModel>();
            services.AddSingleton<CmdModel>(cmd);
            #endregion
            #region 
            //services.AddTransient<IDevCmdRepository, DevCmdRepository>();
            //services.AddTransient<IDevInstructionRepository, DevInstructionRepository>();
            services.AddTransient<ITdevBoxDevSpotRepository, TdevBoxDevSpotRepository>();
            //services.AddTransient<ITdevWebScadaDevSpotRepository, TdevWebScadaDevSpotRepository>();
            services.AddTransient<ITdevBoxRepository, TdevBoxRepository>();
            services.AddTransient<ILargeScreenEventRepository, LargeScreenEventRepository>();
            #endregion
            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();
            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.DcsService", Version = "v1" });
            });
        }

        // 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("/swagger/v1/swagger.json", "Ropin.Environmentally.DcsService v1"));

            //app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

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