//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// AFXUI auxiliary module for Blazor-application (WASM) | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;

using CommonHelpers.WASM;

namespace AFXUI {

//**************************************************************************************************
static partial class UMagistral {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static bool Initialize()
{
   if ( IsInitialized )
      return false;
   UJS.Invoke( "Magistral_Initialize" );
   return IsInitialized = true;
}
public static bool Initialize(string dropDownMenuItemsHTML)
{
   bool rslt = Initialize();
   SetDropDownMenuHtml( dropDownMenuItemsHTML );
   return rslt;
}

public static int GetPageContentWidthDiff() =>
   UJS.Invoke<int>( "Magistral_GetPageContentWidthDiff" );

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

public static void SetDropDownMenuHtml(string itemsHTML) =>
   UJS.Invoke( "Magistral_SetDropDownMenuHtml", itemsHTML );

public static void SetPageName(string? verticalName) =>
   UJS.Invoke( "Magistral_SetPageName", verticalName );
public static void SetPageNameHTML(string? verticalNameHTML) =>
   UJS.Invoke( "Magistral_SetPageNameHTML", verticalNameHTML );

public static void SetDropdownIcon(string? iconClassName,string? iconPath) =>
   UJS.Invoke( "Magistral_SetDropdownIcon", iconClassName, iconPath );
public static void SetDropdownIconClass(string? iconClassName) =>
   UJS.Invoke( "Magistral_SetDropdownIconClass", iconClassName );
public static void SetDropdownIconPath(string? iconPath) =>
   UJS.Invoke( "Magistral_SetDropdownIconPath", iconPath );
public static void SetDropdownIconHtml(string? dwopDownIconHTML) =>
   UJS.Invoke( "Magistral_SetDropdownIconHtml", dwopDownIconHTML );

public static void SetPageNameDropdownIcon( string? verticalName,
                                            string? iconClassName, string? iconPath ) =>
   UJS.Invoke( "Magistral_SetPageNameDropdownIcon", verticalName, iconClassName, iconPath );
public static void SetPageNameIconClass(string? verticalName,string? iconClassName) =>
   UJS.Invoke( "Magistral_SetPageNameIconClass", verticalName, iconClassName );
public static void SetPageNameIconPath(string? verticalName,string? iconPath) =>
   UJS.Invoke( "Magistral_SetPageNameIconPath", verticalName, iconPath );

public static void ResetPageNameDropdownIcon() =>
   UJS.Invoke( "Magistral_ResetPageNameDropdownIcon" );

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

public static void LocationChanged() =>
   UJS.Invoke( "Magistral_LocationChanged" );

public static bool InitiateReloadPage() =>
   UJS.Invoke<bool>( "Magistral_InitiateReloadPage" );
public static bool DeferredInitiateReloadPage() =>
   DeferredInitiateReloadPage( 0 );
public static bool DeferredInitiateReloadPage(int milliseconds) =>
   UJS.Invoke<bool>( "Magistral_DeferredInitiateReloadPage", milliseconds );

public static void ClearSelectionUnscrollEtc() =>
   UJS.Invoke( "Magistral_ClearSelectionUnscrollEtc" );
public static void ClearSelectionDeferredUnscrollEtc() =>
   UJS.Invoke( "Magistral_ClearSelectionDeferredUnscrollEtc" );

public static void SpecifyPageContent(ElementReference selectionContentRef) =>
   UJS.Invoke( "Magistral_SpecifyPageContent", selectionContentRef );

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

public static void PushModalPopup( ComponentBase dialogComponent,
                                   ElementReference dialogContentRef )
{
   UJS.Invoke( "Magistral_SpecifyModalPopup", dialogContentRef );
   _ModalPopupStack_.Add( new ModalPopupRecord(
      CurrentPopup = dialogComponent, dialogContentRef ) );
   OnPushModalPopup?.Invoke();
   foreach (var callbackA in OnPushModalPopupA)
      callbackA.Invoke();
}

/// <exception cref="InvalidOperationException" />
public static void PopModalPopup(ComponentBase dialogComponent)
{
   int count = _ModalPopupStack_.Count;
   if (!( count > 0 && _ModalPopupStack_[count-1].DialogComponent == dialogComponent ))
      throw new InvalidOperationException(
         "Attempt of incorrect modal popup removal from stack." );
   _ModalPopupStack_.RemoveAt( --count );
   CurrentPopup = count > 0 ? _ModalPopupStack_[ count-1 ].DialogComponent : null;
   UJS.Invoke( "Magistral_SpecifyModalPopup",
      count > 0 ? (object?)_ModalPopupStack_[ count-1 ].DialogContentRef : null );
   OnPopModalPopup?.Invoke();
   foreach (var callbackA in OnPopModalPopupA)
      callbackA.Invoke();
}

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

/// <exception cref="InvalidOperationException" />
public static void VerifyNotHasModalPopup()
{
   if ( CurrentPopup != null )
      throw new InvalidOperationException(
         "There is unexpectedly open modal popup element." );
   if ( _ModalPopupStack_.Count > 0 )
      throw new InvalidOperationException(
         "There are unexpectedly open modal popup elements." );
}

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

public static string SetOperationFailure() =>
   SetOperationFailure( null );
public static string SetOperationFailure(string? errorMessage) =>
   UJS.Invoke<string>( "Magistral_SetOperationFailure",
      errorMessage!=null ? HString.ToWebText( errorMessage ) : null );
[HReturnsNull]
public static string? ResetOperationFailure()
{
   UJS.Invoke( "Magistral_ResetOperationFailure" );
   return null;
}

public static bool HasProblemIndication =>
   UJS.Invoke<bool>( "Magistral_HasProblemIndication" );
public static bool HasErrorIndication =>
   UJS.Invoke<bool>( "Magistral_HasErrorIndication" );
public static bool HasAbandomentIndication =>
   UJS.Invoke<bool>( "Magistral_HasAbandomentIndication" );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // UMagistral
//**************************************************************************************************

} // AFXUI
