using Backend.Data;
using Backend.Data.Config;
using Backend.Data.Interfaces;
using Backend.Data.Repositories;
using Backend.Services;
using Backend.Services.Interface;
using Backend.Services.Mapper;
using Mediator;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Reflection;
using System.Threading.Tasks;

namespace Backend.API
{
    public class Startup
    {
        private readonly IConfiguration _configuration = null;
        private readonly IHostEnvironment _appEnvironment = null;
        public Startup(IConfiguration configuration, IHostEnvironment appEnvironment)
        {
            _configuration = configuration;
            _appEnvironment = appEnvironment;
        }

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

            AddDataDependencyInjectionService(services);

            AddDataDependencyInjectionRepository(services);

            AddDatabase(services);

            AddMediaRHandlerDependency(services);

            services.AddAutoMapper(typeof(AutoMapperProfile));

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v2", new OpenApiInfo { Title = "Catalogue.Backend.API", Version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion });
            });

        }

        private static void AddDataDependencyInjectionService(IServiceCollection services)
        {
            services.AddScoped<ITypeProprieteService, TypeProprieteService>();
            services.AddScoped<IProprieteService, ProprieteService>();
            services.AddScoped<IProduitService, ProduitService>();
            services.AddScoped<ICommandeService, CommandeService>();
            services.AddScoped<ILigneCommandeService, LigneCommandeService>();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }

        private static void AddMediaRHandlerDependency(IServiceCollection services)
        {
            services.AddMediatR(Assembly.GetExecutingAssembly());
            services.AddMediatR(typeof(GetTypeProprieteListHandler).Assembly);
            services.AddMediatR(typeof(AddTypeProprieteHandler).Assembly);
            services.AddMediatR(typeof(RemoveTypeProprieteHandler).Assembly);

            services.AddMediatR(typeof(GetProprieteListHandler).Assembly);
            services.AddMediatR(typeof(AddProprieteHandler).Assembly);
            services.AddMediatR(typeof(RemoveProprieteHandler).Assembly);

            services.AddMediatR(typeof(GetProduitListHandler).Assembly);
            services.AddMediatR(typeof(AddProduitHandler).Assembly);
            services.AddMediatR(typeof(RemoveProduitHandler).Assembly);

            services.AddMediatR(typeof(GetCommandeListHandler).Assembly);
            services.AddMediatR(typeof(AddCommandeHandler).Assembly);
            services.AddMediatR(typeof(RemoveCommandeHandler).Assembly);

            services.AddMediatR(typeof(GetLigneCommandeListHandler).Assembly);
            services.AddMediatR(typeof(AddLigneCommandeHandler).Assembly);
            services.AddMediatR(typeof(RemoveLigneCommandeHandler).Assembly);
        }

        private static void AddDataDependencyInjectionRepository(IServiceCollection services)
        {
            services.AddScoped<IApplicationDatabaseContext, ApplicationDatabaseContext>();
            services.AddScoped<ICommandeRepository, CommandeRepository>();
            services.AddScoped<ILigneCommandeRepository, LigneCommandeRepository>();
            services.AddScoped<IProduitRepository, ProduitRepository>();
            services.AddScoped<IProprieteRepository, ProprieteRepository>();
            services.AddScoped<ITypeProprieteRepository, TypeProprieteRepository>();
        }
        protected virtual void AddDatabase(IServiceCollection services)
        {
            if (services != null)
            {
                services.AddDatabaseModule(_configuration);
            }
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            app.UseApplicationDatabase(serviceProvider);
            app.UseSwagger();
            app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/v2/swagger.json", "Catalogue.Backend.API");
            });
        }

    

    }
}
