using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;

using CommonHelpers.WASM;
using AFXUI;

namespace BookRegistry.Client.Shared {

//**************************************************************************************************
partial class MessageBox {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public Task ESC_ShowMessage_A(string message) =>
   ESC_ShowMessage_A( null, message );
public Task ESC_ShowMessage_A(string? title,string message) =>
   ESC_ShowDialog_A(MBParamsOk( title ?? "Information", message, ImagePath_Information ));

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

public Task ESC_ShowExclamation_A(string message) =>
   ESC_ShowExclamation_A( null, message );
public Task ESC_ShowExclamation_A(string? title,string message) =>
   ESC_ShowDialog_A(MBParamsOk( title ?? "Attention", message, ImagePath_Exclamation ));

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

public Task ESC_ShowCompletion_A(string message) =>
   ESC_ShowCompletion_A( null, message );
public Task ESC_ShowCompletion_A(string? title,string message) =>
   ESC_ShowDialog_A(MBParamsOk( title ?? "Completion", message, ImagePath_Completion ));

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

public Task ESC_ShowSuccess_A(string message) =>
   ESC_ShowSuccess_A( null, message );
public Task ESC_ShowSuccess_A(string? title,string message) =>
   ESC_ShowDialog_A(MBParamsOk( title ?? "Success", message, ImagePath_Success ));

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

public Task ESC_ShowError_A(string message) =>
   ESC_ShowError_A( null, message );
public Task ESC_ShowError_A(string? title,string message) =>
   ESC_ShowDialog_A(MBParamsOk( title ?? "Error", message, ImagePath_Error ));

public Task ESC_ShowError_A(Exception e) =>
   ESC_ShowError_A( e );
public Task ESC_ShowError_A(string? title,Exception e) =>
   ESC_ShowError_A( title, HException.GetMessage(e) );

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

public Task ESC_ShowCommunicationError_A(string message) =>
   ESC_ShowError_A( "Communication error", message );
public Task ESC_ShowCommunicationError_A(Exception error) =>
   ESC_ShowCommunicationError_A( HException.GetErrorMessage( error ) );

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

public Task ESC_ShowServerError_A(string message) =>
   ESC_ShowError_A( "Server error", message );

public Task ESC_ShowResponseError_A(GeneralApiResponse response)
{
   string message = response.ErrorMessage;
   return response.IsServerError ?
      ESC_ShowServerError_A( message ) :
      ESC_ShowError_A( message );
}

public Task ESC_ShowRequestError_A(SimpleSIR simpleResult)
{
   if ( simpleResult.Exception != null )
      return ESC_ShowCommunicationError_A( simpleResult.Exception );
   if ( simpleResult.Response != null )
      return ESC_ShowResponseError_A( simpleResult.Response );
   throw HAssertionFailure.DEAD_BRANCH();
}

public Task ESC_ShowRequestError_Unexpected_A() =>
   ESC_ShowRequestError_Unexpected_A( null );
public Task ESC_ShowRequestError_Unexpected_A(string? additionalMessage) =>
   ESC_ShowError_A( UnexpectedDataOrResult_Msg( additionalMessage ) );

public static string UnexpectedDataOrResult_Msg(string? additionalMessage=null) =>
   HString.FormatAsText(
      "The query has returned unexpected data/result.{0}",
      additionalMessage!=null ? "\n"+additionalMessage : "" );

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

public Task<bool> ESC_ConfirmationDialog_A( string text, bool confirmationFocusing = true ) =>
   ESC_ConfirmationDialog_A( null, null, text, confirmationFocusing, null, null );
public Task<bool> ESC_ConfirmationDialog_A( string? title, string text,
                                            bool confirmationFocusing = true ) =>
   ESC_ConfirmationDialog_A( null, title, text, confirmationFocusing, null, null );

public Task<bool> ESC_ConfirmationDialog_A( MessageBoxImage imageType,
                                            string? title, string text,
                                            bool confirmationFocusing = true,
                                            string? confirmButtonCaption = null,
                                            string? cancelButtonCaption = null ) =>
   ESC_ConfirmationDialog_A( GetImagePath( imageType ), title, text, confirmationFocusing,
                             confirmButtonCaption, cancelButtonCaption );
public async Task<bool> ESC_ConfirmationDialog_A( string? imagePath, string? title, string text,
                                                  bool confirmationFocusing = true,
                                                  string? confirmButtonCaption = null,
                                                  string? cancelButtonCaption = null )
{
   var mbp = new MessageBoxParams(){
      ImagePath = imagePath ?? ImagePath_Confirmation,
      Title = title ?? "Confirmation",
      Text = text,
      AcceptButtonCaption = confirmButtonCaption ?? "CONFIRM",
      CloseButtonCaption = cancelButtonCaption ?? "CANCEL",
      FocusAcceptButton = confirmationFocusing,
      FocusCloseButton = !confirmationFocusing
   };
   return Accepted(await ESC_ShowDialog_A( mbp ));
}

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

public Task<bool> ESC_ImportantConfirmationDialog_A( string text ) =>
   ESC_ImportantConfirmationDialog_A( false, null, text, null, null );
public Task<bool> ESC_ImportantConfirmationDialog_A( string? title, string text ) =>
   ESC_ImportantConfirmationDialog_A( false, title, text, null, null );
public Task<bool> ESC_ImportantConfirmationDialog_A( string? title, string text,
                                                     string? confirmButtonCaption = null,
                                                     string? cancelButtonCaption = null ) =>
   ESC_ImportantConfirmationDialog_A( false, title, text,
                                      confirmButtonCaption, cancelButtonCaption );

public Task<bool> ESC_ImportantConfirmationDialog_A( bool confirmationFocusing,
                                                     string text ) =>
   ESC_ImportantConfirmationDialog_A( confirmationFocusing, null, text, null, null );
public Task<bool> ESC_ImportantConfirmationDialog_A( bool confirmationFocusing,
                                                     string? title, string text ) =>
   ESC_ImportantConfirmationDialog_A( confirmationFocusing, title, text, null, null );
public Task<bool> ESC_ImportantConfirmationDialog_A( bool confirmationFocusing,
                                                     string? title, string text,
                                                     string? confirmButtonCaption = null,
                                                     string? cancelButtonCaption = null ) =>
   ESC_ConfirmationDialog_A( ImagePath_Exclamation, title, text, confirmationFocusing,
                             confirmButtonCaption, cancelButtonCaption );

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

public Task<bool> ESC_QuestionDialog_A( string text ) =>
   ESC_QuestionDialog_A( null, null, text );
public Task<bool> ESC_QuestionDialog_A( string? title, string text ) =>
   ESC_QuestionDialog_A( null, title, text );

public Task<bool> ESC_QuestionDialog_A( MessageBoxImage imageType,
                                        string? title, string text,
                                        string? yesButtonCaption = null,
                                        string? noButtonCaption = null ) =>
   ESC_QuestionDialog_A( GetImagePath( imageType ), title, text,
                         yesButtonCaption, noButtonCaption );
public async Task<bool> ESC_QuestionDialog_A( string? imagePath, string? title, string text,
                                              string? yesButtonCaption = null,
                                              string? noButtonCaption = null )
{
   var mbp = new MessageBoxParams( false ){
      ImagePath = imagePath ?? ImagePath_Question,
      Title = title ?? "Question",
      Text = text,
      AcceptButtonCaption = yesButtonCaption ?? "YES",
      CloseButtonCaption = noButtonCaption ?? "NO"
   };
   return Accepted(await ESC_ShowDialog_A( mbp ));
}

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

public static MessageBoxParams MBParamsOk(string title,string message,string imagePath) =>
   new MessageBoxParams(){
      Title = title,
      Text = message,
      ImagePath = imagePath,
      AcceptButtonCaption = "OK",
      FocusAcceptButton = true };

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

public static string ImagePathFromFileName(string fileName) =>
   $"images/message_box/{fileName}";

public static string GetImagePath(MessageBoxImage imageType) =>
   ImagePathFromFileName( GetImageFileName( imageType ) );

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

public static string GetImageFileName(MessageBoxImage imageType)
{
   switch ( imageType )
   {
   case MessageBoxImage.Information : return "information.png";
   case MessageBoxImage.Exclamation : return "exclamation.png";
   case MessageBoxImage.Error       : return "error.png";
   case MessageBoxImage.Success     : return "success.png";
   case MessageBoxImage.Completion  : return "completion.png";
   case MessageBoxImage.Confirmation: return "confirmation.png";
   case MessageBoxImage.Question    : return "question.png";
   default: throw HAssertionFailure.DEAD_BRANCH();
   } // switch
}

public static string ImagePath_Information =>
   GetImagePath( MessageBoxImage.Information );
public static string ImagePath_Exclamation =>
   GetImagePath( MessageBoxImage.Exclamation );
public static string ImagePath_Error =>
   GetImagePath( MessageBoxImage.Error );
public static string ImagePath_Success =>
   GetImagePath( MessageBoxImage.Success );
public static string ImagePath_Completion =>
   GetImagePath( MessageBoxImage.Completion );
public static string ImagePath_Confirmation =>
   GetImagePath( MessageBoxImage.Confirmation );
public static string ImagePath_Question =>
   GetImagePath( MessageBoxImage.Question );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // MessageBox
//**************************************************************************************************

} // BookRegistry.Client.Shared
