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>
/// Up-reset control (application components lib.)
/// </summary>
public partial class AUpResetControl: AComponent {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public ElementReference? HRefElement { get; protected set; }
//==================================================================================================

public static string? DefaultContainerStyle = "display: inline";
public static char DefaultResetSign = '\u2191'; // Upwards Arrow

[Parameter] public string? HRefId { get; set; }
[Parameter] public string? HRefClasses { get; set; }
[Parameter] public string? HRefStyle { get; set; }
[Parameter] public string? HRef { get; set; }
[Parameter] public string? Title { get; set; }
[Parameter] public char ResetSign { get; set; } = DefaultResetSign;
[Parameter] public bool Enabled { 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 AComponent? Target {
   get => _Target_;
   set {
      if ( Target!=null && Target!=value && value!=null &&
           Target?.Interop_OuterUpResetControl == value )
         Target.Interop_OuterUpResetControl = null;
      _Target_ = value;
      if ( value != null )
         value.Interop_OuterUpResetControl = this;
      StateHasChanged();
   }
}

public bool IsDropDownListTargeted =>
   Target is ADropDownList || Target is ADropDownList_IdName;

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

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

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

protected string HRefElement_EDClass =>
   Enabled ? "a_upreset_control_EnabledA" : "a_upreset_control_DisabledA";

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

protected string DefaultHRef
{
   get {
      if ( Target is ADropDownList )
         return ADropDownList.ResetHRefValue;
      if ( Target is ADropDownList_IdName )
         return ADropDownList_IdName.ResetHRefValue;
      if ( IsDropDownListTargeted )
         return ADropDownList.ResetHRefValue;
      return "#reset";
   }
}

protected string DefaultTitle
{
   get {
      if ( Target is ADropDownList )
         return ADropDownList.ResetHRefTitle;
      if ( Target is ADropDownList_IdName )
         return ADropDownList_IdName.ResetHRefTitle;
      if ( IsDropDownListTargeted )
         return ADropDownList.ResetHRefTitle;
      return "Reset";
   }
}

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

void HRefElement_OnClick(MouseEventArgs e) =>
go_nt(()=>{
   if ( !Enabled )
      return;
   Target?.Interop_UpReset();
   OnClick_Invoke( e );
});

void HRefElement_OnFocus(FocusEventArgs e) =>
go_nt(()=>{
   if ( !Enabled )
      return;
   Target?.Interop_Focus();
   OnFocus_Invoke( e );
});

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

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

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

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

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

AComponent? _Target_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // AUpResetControl
//**************************************************************************************************

} // BookRegistry.Client.Shared
