using System;
using System.Net.Http;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
//using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Components.Authorization;

using CommonHelpers.WASM;
using AFXUI;

namespace BookRegistry.Client {

//**************************************************************************************************
public class Program {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static async Task Main(string[] args)
{
   UProgram.SpecifyIsDebugConfiguration( IsDebugConfiguration );
   UProgram.SpecifyApplicationName( "BookRegistry" );
   UBreakdown.TabSubstSpaceCount = 4;

   UConsole.Print( BookRegistry_Client_Info.AssemblyName );
   UConsole.Print( BookRegistry_Client_Info.CopyrightString );
   UConsole.Print( "Compilation: {0}", ConfigurationName );

   var builder = WebAssemblyHostBuilder.CreateDefault( args );
   var services = builder.Services;
   services
      .AddOptions()
      .AddAuthorizationCore()
      .AddScoped<AuthenticationStateProvider,CustomAuthStateProvider>();
   services
      .AddSingleton<HomeService>()
      .AddSingleton<UserAuthorizationService>()
      .AddSingleton<WeatherForecastService>();
   builder.RootComponents.Add<App>( "#app" );

   builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

   var host = builder.Build();
   var provider = host.Services; //services.BuildServiceProvider();
   Services.SpecifyProvider( provider );
   ServicesAFX.SpecifyProvider( provider );

   await App.TryLoadConfiguration();
   UseLanguage( App.QueryLanguage );
/*
var su = new BookRegistry.Client.DB.ServiceUser_Info(true);
Session.Open(su, su.IsAdministrator ? ServiceRoleNames.Administrator : ServiceRoleNames.General, DateTime.UtcNow );
*/
//await Task.Delay( 5000 );
   await builder.Build().RunAsync();
}

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

[HThreadStatic]
public static void UseLanguage(string? language)
{
   string? cultureId = GetSpecificCultureId( language );
   SetCulture( cultureId!=null ? new CultureInfo( cultureId ) : null );
}

//--------------------------------------------------------------------------------------------------

static string? GetSpecificCultureId(string? language)
{
   if ( language == null )
      return null;
   switch ( language?.ToUpper() )
   {
   case "EN": return "en-US";
   case "RU": return "ru-RU";
   default: return null;
   } // switch
}

[HThreadStatic]
static void SetCulture(CultureInfo? culture)
{
   bool useInvariant = false;
   if ( culture == null )
   {
      culture = CultureInfo.InvariantCulture;
      useInvariant = true;
   }
   HThread.CurrentThread.CurrentCulture =
      HThread.CurrentThread.CurrentUICulture = culture;
   Console.WriteLine( "Main-thread culture(s): {0}",
      useInvariant ? "Invariant" : culture.ToString() );
}

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

public static bool IsDebugConfiguration => BookRegistry_Client_Info.IsDebugConfiguration;
public static bool IsReleaseConfiguration => BookRegistry_Client_Info.IsReleaseConfiguration;

public static string ConfigurationName => BookRegistry_Client_Info.ConfigurationName;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Program
//**************************************************************************************************

} // BookRegistry.Client
