using GameIdeas.Resources;
using System.Resources;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

builder.Services.AddCors(option => option.AddDefaultPolicy(policy =>
    policy.WithOrigins("http://localhost:5172")
        .AllowAnyHeader()
        .WithMethods("GET", "POST")));

builder.Services.AddSingleton<TranslationService>();
builder.Services.AddSingleton<Translations>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files");
var translationFiles = Directory.GetFiles(filesDirectory, "*.json");
var dictionary = new Dictionary<string, string>();
foreach (var file in translationFiles)
{
    var name = file.Split('.');
    var culture = name[^2];
    var content = await File.ReadAllTextAsync(file);
    dictionary.Add(culture, content);
}

app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
ResourcesKey.Initialize(app.Services.GetRequiredService<Translations>());

app.UseCors();
app.UseAuthorization();

app.MapControllers();

app.Run();
