using Connected.Annotations;
using Connected.Extensions;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

namespace Connected.Components;

public partial class Link : UIComponent
{
	protected string Classname =>
	new CssBuilder("typography mud-link")
		.AddClass($"{Color.ToDescription()}-text")
	  .AddClass($"link-underline-{Underline.ToDescription()}")
	  .AddClass($"typography-{Typo.ToDescription()}")
	  // When Href is empty, link's hover cursor is text "I beam" even when Clicked has a delegate.
	  // To change this for more expected look change hover cursor to a pointer:
	  .AddClass("cursor-pointer", Href == default && OnClick.HasDelegate && !Disabled)
	  .AddClass($"link-disabled", Disabled)
	  .AddClass(AdditionalClassList)
	.Build();

	private Dictionary<string, object> Attributes
	{
		get => Disabled ? CustomAttributes : new Dictionary<string, object>(CustomAttributes)
		 {
			 { "href", Href },
			 { "target", Target }
		 };
	}

	/// <summary>
	/// The color of the component. It supports the theme colors.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Link.Appearance)]
	public ThemeColor Color { get; set; } = ThemeColor.Primary;

	/// <summary>
	/// Typography variant to use.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Link.Appearance)]
	public Typo Typo { get; set; } = Typo.body1;

	/// <summary>
	/// Controls when the link should have an underline.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Link.Appearance)]
	public Underline Underline { get; set; } = Underline.Hover;

	/// <summary>
	/// The URL, which is the actual link.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Link.Behavior)]
	public string Href { get; set; }

	/// <summary>
	/// The target attribute specifies where to open the link, if Link is specified. Possible values: _blank | _self | _parent | _top | <i>framename</i>
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Link.Behavior)]
	public string Target { get; set; }

	/// <summary>
	/// Link click event.
	/// </summary>
	[Parameter] public EventCallback<MouseEventArgs> OnClick { get; set; }

	protected async Task OnClickHandler(MouseEventArgs ev)
	{
		if (Disabled) return;
		await OnClick.InvokeAsync(ev);
	}

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

	/// <summary>
	/// If true, the navlink will be disabled.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Link.Behavior)]
	public bool Disabled { get; set; }
}
