using Microsoft.AspNetCore.Components;

namespace Connected.Components;

public partial class ToggleGlyphButton : UIComponent
{
	#region EventCallbacks
	/// <summary>
	/// Fires whenever toggled is changed. 
	/// </summary>
	[Parameter]
	public EventCallback<bool> ToggledChanged { get; set; }
	#endregion

	#region Content placeholders
	/// <summary>
	/// The glyph that will be used in the untoggled state.
	/// </summary>
	[Parameter]
	public string? Glyph { get; set; }

	/// <summary>
	/// GlyphTitle of the icon used for accessibility.
	/// </summary>
	[Parameter]
	public string? GlyphTitle { get; set; }

	/// <summary>
	/// The glyph that will be used in the toggled state.
	/// </summary>
	[Parameter]
	public string? ToggledGlyph { get; set; }

	/// <summary>
	/// GlyphTitle used in toggled state, if different.
	/// </summary>
	[Parameter]
	public string? ToggledGlyphTitle { get; set; }
	#endregion

	#region Styling properties
	/// <summary>
	/// A space separated list of class names, added on top of the default class list.
	/// </summary>
	[Parameter]
	public string? ClassList { get; set; }

	/// <summary>
	/// The variant to use.
	/// </summary>
	[Parameter]
	public Variant Variant { get; set; } = Variant.Text;
	#endregion

	#region Behavior properties
	/// <summary>
	/// If true, the button will be disabled.
	/// </summary>
	[Parameter]
	public bool Disabled { get; set; }

	/// <summary>
	/// The button toggled state.
	/// </summary>
	[Parameter]
	public bool Toggled { get; set; }
	#endregion

	public async Task Toggle()
	{
		await SetToggledAsync(!Toggled);
	}

	protected internal async Task SetToggledAsync(bool toggled)
	{
		if (Disabled)
			return;

		if (Toggled != toggled)
		{
			Toggled = toggled;

			if (!ToggledChanged.HasDelegate)
				return;

			await ToggledChanged.InvokeAsync(Toggled);
		}
	}
}
