using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;

using CommonHelpers.WASM;
using AFXUI;

using BookRegistry.Client.Shared;
using BookRegistry.Client.Pages;

namespace BookRegistry.Client {

//**************************************************************************************************
public partial class App: AComponent,IDisposable {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static NavigationManager NavManager => Services.NavigationManager;
//==================================================================================================
protected bool ExcludeAppMarkup;
//==================================================================================================

internal static string? QueryLanguage { get; private set; }
internal static AppLocale Locale { get; private set; }

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

public static bool RefreshMarkup()
{
   var instance = _Instance_;
   if ( instance == null )
      return false;
   instance.DoRefreshMarkup();
   return true;
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
[JSInvokable]
public static void wasm_Magistral_PerformNavigation(string href) =>
go_static_nt(MethodBase.GetCurrentMethod(),()=>{
   NAV.NavigateTo( HArgChecking.NN( href ) );
});

[JSInvokable]
public static Task wasm_Magistral_ReloadPage_A() =>
go_static_nt(MethodBase.GetCurrentMethod(),async()=>{
   ExclusiveLock.SetExplicit();
   ExclusiveLock.NeutralizeInstances();
   UInputBlocking.Block( true );
   UInputBlocking.NeutralizeInstances();
   SetEscapeRequested();
   await Exclusive.WaitOperation_A();
   APageComponent.ResetCurrentPage();
   Instance.DoRefreshMarkup();
});

[JSInvokable]
public static void wasm_Magistral_CancelPendingOperation() =>
go_static(MethodBase.GetCurrentMethod(),()=>{
   UCircuit.CancelExclusiveOperation();
});

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

protected override void OnInitialized() =>
go(()=>{
   if ( Program.IsDebugConfiguration )
      JS.Invoke( "App_SpecifyIsDebugConfiguration" );
   UMagistral.Initialize(HTextBuilder.CombineLines(
      HomeMI.HTML(),
      AuthorizationMI.HTML(),
      WeatherForecastMI.HTML(),
      AboutMI.HTML(),
      NavMenuItem.DividerHtml,
      ErrorMI.HTML()
   ));
   _Instance_ = this;
   NavManager.LocationChanged += LocationChanged;
   base.OnInitialized();
   UInputBlocking.Block( true );
   JS.Invoke( "App_RegisterEvents" );
//throw new ApplicationException("Qwertyuiop.");
});
/*
protected override Task OnInitializedAsync() =>
go(async()=>{
   await base.OnInitializedAsync();
});
*/
//==================================================================================================

/// <exception cref="InvalidOperationException" />
internal static App Instance
{
   get {
      var instance = _Instance_;
      if ( instance == null )
         throw new InvalidOperationException(
            "Instance of \"App\"-class has not been yet created." );
      return instance;
   }
}
/*
/// <exception cref="InvalidOperationException" />
internal static MessageBoxDialog MessageBoxDialogReset()
{
   var dlg = Instance.MessageBoxDialogRef;
   if ( dlg == null )
      throw new InvalidOperationException(
         "Instance of \"MessageBoxDialog\"-class has not been yet created." );
   dlg.ResetProperties();
   return dlg;
}
*/
//==================================================================================================

void IDisposable.Dispose() =>
go(()=>{
   NavManager.LocationChanged -= LocationChanged;
   _Instance_ = null;
});

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

void DoRefreshMarkup()
{
   ExcludeAppMarkup = true;
   StateHasChanged();
   ExcludeAppMarkup = false;
   StateHasChanged();
}

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

void LocationChanged(object? sender,LocationChangedEventArgs e) =>
go(()=>{
   DebugConsole.Print( e.Location );
   UMagistral.LocationChanged();
});

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

static App? _Instance_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // App
//**************************************************************************************************

} // BookRegistry.Client
