//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// 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

/// <summary>
/// Win32 error helpers
/// </summary>
[u.win32]
public static partial class w32_error {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static UInt32 LastErrorCode =>
   w32_core.GetLastError();

public static string LastErrorMessage =>
   ErrorCodeToMessage( LastErrorCode );

public static bool HasLastError =>
   LastErrorCode != 0;

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

public static string NormalizeErrorMessage( string message )
{
   message = message.TrimEnd();
   if ( message == "" )
      return "???";
   if ( !char.IsPunctuation( u.str.StrictLastChar( message ) ) )
      message += '.';
   return message;
}

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

public static string ErrorCodeToMessage( UInt32 errorCode ) =>
   ErrorCodeToMessageS( unchecked( (Int32)errorCode ) );

public static string ErrorCodeToMessageS( Int32 errorCode )
{
   try { throw new Win32Exception( errorCode ); }
   catch (Win32Exception e) {
      return NormalizeErrorMessage( e.Message ); }
}

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

public static string FuncFailedErrorMessage( string functionName ) =>
   FuncFailedErrorMessage( functionName, null );
public static string FuncFailedErrorMessage( string functionName, string? remarkMessage ) =>
   u.str.FMT(
      "Win32-function “{0}” has failed.{1}", functionName,
      remarkMessage!=null ? $"\n({remarkMessage})" : "" );

public static string FuncFailedErrorMessage( UInt32 errorCode, string functionName ) =>
   FuncFailedErrorMessage( functionName, null );
public static string FuncFailedErrorMessage( UInt32 errorCode, string functionName,
                                             string? remarkMessage ) =>
   u.str.FMT(
      "Win32-function “{0}” has failed.\n"+
      "{1}{2}", functionName, ErrorCodeToMessage( errorCode ),
      remarkMessage!=null ? $"\n({remarkMessage})" : "" );

/// <summary>
/// Signed version of system error message
/// </summary>
public static string FuncFailedErrorMessageS( Int32 errorCode, string functionName ) =>
   FuncFailedErrorMessage( unchecked((uint)errorCode), functionName, null );
/// <summary>
/// Signed version of system error message
/// </summary>
public static string FuncFailedErrorMessageS( Int32 errorCode, string functionName,
                                              string? remarkMessage ) =>
   FuncFailedErrorMessage( unchecked((uint)errorCode), functionName, remarkMessage );

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

public static string FuncFailedErrorMessage_WithResultCodeToken( string functionName,
                                                                 string resultCodeToken ) =>
   FuncFailedErrorMessage( functionName,
      $"Win32 result code = {resultCodeToken}." );

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

} // user_rtx (.wasm)
