//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// AFXUI auxiliary module for Blazor-application (WASM) | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Net.Http;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;

using CommonHelpers.WASM;

namespace AFXUI {

//**************************************************************************************************
public static class ServicesAFX {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// <exception cref="InvalidOperationException" />
[HStrictResult]
public static IServiceProvider Provider =>
   _Provider_ ?? throw new InvalidOperationException(
      "Service provider for AFXUI-module 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 ...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 ...Service? _...Service_;
*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // ServicesAFX
//**************************************************************************************************

} // AFXUI
