using System.Windows.Input;
using Connected.Annotations;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

namespace Connected.Components;

public abstract class SelectItemBase : UIComponent
{
	/// <summary>
	/// If true, the input element will be disabled.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.Behavior)]
	public bool Disabled { get; set; }

	/// <summary>
	/// If true, disables ripple effect.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.Appearance)]
	public bool DisableRipple { get; set; }

	/// <summary>
	/// Link to a URL when clicked.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.ClickAction)]
	public string Href { get; set; }

	/// <summary>
	/// If true, force browser to redirect outside component router-space.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.ClickAction)]
	public bool ForceLoad { get; set; }

	/// <summary>
	/// Child content of component.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.Behavior)]
	public RenderFragment ChildContent { get; set; }

	/// <summary>
	/// Command parameter.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.ClickAction)]
	public object CommandParameter { get; set; }

	/// <summary>
	/// Command executed when the user clicks on an element.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.ClickAction)]
	public ICommand Command { get; set; }

	[Inject] private NavigationManager UriHelper { get; set; }

	[Parameter]
	public EventCallback<MouseEventArgs> OnClick { get; set; }

	protected async Task OnClickHandler(MouseEventArgs ev)
	{
		if (Disabled)
			return;
		if (Href != null)
		{
			UriHelper.NavigateTo(Href, ForceLoad);
		}
		else
		{
			await OnClick.InvokeAsync(ev);
			if (Command?.CanExecute(CommandParameter) ?? false)
			{
				Command.Execute(CommandParameter);
			}
		}
	}
}
