using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using CommonHelpers;

namespace MyFlat {

//**************************************************************************************************
public partial class Startup {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static HConsole CON => HConsole.StdConIO;
//==================================================================================================

public IConfiguration Configuration { get; }

//==================================================================================================

/// <exception cref="ArgumentNullException" />
public Startup(IConfiguration configuration) =>
   Configuration = HArgChecking.NN( configuration );

//==================================================================================================

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
   new StartupData( Configuration ).ObtainConfigParameters();
   LocaleHlp.UpdateCulture();

   services.Configure<CookiePolicyOptions>(options =>
   {
      // This lambda determines whether user consent for non-essential cookies is needed
      // for a given request.
      options.CheckConsentNeeded = context => true;
      options.MinimumSameSitePolicy = SameSiteMode.None;
   });
   services.AddControllersWithViews().AddNewtonsoftJson();
   services.AddRazorPages();
   services.AddDistributedMemoryCache();
   services.AddSession(options =>
   {
      //options.IdleTimeout = ;
      options.Cookie.HttpOnly = true;
   });
   //services.AddTransient<IDBRepository, DBRepository>(
   //   provider => new DBRepository( connectionString ));
   services.AddMvc().AddSessionStateTempDataProvider();

   CON.PrintAlignedList(
      new[]{ "Server", "Database", "DB-language", "Culture neutral" },
      new[]{ StartupData.ServerName, StartupData.DatabaseName,
             StartupData.DatabaseLanguage, StartupData.UseInvariantCulture.ToString() } );
}

//==================================================================================================

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   Program.SpecifyDevelopmentBehaviour( env.IsDevelopment() ||
      HString.Equals( env.EnvironmentName, "DebugExpose", true ) ); // case insensitive comparison
   // This is in order to use fatal error message box in case of fatal breakdown.
   // Such system GUI-box is possible only in Win32-version of CommonHelpers library.
   // In any way, the library attempts to open such an error in browser
   // (specially prepared html-file in associated program).
   HProgram.SpecifyInteractsWithDesktop(
      Program.DevelopmentBehaviour && Program.IsDebugConfiguration );

   CON.PrintSplitter();
   CON.PrintDashAlignedList(
      new string[]{ "Current dir.", "Environment name", "Development" },
      new string[]{ Directory.GetCurrentDirectory(), env.EnvironmentName ?? "",
                    Program.DevelopmentBehaviour.ToString() } );
   CON.PrintShallowSplitter();

   if ( Program.DevelopmentBehaviour )
   {
      app.UseDeveloperExceptionPage();
   }
   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.UseCookiePolicy();
   app.UseRouting();
   //app.UseAuthorization();
   app.UseStatusCodePages();
   app.UseSession();
   app.UseEndpoints(endpoints =>
   {
      endpoints.MapControllerRoute(
         name: "default",
         pattern: "{controller=Home}/{action=Index}/{id?}" );
      endpoints.MapRazorPages();
   });
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Startup
//**************************************************************************************************

} // MyFlat
