using Connected.Annotations;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;

namespace Connected.Components;

public partial class NavLink : SelectItemBase
{
	protected string Classname =>
	new CssBuilder("nav-item")
	  .AddClass($"ripple", !DisableRipple && !Disabled)
	  .AddClass(AdditionalClassList)
	  .Build();

	protected string LinkClassname =>
	new CssBuilder("nav-link")
	  .AddClass($"nav-link-disabled", Disabled)
	  .Build();

	protected string IconClassname =>
	new CssBuilder("nav-link-icon")
	  .AddClass($"nav-link-icon-default", IconColor == ThemeColor.Default)
	  .Build();

	protected Dictionary<string, object> Attributes
	{
		get => Disabled ? null : new Dictionary<string, object>()
		 {
			 { "href", Href },
			 { "target", Target },
			 { "rel", !string.IsNullOrWhiteSpace(Target) ? "noopener noreferrer" : string.Empty }
		 };
	}

	/// <summary>
	/// Glyph to use if set.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.NavMenu.Behavior)]
	public string Icon { get; set; }

	/// <summary>
	/// The color of the icon. It supports the theme colors, default value uses the themes drawer icon color.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.NavMenu.Appearance)]
	public ThemeColor IconColor { get; set; } = ThemeColor.Default;

	[Parameter]
	[Category(CategoryTypes.NavMenu.Behavior)]
	public NavLinkMatch Match { get; set; } = NavLinkMatch.Prefix;

	[Parameter]
	[Category(CategoryTypes.NavMenu.ClickAction)]
	public string Target { get; set; }

	/// <summary>
	/// User class names when active, separated by space.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.ComponentBase.Common)]
	public string ActiveClass { get; set; } = "active";

	[CascadingParameter] INavigationEventReceiver NavigationEventReceiver { get; set; }

	protected Task HandleNavigation()
	{
		if (!Disabled && NavigationEventReceiver != null)
		{
			return NavigationEventReceiver.OnNavigation();
		}
		return Task.CompletedTask;
	}
}
