namespace WebApplication1
{
    using System.Net;
    using ScoreboardApi;
    using ScoreboardApi.Extensions;
    using ScoreboardApi.Functions;
    using ScoreboardApi.Settings;
    using System.Reflection;
    using System.Text.Json.Serialization;
    using Microsoft.AspNetCore.HttpOverrides;

    public class Program
    {
        public static void Main(string[] args)
        {
            WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
            builder.Services.Configure<ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.All; });

            // Add services to the container.
            ApplicationSettings appSettings = DeserializeApplicationSettings();
            builder.Services.AddSingleton(appSettings);

            builder.Services.AddControllers().AddJsonOptions(
                x =>
                {
                    var enumConverter = new JsonStringEnumConverter();
                    x.JsonSerializerOptions.Converters.Add(enumConverter);
                });

            builder.Services.AddCors(
                options =>
                {
                    options.AddPolicy(
                        name: "allowedOrigins",
                        policy =>
                        {
                            policy
                                .AllowAnyOrigin()
                                .WithHeaders("x-api-key")
                                .WithHeaders("content-type")
                                .AllowAnyMethod();
                        });
                });

            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();

            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

            builder.Services.AddSwaggerGen(
                x =>
                    x.IncludeXmlComments(xmlPath));

            builder.Services.AddSingleton<IPointsCalculator, PointsCalculator>();
            builder.Services.AddSingleton<IGameManager, GameManager>();

            WebApplication app = builder.Build();
            app.UseForwardedHeaders();
            app.UseCors("allowedOrigins");
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            //app.UseHttpsRedirection();
            app.UseApiKeyAuthentication();
            app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }

        private static ApplicationSettings DeserializeApplicationSettings()
        {
            var applicationSettings = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build()
                .Get<ApplicationSettings>();

            applicationSettings.Verify<ApplicationSettings>();

            return applicationSettings;
        }
    }
}