using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
using Microsoft.AspNetCore.Components;

using CommonHelpers.WASM;
using AFXUI;

namespace BookRegistry.Client {

//**************************************************************************************************
static class Exclusive {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static bool HasOperation => UCircuit.HasExclusiveOperation;

/// <exception cref="InvalidOperationException" />
public static bool WasAbandonedOrCancelled => UCircuit.WasXOpAbandonedOrCancelled;

public static bool HasExplicitLock =>
   UCircuit.HasExplicitExclusiveLock;

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

/// <exception cref="ArgumentOutOfRangeException" />
public static bool HasOperationAt(int level) =>
   UCircuit.HasExclusiveOperationAt( level );

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

/// <exception cref="InvalidOperationException" />
public static void SetCancellation(CancellationTokenSource source) =>
   UCircuit.SetExclusiveCancellation( source );

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

public static bool CancelOperation() =>
   UCircuit.CancelExclusiveOperation();

public static async Task<bool> WaitOperation_A()
{
   await UCircuit.WaitExclusiveOperation_A();
   return !UCircuit.IsEscapeRequested;
}

/// <exception cref="UEscapeException" />
public static async Task ESC_WaitOperation_A()
{
   await UCircuit.WaitExclusiveOperation_A();
   UCircuit.ESC_Check( typeof(Exclusive) );
}

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

/// <exception cref="ArgumentOutOfRangeException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="UEscapeException" />
public static Task ESC_WaitInterception( int level, Type methodType,
                                         [CallerMemberName][HRequired]
                                         string? methodNameOrSignature=null ) =>
   UCircuit.ESC_WaitExclusiveInterception( level, methodType.Name, methodNameOrSignature );

/// <exception cref="ArgumentOutOfRangeException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="UEscapeException" />
public static Task ESC_WaitInterception( int level, string methodTypeName,
                                         [CallerMemberName][HRequired]
                                         string? methodNameOrSignature=null ) =>
   UCircuit.ESC_WaitExclusiveInterception( level, methodTypeName, methodNameOrSignature );

/// <exception cref="ArgumentOutOfRangeException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="UEscapeException" />
public static Task ESC_WaitInterception( int level, MethodBase method ) =>
   UCircuit.ESC_WaitExclusiveInterception( level, method );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Exclusive
//**************************************************************************************************

} // BookRegistry.Client
