//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Principal;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HEnvironment {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static string DomainUserName
{
   [MethodImpl(MethodImplOptions.NoInlining)]
   get
   {
      return Environment.UserDomainName+'\\'+Environment.UserName;
   }
}

#if WIN32
/// <exception cref="HOperationFailedException" />
public static ProcessorArchitecture ProcessorArchitecture
{
   get
   {
      return HPlatform.GetProcessorArchitecture( true );
   }
}
#endif // WIN32

#if LEGACY
/// <exception cref="SecurityException" />
static bool IsElevatedToAdmin
{
   [MethodImpl(MethodImplOptions.NoInlining)]
   get
   {
   // "WindowsIdentity.GetCurrent" method must return valid reference, if completed
   // without exception. (Null reference value is not a subject to warry here.)
      return new WindowsPrincipal( WindowsIdentity.GetCurrent() ).IsInRole(
         WindowsBuiltInRole.Administrator );
   }
}
#endif // LEGACY

//==================================================================================================
#if LEGACY

public static int TryDetermineIsElevatedToAdmin()
{
   try { return HBool.ToInt( IsElevatedToAdmin ); }
   catch (SecurityException) { return -1; }
}

public static bool TryDetermineIsElevatedToAdmin(out bool accessDenied)
{
   int code = TryDetermineIsElevatedToAdmin();
   if ( code < 0 )
   {
      accessDenied = true;
      return false;
   }
   accessDenied = false;
   return HBool.FromInt( code );
}

#endif // LEGACY
//==================================================================================================

/// <exception cref="InvalidOperationException" />
public static bool IsWASM
{
   get
   {
      int isWASM = _IsWASM_;
      if ( isWASM == -1 )
         throw new InvalidOperationException(
            "WASM-environment flag has not been specified." );
      return isWASM == 1;
   }
}
public static bool? IsWASMSpecification
{
   get { return HOps.IntToNullableBool( _IsWASM_ ); }
}

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

/// <exception cref="PlatformNotSupportedException" />
public static void SpecifyIsWASM()
{
   SpecifyIsWASM( true );
}
/// <exception cref="PlatformNotSupportedException" />
public static void SpecifyIsWASM(bool? value)
{
#if !WIN32
   if ( value == true )
      throw new PlatformNotSupportedException(
         "WASM-environment flag can not be specified in WIN32-build." );
#endif // !WIN32
#if WASM
   if ( value == null )
      throw new PlatformNotSupportedException(
         "WASM-environment flag can not be unspecified in WASM-build." );
   if ( value == false )
      throw new PlatformNotSupportedException(
         "WASM-environment flag can not be specified to false in WASM-build." );
#endif // WASM
   _IsWASM_ = HOps.NullableBoolToInt( value );
}

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

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

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

#if WASM
static volatile int _IsWASM_ = 1;
#else // !WASM:
static volatile int _IsWASM_ = -1;
#endif // !WASM

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HEnvironment
//**************************************************************************************************

} // CommonHelpers (.WASM)
