//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// UserRTX.NET satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.ComponentModel;

#if !WASM
namespace user_rtx { using u = user_rtx;
#else
namespace user_rtx.wasm { using u = user_rtx.wasm;
#endif

static partial class w32_error {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static ESystemError SystemError( UInt32 errorCode ) =>
   new ESystemError( SystemErrorKind.Win32, errorCode, ErrorCodeToMessage( errorCode ) );

public static ESystemError SystemError( UInt32 errorCode, string message ) =>
   new ESystemError( SystemErrorKind.Win32, errorCode, message );

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

/// <summary>
/// Signed version of system error creation
/// </summary>
public static ESystemError SystemErrorS( Int32 errorCode ) =>
   new ESystemError( SystemErrorKind.Win32,
      unchecked( (UInt64)errorCode ), ErrorCodeToMessageS( errorCode ) );

/// <summary>
/// Signed version of system error creation
/// </summary>
public static ESystemError SystemErrorS( Int32 errorCode, string message ) =>
   new ESystemError( SystemErrorKind.Win32,
      unchecked( (UInt64)errorCode ), NormalizeErrorMessage( message ) );

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

public static ESystemError SystemError( Win32Exception error ) =>
   new ESystemError( SystemErrorKind.Win32,
      unchecked( (UInt64)error.NativeErrorCode ), error.Message );

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

public static ESystemError LastError() =>
   new ESystemError( SystemErrorKind.Win32, LastErrorCode, LastErrorMessage );

public static ESystemError LastError( string message ) =>
   new ESystemError( SystemErrorKind.Win32, LastErrorCode,
      u.str.FMT( "{0}\n{1}", message, LastErrorMessage ) );
public static ESystemError LastError( string messageFmt, params object?[] args ) =>
   LastError( u.str.FMT( messageFmt, args ) );

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

/// <exception cref="ArgumentNullException" />
public static ESystemError FunctionFailed( string functionName ) =>
   FunctionFailed( functionName, null );
/// <exception cref="ArgumentNullException" />
public static ESystemError FunctionFailed( string functionName, string? remarkMessage ) =>
   new ESystemError( FuncFailedErrorMessage( functionName, remarkMessage ) );

/// <exception cref="ArgumentNullException" />
public static ESystemError FunctionFailed( UInt32 errorCode, string functionName ) =>
   FunctionFailed( errorCode, functionName, null );
/// <exception cref="ArgumentNullException" />
public static ESystemError FunctionFailed( UInt32 errorCode,
                                           string functionName, string? remarkMessage ) =>
   new ESystemError( FuncFailedErrorMessage( errorCode, functionName, remarkMessage ) );

/// <summary>
/// Signed version of system error creation
/// </summary>
/// <exception cref="ArgumentNullException" />
public static ESystemError FunctionFailedS( Int32 errorCode, string functionName ) =>
   FunctionFailedS( errorCode, functionName, null );
/// <summary>
/// Signed version of system error creation
/// </summary>
/// <exception cref="ArgumentNullException" />
public static ESystemError FunctionFailedS( Int32 errorCode,
                                            string functionName, string? remarkMessage ) =>
   new ESystemError( FuncFailedErrorMessageS( errorCode, functionName, remarkMessage ) );

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

/// <exception cref="ArgumentNullException" />
public static ESystemError FunctionFailed_WithResultCodeToken(
   string functionName, string resultCodeToken ) =>
      FunctionFailed( FuncFailedErrorMessage_WithResultCodeToken(
         functionName, resultCodeToken ) );

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

[u.returns_true]
public static bool ClearLastError()
{
   w32_core.SetLastError( 0 );
   return true;
}

[u.returns_false]
public static bool SetLastError( UInt32 errorCode )
{
   w32_core.SetLastError( errorCode );
   return false;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // w32_error

} // user_rtx (.wasm)
