using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;

using CommonHelpers.WASM;
using AFXUI;
using BookRegistry.Client.DB;

namespace BookRegistry.Client.Shared {

//**************************************************************************************************
public partial class MainLayout: ALayoutComponent,IDisposable {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static ServiceUser_Info USR => Session.UserInfo;
//MarkupString VR => (MarkupString)"<span class=\"faint_text\">|<span>";
//==================================================================================================
protected ElementReference PageContentDIV;
protected MessageBox? MessageBoxInstance;
protected ModalDialogFrame? ModalDialogInstance;
//==================================================================================================

string ToHomePageHRCaption => "To home-page";
MarkupString ToHomePageHRCaption_Markup => (MarkupString)"To <u><b>h</b></u>ome-page";
string ToHomePageHRTile => "Go to home-page";
string ToHomePageAccessKey => "h";

string ToAuthorizationHRCaption => "To authorization";
MarkupString ToAuthorizationHRCaption_Markup => (MarkupString)"To <u><b>a</b></u>uthorization";
string ToAuthorizationHRTile => "Go to authorization page";
string ToAuthorizationAccessKey => "a";

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

/// <exception cref="InvalidOperationException" />
public static MainLayout Instance
{
   get {
      var instance = _Instance_;
      if ( instance == null )
         throw new InvalidOperationException(
            "Instance of \"MainLayout\"-class has not yet been created." );
      return instance;
   }
}

/// <exception cref="InvalidOperationException" />
public static MessageBox MessageBox
{
   get {
      var messageBox = Instance.MessageBoxInstance;
      if ( messageBox == null )
         throw new InvalidOperationException(
            "Instance of \"MessageBox\"-class is unavailable (\"MainLayout\")." );
      return messageBox;
   }
}

/// <exception cref="InvalidOperationException" />
public static ModalDialogFrame ModalDialog
{
   get {
      var modalDialog = Instance.ModalDialogInstance;
      if ( modalDialog == null )
         throw new InvalidOperationException(
            "Instance of \"ModalDialogFrame\"-class is unavailable (\"MainLayout\")." );
      return modalDialog;
   }
}

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

public static bool Update()
{
   if ( _Instance_ == null )
      return false;
   //_AdjustLayout_ = true;
   _Instance_.StateHasChanged();
   return true;
}

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

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

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

protected override void OnInitialized() =>
go(()=>{
   base.OnInitialized();
   _Instance_ = this;
});

protected override void On_Load() =>
go(()=>{
   UMagistral.SpecifyPageContent( PageContentDIV );
});
/*
protected override void OnAfterRender(bool firstRender) =>
go_nt(()=>{
   base.OnAfterRender( firstRender );
   if ( HOps.DefaultExchange(ref _AdjustLayout_) )
      JS.Invoke( "MainLayout_Adjust" );
});
*/
//==================================================================================================

// Not-busy only navigation (exclusive method):
void ToHomePageHR_OnClick() =>
GO(()=>{
   NAV.NavigateTo( "/" );
});
// Not-busy only navigation (exclusive method):
void ToAuthorizationHR_OnClick() =>
GO(()=>{
   NAV.NavigateTo( "/authorization" );
});

// Not-busy only click (exclusive method):
void CloseSessionBUTTON_OnClick() =>
GO(()=>{
   Session.Close();
   UMagistral.InitiateReloadPage();
});

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

static MainLayout? _Instance_;
//static bool _AdjustLayout_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // MainLayout
//**************************************************************************************************

} // BookRegistry.Client.Shared
