//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// AFXUI auxiliary module for Blazor-application (WASM) | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;

using CommonHelpers.WASM;

namespace AFXUI {

//**************************************************************************************************
/// <summary>
/// Application busy appearance and blocking for query operation (not for long time period)
/// </summary>
public static class UBusyState {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static bool IsActive { get; private set; }
public static string? OperationText { get; private set; }

public static Func<string,string>? OperationTextDecorator { get; set; }

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

/// <exception cref="InvalidOperationException" />
public static void Activate(string operationText,bool allowOpCancellation=true)
{
   UCircuit.VerifyExclusiveActive();
   UJS.Invoke<string>( "BusyState_Activate",
      DecorateOperationText( operationText ),
      allowOpCancellation );
   OperationText = operationText;
   IsActive = true;
   UApplication.PageStateHasChanged();
}

/// <exception cref="InvalidOperationException" />
public static void SetOperationText(string operationText,bool allowOpCancellation=true)
{
   UCircuit.VerifyExclusiveActive();
   UJS.Invoke( "BusyState_SetOperationText",
      DecorateOperationText( operationText ), allowOpCancellation );
}
/// <exception cref="InvalidOperationException" />
public static void NotifyOpAbandoned(bool deactivate=true)
{
   UCircuit.VerifyExclusiveActive();
   UJS.Invoke( "BusyState_NotifyOpAbandoned", deactivate );
   if ( IsActive )
      DeactivateComplete();
}

/// <exception cref="InvalidOperationException" />
public static bool Deactivate()
{
   UCircuit.VerifyExclusiveActive();
   if ( !IsActive )
      return false;
   UJS.Invoke( "BusyState_Deactivate" );
   DeactivateComplete();
   return true;
}

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

/// <exception cref="InvalidOperationException" />
public static void VerifyActive()
{
   if ( !IsActive )
      throw new ArgumentException( "Busy appearance is unexpectedly not in active state." );
}
/// <exception cref="InvalidOperationException" />
public static void VerifyNotActive()
{
   if ( IsActive )
      throw new ArgumentException( "Busy appearance is unexpectedly in active state." );
}

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

public static string DecorateOperationText(string baseOperationText)
{
   baseOperationText = HString.ToWebText( baseOperationText );
   var decorator = OperationTextDecorator;
   return decorator != null ?
      decorator( baseOperationText ) :
      baseOperationText+" …";
}

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

static void DeactivateComplete()
{
   OperationText = null;
   IsActive = false;
   UApplication.PageStateHasChanged();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // UBusyState
//**************************************************************************************************

} // AFXUI
