//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if WIN32
using System;
using System.ComponentModel;
#if LEGACY
using System.Security.Permissions;
#endif // LEGACY

namespace CommonHelpers {

//**************************************************************************************************
/// <summary>
/// </summary>
#if LEGACY
[PermissionSet(SecurityAction.Demand,Unrestricted=true)]
#endif // LEGACY
public static partial class HWin32Error {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[CLSCompliant(false)]
public static uint LastErrorCode =>
   Win32_Core.GetLastError();

public static string LastErrorMessage =>
   ErrorCodeToMessage( LastErrorCode );

public static bool HasLastError =>
   LastErrorCode != 0;

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

/// <exception cref="ArgumentNullException" />
public static string NormalizeErrorMessage(string message)
{
   message = HArgChecking.NN( message ).TrimEnd();
   if ( message == "" )
      return "???";
   if ( !char.IsPunctuation( HString.LastChar( message ) ) )
      message += '.';
   return message;
}

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

[CLSCompliant(false)]
public static string ErrorCodeToMessage(uint errorCode) =>
   ErrorCodeToMessage( unchecked( (int)errorCode ) );

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

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

/// <exception cref="ArgumentNullException" />
public static string ComposeOpFailedErrorMessage(string functionName) =>
   ComposeOpFailedErrorMessage( functionName, null );

/// <exception cref="ArgumentNullException" />
public static string ComposeOpFailedErrorMessage( string functionName,
                                                  [HOptional] string remarkMessage )
{
   HArgChecking.VerifyNotNull( functionName );
   return HString.FormatAsText(
      "Win32-function \"{0}\" has failed.{1}", functionName,
      remarkMessage!=null ? string.Format("\n(0)",remarkMessage) : "" );
}

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

/// <exception cref="ArgumentNullException" />
public static string ComposeOpFailedErrorMessageWithResultCodeToken( string functionName,
                                                                     string resultCodeToken )
{
   HArgChecking.VerifyNotNulls( functionName, resultCodeToken );
   return ComposeOpFailedErrorMessage( functionName,
      string.Format("Win32 result code = {0}.",resultCodeToken) );
}

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

static HWin32Error()
{
   CommonHelpers_Core.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HWin32Error
//**************************************************************************************************

} // CommonHelpers
#endif // WIN32
