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>
/// Hyperlink-reference control (application components lib.)
/// </summary>
public partial class AHRef: AComponent {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static NavigationManager NAV => Services.NavigationManager;
//==================================================================================================
public ElementReference? HRefElement { get; protected set; }
//==================================================================================================

[Parameter] public RenderFragment? ChildContent { get; set; }
[Parameter] public string? ID { get; set; }
[Parameter] public string? HRef { get; set; }
[Parameter] public string? Classes { get; set; }
[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 bool NavigateAuto { get; set; }

[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 ? HRefElement.Click() : false;

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

public bool Focus() =>
   Enabled ? HRefElement.Focus() : false;

public bool DeferredFocus() =>
   Enabled ? HRefElement.DeferredFocus() : false;
public bool DeferredFocus(int milliseconds) =>
   Enabled ? HRefElement.DeferredFocus( milliseconds ) : false;

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

protected string HRefElement_EDClass =>
   Enabled ? "a_href_EnabledA" : "a_href_DisabledA";

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

void HRefElement_OnClick(MouseEventArgs e) =>
go_nt(()=>{
   if ( !Enabled )
      return;
   if ( NavigateAuto && HRef != null )
      NAV.NavigateTo( HRef );
   OnClick_Invoke( e );
});

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

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

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

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

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

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

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 );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // AHRef
//**************************************************************************************************

} // BookRegistry.Client.Shared
