using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Hosting;
using isn.Data;
using isn.Interfaces;
using isn.Services;
using isn.Entities;
using isn.Authorization;
using isn.Data.Roles;
using Microsoft.AspNetCore.Authorization;
using Unleash;
using System.Collections.Generic;
using System;
using Unleash.ClientFactory;
using isnd.Entities;
using Microsoft.Extensions.Options;

namespace isn
{
    public class Startup
    {
        public Startup(IConfiguration config)
        {
            Configuration = 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.AddDbContext<ApplicationDbContext>(options =>
                options.UseNpgsql(
                    Configuration.GetConnectionString("DefaultConnection")));


            services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddSignInManager()
                .AddDefaultUI()
                .AddDefaultTokenProviders();

            services.AddMvc();

            services.AddDataProtection();

            services.AddTransient<IMailer, EmailSender>();
            services.AddTransient<IEmailSender, EmailSender>();

            services.AddAuthorization(options =>
            {
                options.AddPolicy(Constants.RequireAdminPolicyName,
                    policy => policy.RequireRole(Constants.AdministratorRoleName));
                options.AddPolicy(Constants.RequireValidApiKey, policy =>
                    policy.Requirements.Add(new ValidApiKeyRequirement()));

            });

            services.AddSingleton<IAuthorizationHandler, ValidApiKeyRequirementHandler>();

            var smtpSettingsconf = Configuration.GetSection("Smtp");
            services.Configure<SmtpSettings>(smtpSettingsconf);
            var nugetSettingsconf = Configuration.GetSection("Nuget");
            services.Configure<NugetSettings>(nugetSettingsconf);
            var adminStartupListConf = Configuration.GetSection("AdminList");
            services.Configure<AdminStartupList>(adminStartupListConf);
            var unleashConf = Configuration.GetSection("Unleash");
            services.Configure<UnleashClientSettings>(unleashConf);
            services.Configure<MigrationsEndPointOptions>(o => o.Path = "~/migrate");
        }


        public static IUnleash UnleashĈlient { get; private set; }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, 
        Microsoft.AspNetCore.Hosting.IHostingEnvironment env,
        ApplicationDbContext dbContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
            }
            else
            {
                // app.UseExceptionHandler("/Home/Error");
                app.UseDeveloperExceptionPage();
                dbContext.Database.Migrate();
            }

            

            app.UseStatusCodePages().UseStaticFiles().UseAuthentication().UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=PackageVersion}/{action=Index}/{PackageId?}");
            });
        }
    }
}
