using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using chapitre5_aspnet_core_bookshop.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration.Memory;

namespace chapitre5_aspnet_core_bookshop
{
    public class Startup
    {
        /// <summary>
        /// Constructeur de la classe Startup
        /// </summary>
        /// <param name="configuration">Dépendance injectée : configuration</param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        /// <summary>
        /// Appelée par le runtime .NET Core. 
        /// Cette méthode sert à configurer des services (dépendances) comme EntityFramework, l'authentification
        /// </summary>
        /// <param name="services">Services de l'application (dépendances)</param>
        public void ConfigureServices(IServiceCollection services)
        {
            // active le support EntityFramework SQL Server
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            // active le service d'authentification Identity
            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();
            
            // active le service MVC
            services.AddControllersWithViews();

            // active le service de rendu de vue Razor
            services.AddRazorPages();

            services.AddScoped(typeof(Services.ILibrairieService), typeof(Services.LibairieService));
        }

        /// <summary>
        /// Appelée par le runtime. 
        /// Configure le pipeline de traitement des requêtes
        /// </summary>
        /// <param name="app">Instance d'application</param>
        /// <param name="env">Environement d'exécution</param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

            // Récupérer la configuration 
            var config = new ConfigurationBuilder()
                .AddJsonFile("appSettings.json").Build();

            // Récupération de la valeur 
            string param1 = config["parametre1"];
            string param2_1 = config["parametre2:param2.1"];
        }
    }
}
