//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// AFXUI auxiliary module for Blazor-application (WASM) | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Text;

using CommonHelpers.WASM;

namespace AFXUI {

//**************************************************************************************************
static partial class UBreakdown {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static int TabSubstSpaceCount { get; set; } = 0;

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

static string PrepareExceptionHtml(Exception e)
{
   var sb = new StringBuilder();
   bool isChained = e.InnerException != null;
   if ( isChained )
      sb.AppendLine( "<ui>" );
   for (Exception? x=e; x!=null; x=x.InnerException)
   {
      if ( x != e )
      {
         sb.AppendLine();
         if ( UProgram.IsDebugConfiguration )
            sb.AppendLine( "<br>" );
      }
      GetExceptionItemHtml( sb, x, isChained );
   }
   if ( isChained )
      sb.AppendLine( "</ui>" );
   return sb.ToString();
}

static void GetExceptionItemHtml(StringBuilder sb,Exception e,bool isChained)
{
   string? stackTraceStr = UProgram.IsDebugConfiguration ? e.StackTrace : null;
   bool withStackInfo = stackTraceStr != null;
   if ( isChained )
      sb.Append( "<li>" );
   sb.Append( "<font style=\"color: Red\">" );
   if ( withStackInfo )
      sb.Append( "<u><b>" );
   sb.Append( e.GetType().ToString() );
   sb.Append( ":&nbsp;" );
   sb.Append( e.Message );
   sb.Append( "</font>" );
   if ( withStackInfo )
      sb.Append( "</b></u>" );
   if ( stackTraceStr != null )
   {
      sb.AppendLine();
      sb.AppendLine( "<br>" );
      sb.Append( StackTraceToMarkup( stackTraceStr ) );
   }
   sb.AppendLine();
   if ( isChained )
      sb.AppendLine( "</li>" );
}

static string StackTraceToMarkup(string stackTraceStr)
{
   var sb = new StringBuilder();
   foreach (char ch in stackTraceStr)
      sb.Append( UMarkup.CharToStr( ch ) );
   string tabSubst = HString.Repeat( "&nbsp;", TabSubstSpaceCount );
   return tabSubst + sb.ToString().Replace( Environment.NewLine,
      Environment.NewLine+"<br>"+Environment.NewLine+tabSubst );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // UBreakdown
//**************************************************************************************************

} // AFXUI
