using Barcode.Host.Server.HostedService;
using Barcode.Host.Server.Hubs;
using Barcode.Host.Server.Models;
using Barcode.Host.Server.Services;
using Barcode.Host.Shared.Models.Stateless;

namespace Barcode.Host.Server;

public class Program
{

    public static int Main(string[] args)
    {
        ILogger<Program>? logger = null;
        WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(args);
        _ = webApplicationBuilder.Configuration.AddUserSecrets<Program>();
        AppSettings appSettings = Models.Binder.AppSettings.Get(webApplicationBuilder.Configuration);
        if (string.IsNullOrEmpty(appSettings.Company))
            throw new Exception("Company name must have a value!");
        try
        {
            if (appSettings.IsStaging && appSettings.IsDevelopment)
                throw new NotSupportedException("Please check appsettings file(s)!");
            if (appSettings.IsStaging != webApplicationBuilder.Environment.IsStaging())
                throw new NotSupportedException("Please check appsettings file(s)!");
            if (appSettings.IsDevelopment != webApplicationBuilder.Environment.IsDevelopment())
                throw new NotSupportedException("Please check appsettings file(s)!");
            _ = webApplicationBuilder.Services.AddRazorPages();
            _ = webApplicationBuilder.Services.AddSignalR();
            _ = webApplicationBuilder.Services.AddControllersWithViews();
            _ = webApplicationBuilder.Services.AddSingleton(_ => appSettings);
            _ = webApplicationBuilder.Services.AddHttpClient();
            _ = webApplicationBuilder.Services.AddSingleton<IFileService, FileService>();
            _ = webApplicationBuilder.Services.AddSingleton<IPostService, PostService>();
            _ = webApplicationBuilder.Services.AddSingleton<ISerialService, SerialService>();
            _ = webApplicationBuilder.Services.AddSingleton<ISerialService, SerialService>();
            _ = webApplicationBuilder.Services.AddSingleton<ILastScanService, LastScanService>();
            _ = webApplicationBuilder.Services.AddSingleton<ILinuxGroupManager, LinuxGroupManager>();
            _ = webApplicationBuilder.Services.AddHostedService<TimedHostedService>();
            _ = webApplicationBuilder.Services.AddSwaggerGen();
            WebApplication webApplication = webApplicationBuilder.Build();
            logger = webApplication.Services.GetRequiredService<ILogger<Program>>();
            if (appSettings.IsDevelopment)
            {
                _ = webApplication.UseSwagger();
                _ = webApplication.UseDeveloperExceptionPage();
                _ = webApplication.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Server V1"));
            }
            if (!appSettings.IsDevelopment)
            {
                _ = webApplication.UseExceptionHandler("/Error");
                _ = webApplication.UseHsts();
            }
            _ = webApplication.UseStaticFiles();
            _ = webApplication.UseRouting();
            _ = webApplication.UseAuthorization();
            _ = webApplication.MapControllers();
            _ = webApplication.MapRazorPages();
            _ = webApplication.MapHub<NotificationHub>($"/{nameof(NotificationHub)}");
            logger.LogInformation("Starting Web Application");
            logger.LogCritical("{Company}", appSettings.Company);
            webApplication.Run();
            return 0;
        }
        catch (Exception ex)
        {
            try
            { logger?.LogCritical(ex, "Host terminated unexpectedly"); }
            catch (Exception) { }
            throw;
        }
    }

}