using System;
using System.Text;

using CommonHelpers;

namespace BookRegistry.Server {

//**************************************************************************************************
/// <summary>
/// Message helpers (error text for web-client etc.)
/// </summary>
public static class MSG {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static string ServerErrorMessage_DbAccessError =>
   HLocal.TEXT(
      "Server error has occured during request performance.",
      "Произошла ошибка сервера при исполнении запроса." );
public static string ServerErrorMessageWithColumn_DbAccessError =>
   HLocal.TEXT(
      "Server error during request performance (debug info):",
      "Ошибка сервера при исполнении запроса (информация отладки):" );

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

public static string ServerError(string errorMessage) =>
   Program.DevelopmentBehaviour ?
      ServerErrorMessageWithColumn_DbAccessError+"\n"+
      HString.ToWebText( errorMessage ) :
      ServerErrorMessage_DbAccessError;

public static string ServerError(Exception e) =>
   Program.DevelopmentBehaviour ?
      ServerErrorMessageWithColumn_DbAccessError+"\n"+
      HString.ToWebText( HException.GetErrorMessage( e ) ) :
      ServerErrorMessage_DbAccessError;

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

public static string OperationError(string errorMessage,string? operationName=null)
{
   if ( Program.DevelopmentBehaviour )
      return HString.ConvertAsText( HString.ToWebText( errorMessage ) );
   return HLocal.TEXT(
      "Operation has failed.",
      "Операция потерпела неудачу." ) +
      ( operationName != null ?
        HString.FormatAsText(
            "\n({0}: \"{1}\".)",
            HLocal.TEXT( "Operation", "Операция" ),
            operationName ) :
        "" );
}
public static string OperationError(Exception e,string? operationName=null) =>
   OperationError( HString.ConvertAsText( e.Message ), operationName );

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

public static string ToHTMLBreakedLines(string text)
{
   text = HString.ToWebText( text );
   var sb = new StringBuilder();
   char ch = '\0';
   for (int i=0; i<text.Length; i++)
   {
      char prevCh = ch;
      ch = text[ i ];
      if ( !HOps.IsOneOf( ch, '\r', '\n' ) )
         sb.Append(ch);
      else
         if (!( ch=='\n' && prevCh=='\r' ))
            sb.Append( "<br>" );
   }
   return sb.ToString();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // MSG
//**************************************************************************************************

} // BookRegistry.Server
