//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.IO;
using System.Resources;
using System.Reflection;

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

//**************************************************************************************************
/// <summary>
/// </summary>
public static class HCommonHlpResources {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static ResourceManager
   ResourceManager => _ResourceManager_;

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

/// <exception cref="ArgumentNullException" />
public static string TryGetString(MethodBase getMethod)
{
   _IsInTryGetString_ = true;
   try
   {
      return DoTryGetString( getMethod );
   }
   catch (Exception e)
   {
      try
      {
         DetailedPrintError(
            string.Format(
               "CommonHelpers string resource access ( \"{0}\" )",
               getMethod ),
            e.ToString() );
      }
      catch
      {}
      return null;
   }
   finally
   {
      _IsInTryGetString_ = false;
   }
}

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

static HCommonHlpResources()
{
   CommonHelpers_Core.Access();
   Assembly rcAssembly;
   try
   {
      rcAssembly = Assembly.Load(string.Format(
         "CommonHelpers.Resources, Version={0}, Culture=neutral, PublicKeyToken={1}",
         CommonHelpers_Info.Version, CommonHelpers_Info.PublicKeyToken ));
   }
   catch (Exception e)
   {
      if (e is FileNotFoundException)
         return;
      throw;
   }
   _ResourceManager_ = new ResourceManager( "CommonHelpers.Resources", rcAssembly );
}

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

static void Access()
{}

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

static string DoTryGetString(MethodBase getMethod)
{
   HArgChecking.VerifyNotNull( getMethod );
   if ( ResourceManager==null || HLanguage.IsInCodeSupported )
      return null;

   string typeName = getMethod.DeclaringType.ToString();
   int dotIndex =  typeName.IndexOf( '.' );
   if (!(
      HOps.IsExBetween( dotIndex, 0, typeName.Length-1 ) &&
      typeName.Substring( 0, dotIndex ) == "CommonHelpers" ))
   {
      DetailedPrintError(
         "CommonHelpers string resource access",
         "Incorrect type name for the resource: \"{0}\".",
         typeName );
      return null;
   }

   string resourceName = string.Format(
      "{0}:{1}", typeName.Substring( dotIndex+1 ),
      HPropertyInfo.ExtractNameByGetMethod( getMethod ) );
   try
   {
      string str = ResourceManager.GetString( resourceName );
      if ( str == null )
         DetailedPrintError(
            "CommonHelpers string resource extraction",
            "String name was not found: {0}.",
            resourceName );
      return str;
   }
   catch (Exception e)
   {
      DetailedPrintError(
         string.Format(
            "CommonHelpers string resource extraction ( \"{0}\" )",
            resourceName ),
         HException.ComposeChainedMessage( e ) );
      return null;
   }
}

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

static void DetailedPrintError(string caption,string details)
{
   if ( !_IsInTryGetString_ )
      HDebug.DetailedPrintError( caption, details );
}

static void DetailedPrintError(string caption,string detailsFmt,params object[] parameters)
{
   if ( !_IsInTryGetString_ )
      HDebug.DetailedPrintError( caption, detailsFmt, parameters );
}

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

static readonly ResourceManager
   _ResourceManager_;

[ThreadStatic]
static bool
   _IsInTryGetString_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HCommonHlpResources
//**************************************************************************************************

} // CommonHelpers (.WASM)
