using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

using CommonHelpers.WASM;
using AFXUI;

using BookRegistry.Client.Shared;
using BookRegistry.Client.Pages;
using BookRegistry.Client.DB;

namespace BookRegistry.Client.Components {

//**************************************************************************************************
/// <summary>
/// BookInfo-subpage component (in HomePage)
/// </summary>
public partial class BookInfo: AComponent {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static HomeService SRV => Services.HomeService;
//==================================================================================================
//protected ElementReference;
#nullable disable
protected AButton CloseButton;
#nullable restore
//==================================================================================================
bool DisplayContent => ParentPage.IsOnBookInfoSubpage;
//--------------------------------------------------------------------------------------------------

[CascadingParameter] HomePage? ParentPageN { get; set; }
[CascadingParameter] SelectBooks_Item? CurrentBookN { get; set; }

/// <exception cref="InvalidOperationException" />
HomePage ParentPage =>
   ParentPageN ?? throw CascadingPropertyIsNull( nameof(ParentPage) );
/// <exception cref="InvalidOperationException" />
SelectBooks_Item CurrentBook =>
   CurrentBookN ?? throw CascadingPropertyIsNull( nameof(CurrentBook) );

static InvalidOperationException CascadingPropertyIsNull(string propertyName) =>
   HInvOpExceptionCreator.Create(
      "\"{0}\" cascading property is null (\"{1}\"-component).",
      propertyName, nameof(BookCard) );

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

bool IsInEditMode => CurrentBook.NID.HasValue;
bool IsInAddingMode => !IsInEditMode;

bool IsForCorrection => ParentPage.IsForCorrection;
bool IsForReadOnly => !IsForCorrection;

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

public static string GetDisplayName(string? bookName) =>
   !string.IsNullOrWhiteSpace( bookName ) ? bookName.Trim() : "<No-name>";

//==================================================================================================
/*
protected override void On_Load() =>
go(()=>{
});
*/
protected override Task On_AfterLoad_A() =>
go(async()=>{
   UPage.DeferredUnscroll();
   CloseButton.DeferredFocus();
   await Task.Delay(0);
});

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

void EditButton_OnClick() =>
GO(()=>{
   ParentPage.SwitchToBookCardSubpage_Edit();
});

void CloseButton_OnClick() =>
GO(()=>{
   ParentPage.SwitchToMainContent();
});

Task RemoveButton_OnClick_A() =>
GO(async()=>{using (var fo=ForeOperation.WithSaveFocus(CloseButton)){
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   var book = CurrentBook;
   if (!await ESC_ImportantConfirmationDialogWithTitleFmt_A(
      "Book removal",
      "Are you sure you want to remove the book from registry?\n"+
      "Book-name: “{0}”.", GetDisplayName( book.Name ) ))
      return;
   var sir = await SRV.ESC_RemoveBook_A( book.ID );
   if ( WasAbandoned_Notify_Deactivate( sir ) )
      return;
   if ( await ESC_HasRequestError_ShowError_A( sir ) )
      return;
   fo.SaveFocus = false;   
   ParentPage.SwitchToMainContent( BookListCorrection.ApplyRemoval );
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}});

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // BookInfo
//**************************************************************************************************

} // BookRegistry.Client.Components
