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

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

static partial class fatal_impl {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static string MakeReportText( u.FatalAttributes attr,
                                     u.FatalErrorArgs parameters,
                                     bool withStackTrace = false,
                                     bool separateWithNL = false,
                                     bool consoleFormatting = false )
{
   var sb = new StringBuilder();
   void SeparateWithNL() {
      if ( separateWithNL )
         AppendLine( sb );
   }

   AppendLine( sb, FMT(
      "___ Fatal error in {1}{0}{2}: ___",
      attr.MainModuleName,
      consoleFormatting ? '“' : '«',
      consoleFormatting ? '”' : '»' ));
   SeparateWithNL();

   AppendLine( sb, parameters.CauseOrCategory );
   SeparateWithNL();

   if ( parameters.ErrorMessage != null ) {
      AppendLine( sb, TXT( parameters.ErrorMessage ) );
      SeparateWithNL();
   }

   var e = parameters.Exception;
   if ( e != null ) {
      for ( var ex=e; ex!=null; ex=GetFlattenedInnerException( ex ) ) {
         AppendLine( sb );
         if ( e.InnerException != null )
            sb.Append( consoleFormatting ? "* " : "\u2022 " );
         sb.Append( ex.GetType() );
         sb.Append( ": " );
         sb.Append( AsSingleLine( ex.Message ) );
         if ( withStackTrace ) {
            var stackTrace = ex.StackTrace;
            if ( stackTrace != null )
               AppendLine( sb, TXT( stackTrace ) );
         }
      }
      SeparateWithNL();
   }

   if ( withStackTrace && e == null ) {
      if ( parameters.StackTrace != null ) {
         AppendLine( sb, "=== ERROR STACK: ===" );
         AppendLine( sb, StackTraceToText( parameters.StackTrace ) );
         SeparateWithNL();
      }
      if ( parameters.DispatcherCallerStack != null ) {
         AppendLine( sb, "=== DISPATCHER CALLER STACK: ===" );
         AppendLine( sb, StackTraceToText( parameters.DispatcherCallerStack ) );
         SeparateWithNL();
      }
   }

   AppendLine( sb, $"* Source assembly (raise-call): {parameters.SourceAssembly};" );
   if ( attr.ProcessId.HasValue )
      AppendLine( sb, $"* Process ID: {attr.ProcessId};" );
   AppendLine( sb, $"* Managed thread ID: {attr.ManagedThreadId}" );
   if ( attr.IsInMainThread )
      sb.Append( " (main)" );
   else
      if ( attr.IsInSecondaryThread )
         sb.Append( " (secondary)" );
   sb.Append( "." );

   attr.ReportText = sb.ToString();
   if ( attr.ReportFilePath != null )
      AppendLine( sb, $"(Report-file: “{attr.ReportFilePath}”.)" );

   return sb.ToString();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // fatal_impl

} // user_rtx (.wasm)
