using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;

using CommonHelpers.WASM;
using AFXUI;

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

namespace BookRegistry.Client {

//**************************************************************************************************
partial class App {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static event Action?
   FocusChanged, FocusBlur;
public static event Action<KeyboardEventArgs>?
   OnKeyDown, OnKeyUp;
/*
public static event WithDomIdCallbackN?
   OnInput;
*/
public static readonly HListSet<Func<Task>>?
   FocusChangedA = new HListSet<Func<Task>>(),
   FocusBlurA = new HListSet<Func<Task>>();
public static readonly HListSet<Func<KeyboardEventArgs,Task>>?
   OnKeyDownA = new HListSet<Func<KeyboardEventArgs,Task>>(),
   OnKeyUpA = new HListSet<Func<KeyboardEventArgs,Task>>();
/*
public static HListSet<WithDomIdCallbackNA>?
   OnInputA = new HListSet<Func<WithDomIdCallbackNA>>;
*/
//==================================================================================================

[JSInvokable]
public static void wasm_App_FocusChanged() =>
go_static_nt(MethodBase.GetCurrentMethod(),()=>{
   FocusChanged?.Invoke();
});
[JSInvokable]
public static Task wasm_App_FocusChanged_A() =>
go_static_nt(MethodBase.GetCurrentMethod(),async()=>{
   if ( FocusChangedA != null )
      foreach (var callbackA in FocusChangedA)
#pragma warning disable 4014
         callbackA.Invoke();
#pragma warning restore 4014
   await Task.Delay(0);
});

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

[JSInvokable]
public static void wasm_App_FocusBlur() =>
go_static_nt(MethodBase.GetCurrentMethod(),()=>{
   FocusBlur?.Invoke();
});
[JSInvokable]
public static Task wasm_App_FocusBlur_A() =>
go_static_nt(MethodBase.GetCurrentMethod(),async()=>{
   if ( FocusBlurA != null )
      foreach (var callbackA in FocusBlurA)
#pragma warning disable 4014
         callbackA.Invoke();
#pragma warning restore 4014
   await Task.Delay(0);
});

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

[JSInvokable]
public static void wasm_App_OnKeyDown(
   bool altKey, string? code, bool ctrlKey, string? key, float location, bool metaKey, bool repeat,
   bool shiftKey, string? type ) =>
go_static_nt(MethodBase.GetCurrentMethod(),()=>{
   OnKeyDown?.Invoke(new KeyboardEventArgs(){
      AltKey = altKey,
      Code = code ?? "",
      CtrlKey = ctrlKey,
      Key = key ?? "",
      Location = location,
      MetaKey = metaKey,
      Repeat = repeat,
      ShiftKey = shiftKey,
      Type = type ?? ""
   });
});
[JSInvokable]
public static Task wasm_App_OnKeyDown_A(
   bool altKey, string? code, bool ctrlKey, string? key, float location, bool metaKey, bool repeat,
   bool shiftKey, string? type ) =>
go_static_nt(MethodBase.GetCurrentMethod(),async()=>{
   if ( OnKeyDownA != null )
      foreach (var callbackA in OnKeyDownA)
#pragma warning disable 4014
         callbackA.Invoke(new KeyboardEventArgs(){
            AltKey = altKey,
            Code = code ?? "",
            CtrlKey = ctrlKey,
            Key = key ?? "",
            Location = location,
            MetaKey = metaKey,
            Repeat = repeat,
            ShiftKey = shiftKey,
            Type = type ?? ""
         });
#pragma warning restore 4014
   await Task.Delay(0);
});

[JSInvokable]
public static void wasm_App_OnKeyUp(
   bool altKey, string? code, bool ctrlKey, string? key, float location, bool metaKey, bool repeat,
   bool shiftKey, string? type ) =>
go_static_nt(MethodBase.GetCurrentMethod(),()=>{
   OnKeyUp?.Invoke(new KeyboardEventArgs(){
      AltKey = altKey,
      Code = code ?? "",
      CtrlKey = ctrlKey,
      Key = key ?? "",
      Location = location,
      MetaKey = metaKey,
      Repeat = repeat,
      ShiftKey = shiftKey,
      Type = type ?? ""
   });
});
[JSInvokable]
public static Task wasm_App_OnKeyUp_A(
   bool altKey, string? code, bool ctrlKey, string? key, float location, bool metaKey, bool repeat,
   bool shiftKey, string? type ) =>
go_static_nt(MethodBase.GetCurrentMethod(),async()=>{
   if ( OnKeyUpA != null )
      foreach (var callbackA in OnKeyUpA)
#pragma warning disable 4014
         callbackA.Invoke(new KeyboardEventArgs(){
            AltKey = altKey,
            Code = code ?? "",
            CtrlKey = ctrlKey,
            Key = key ?? "",
            Location = location,
            MetaKey = metaKey,
            Repeat = repeat,
            ShiftKey = shiftKey,
            Type = type ?? ""
         });
#pragma warning restore 4014
   await Task.Delay(0);
});

//--------------------------------------------------------------------------------------------------
/*
[JSInvokable]
public static void wasm_App_OnInput(string? domId) =>
go_static_nt(MethodBase.GetCurrentMethod(),()=>{
   OnInput?.Invoke( domId );
});
[JSInvokable]
public static Task wasm_App_OnInput_A(string? domId) =>
go_static_nt(MethodBase.GetCurrentMethod(),async()=>{
   if ( OnInputA != null )
      foreach (var callbackA in OnInputA)
#pragma warning disable 4014
         callbackA.Invoke( domId );
#pragma warning restore 4014
   await Task.Delay(0);
});
*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // App
//**************************************************************************************************

} // BookRegistry.Client
