using System;
using System.Net.Http;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authorization;

using CommonHelpers.WASM;
using AFXUI;

namespace BookRegistry.Client {

//**************************************************************************************************
static class Services {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// <exception cref="InvalidOperationException" />
[HStrictResult]
public static IServiceProvider Provider =>
   _Provider_ ?? throw new InvalidOperationException(
      "Service provider has not been specified." );

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

// <exception cref="InvalidOperationException" />
public static R GetService<R>() where R: notnull =>
   Provider.GetRequiredService<R>() ?? throw HAssertion.FAIL();

//--------------------------------------------------------------------------------------------------
/*
// <exception cref="InvalidOperationException" />
public static IJSRuntime JSRuntime =>
   RetreiveService<IJSRuntime>( ref _JSRuntime_ );
// <exception cref="InvalidOperationException" />
public static IJSInProcessRuntime JSInProcessRuntime =>
   RetreiveService<IJSInProcessRuntime>( ref _JSInProcessRuntime_ );
*/
// <exception cref="InvalidOperationException" />
public static HttpClient HttpClient =>
   RetreiveService<HttpClient>( ref _HttpClient_ );
// <exception cref="InvalidOperationException" />
public static NavigationManager NavigationManager =>
   RetreiveService<NavigationManager>( ref _NavigationManager_ );
// <exception cref="InvalidOperationException" />
public static IAuthorizationService AuthorizationService =>
   RetreiveService<IAuthorizationService>( ref _AuthorizationService_ );
// <exception cref="InvalidOperationException" />
public static HomeService HomeService =>
   RetreiveService<HomeService>( ref _HomeService_ );
// <exception cref="InvalidOperationException" />
public static UserAuthorizationService UserAuthorizationService =>
   RetreiveService<UserAuthorizationService>( ref _UserAuthorizationService_ );
/*
// <exception cref="InvalidOperationException" />
public static ...Service ...Service =>
   RetreiveService<...Service>( ref _...Service_ );
*/
//==================================================================================================

public static void SpecifyProvider(IServiceProvider provider) =>
   _Provider_ = provider;

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

// <exception cref="InvalidOperationException" />
static T RetreiveService<T>(ref T? fieldValue) where T: class =>
   fieldValue ?? (fieldValue = GetService<T>());

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

static IServiceProvider? _Provider_;
/*
static IJSRuntime? _JSRuntime_;
static IJSInProcessRuntime? _JSInProcessRuntime_;
*/
static HttpClient? _HttpClient_;
static NavigationManager? _NavigationManager_;
static IAuthorizationService? _AuthorizationService_;
static HomeService? _HomeService_;
static UserAuthorizationService? _UserAuthorizationService_;
/*
static ...Service? _...Service_;
*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Services
//**************************************************************************************************

} // BookRegistry.Client
