// This Startup file is based on ASP.NET Core new project templates and is included
// as a starting point for DI registration and HTTP request processing pipeline configuration.
// This file will need updated according to the specific scenario of the application being upgraded.
// For more information on ASP.NET Core startup files, see https://docs.microsoft.com/aspnet/core/fundamentals/startup

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using EVCB_OCPP.WEBAPI.Handlers;
using EVCB_OCPP.WEBAPI.Helpers;
using EVCB_OCPP.WEBAPI.Middleware;
using EVCB_OCPP.WEBAPI.Services;
using EVCB_OCPP.WEBAPI.Services.Integration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace EVCB_OCPP.WEBAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        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.AddControllersWithViews(ConfigureMvcOptions)
                .AddControllersAsServices()
                // Newtonsoft.Json is added for compatibility reasons
                // The recommended approach is to use System.Text.Json for serialization
                // Visit the following link for more guidance about moving away from Newtonsoft.Json to System.Text.Json
                // https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                    options.SerializerSettings.DateFormatString = Configuration["UTC_DATETIMEFORMAT"];
                    options.UseMemberCasing();
                });

            services.AddSingleton<Services.ServerTriggerService>();
            services.AddSingleton<Services.ChargingStationService>();
            services.AddSingleton<Services.Integration.ServerTriggerService>();
            services.AddSingleton<Services.Integration.ChargingStationService>();
            services.AddSingleton<ICustomerService,CustomerService>();
            services.AddSingleton<ChargePointService>();
            services.AddSingleton<ChargePoint16Service>();
            services.AddSingleton<ChargePoint201Service>();

            services.AddSingleton<CPOAuthentication>();
            services.AddSingleton<InernalAuthentication>();
            services.AddSingleton<InternalHttpClient>();

            services.AddMemoryCache();
            services.AddSingleton<ApiLogDbService>();
            services.AddSingleton<MainDbService>();
            services.AddSingleton<MeterValueDbService>();
        }

        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseWhen(
                context => !context.Request.Path.StartsWithSegments("/file"),
                appBuilder =>
                {
                    appBuilder.UseMiddleware<PerformaceMonitorMiddleware>(new PerformaceMonitorMiddlewareOptions() { Label = "Outter" });
                    appBuilder.EnableRequestBodyRewind();
                    appBuilder.UseMiddleware<ExceptionMiddleware>();
                    appBuilder.UseMiddleware<APILogMiddleware>();
                    appBuilder.UseMiddleware<PerformaceMonitorMiddleware>(new PerformaceMonitorMiddlewareOptions() { Label = "Inter" });
                }
                );

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

        private void ConfigureMvcOptions(MvcOptions mvcOptions)
        { 
        }
    }
}
