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 Microsoft.JSInterop;

using CommonHelpers.WASM;
using AFXUI;

namespace BookRegistry.Client.Shared {

//**************************************************************************************************
/// <summary>
/// Button-control (application components lib.)
/// </summary>
public partial class AButton: AComponent {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public ElementReference? ButtonElement { get; protected set; }
//==================================================================================================

public static string? DefaultClasses = "btn btn-secondary btn-sm";
public static string? DefaultStyle;

[Parameter] public RenderFragment? ChildContent { get; set; }
[Parameter] public string? ID { get; set; }
[Parameter] public string? Classes_0 { get; set; } = DefaultClasses;
[Parameter] public string? Classes { get; set; }
[Parameter] public string? Style_0 { get; set; } = DefaultStyle;
[Parameter] public string? Style { get; set; }
[Parameter] public string? Title { get; set; }
[Parameter] public string? AccessKey { get; set; }
[Parameter] public int? TabIndex { get; set; }
[Parameter] public bool Enabled { get; set; } = true;
[Parameter] public bool Focusable { get; set; } = true;

[Parameter] public Action? ON_Click { get; set; }
[Parameter] public Action? ON_Focus { get; set; }

[Parameter] public EventCallback ON_Click_A { get; set; }
[Parameter] public EventCallback ON_Focus_A { get; set; }

[Parameter(CaptureUnmatchedValues=true)]
public Dictionary<string,object>? Attributes { get; set; }

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

public bool Click() =>
   Enabled ? ButtonElement.Click() : false;

public bool FocusClick() =>
   Focus() && Click();

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

public bool Focus() =>
   ButtonElement.Focus();

public bool DeferredFocus() =>
   ButtonElement.DeferredFocus();
public bool DeferredFocus(int milliseconds) =>
   ButtonElement.DeferredFocus( milliseconds );

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

protected string ButtonElement_EDClass =>
   Enabled ? "a_button_EnabledBUTTON" : "a_button_DisabledBUTTON";

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

void ButtonElement_OnClick(MouseEventArgs e) =>
go_nt(()=>{
   if ( Enabled )
      OnClick_Invoke( e );
});

void ButtonElement_OnFocus(FocusEventArgs e) =>
go_nt(()=>{
   if ( !Enabled )
      return;
   if ( Focusable )
      OnFocus_Invoke( e );
   else
      UPage.FocusBackground();
});

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

public override bool IsEnabled =>
   Enabled && ButtonElement.IsEnabled();

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

public override ElementReference? MainElement { get => ButtonElement; }

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

public override bool Interop_Focus() =>
   Focus();
public override bool Interop_DeferredFocus() =>
   DeferredFocus();
public override bool Interop_Click() =>
   Click();

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

void OnClick_Invoke(MouseEventArgs e)
{
   ON_Click?.Invoke();
   ON_Click_A.InvokeAsync( e );
}
void OnFocus_Invoke(FocusEventArgs e)
{
   ON_Focus?.Invoke();
   ON_Focus_A.InvokeAsync( e );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // AButton
//**************************************************************************************************

} // BookRegistry.Client.Shared
