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

namespace Connected.Components;

public partial class Alert : UIComponent{
	/// <summary>
	/// Contains the default container classlist and the user defined classes.
	/// </summary>
	private CssBuilder CompiledClassList
	{
		get
		{
			return new CssBuilder("alert")
				.AddClass($"alert-{Variant}-{Severity}")
				.AddClass(ClassList);
		}
	}

	/// <summary>
	/// Child content of the component.
	/// </summary>
	[Parameter]
	public RenderFragment? ChildContent { get; set; }

	#region EventCallbacks
	/// <summary>
	/// Raised when the alert is clicked.
	/// </summary>
	[Parameter]
	public EventCallback<MouseEventArgs> Clicked { get; set; }

	/// <summary>
	/// The callback, when the close glyph has been clicked.
	/// </summary>
	[Parameter] public EventCallback<Alert> CloseClicked { get; set; }

	#endregion

	#region Content placeholders
	/// <summary>
	/// Define the glyph used for the close button.
	/// </summary>
	[Parameter]
	public string CloseGlyph { get; set; } = Icons.Material.Filled.Close;

	/// <summary>
	/// Custom glyph, leave unset to use the standard glyph which depends on the Severity.
	/// </summary>
	[Parameter]
	public string Glyph { get; set; } = default!;
	#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>
	/// Sets if the alert shows a close glyph.
	/// </summary>
	[Parameter]
	public bool CloseGlyphVisible { get; set; }

	/// <summary>
	/// If true, no alert glyph will be used.
	/// </summary>
	[Parameter]
	public bool GlyphVisible { get; set; }

	/// <summary>
	/// Specifies the control's display style
	/// </summary>
	//TODO: Implement in DOM
	[CascadingParameter(Name = "RightToLeft")]
	public bool RightToLeft { get; set; }

	/// <summary>
	/// The severity of the alert. This defines the color and glyph used.
	/// </summary>
	[Parameter]
	public Severity Severity { get; set; } = Severity.Normal;

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

	protected override void OnParametersSet()
	{
		if (!string.IsNullOrEmpty(Glyph))
			return;

		Glyph = GetGlyphForSeverity(Severity);
	}

	private string GetGlyphForSeverity(Severity severity)
	{
		return Severity switch
		{
			Severity.Normal => Icons.Material.Outlined.EventNote,
			Severity.Info => Icons.Material.Outlined.Info,
			Severity.Success => Icons.Custom.Uncategorized.AlertSuccess,
			Severity.Warning => Icons.Material.Outlined.ReportProblem,
			Severity.Error => Icons.Material.Filled.ErrorOutline,
			_ => throw new ArgumentOutOfRangeException(nameof(severity)),
		};
	}

	internal async Task OnCloseGlyphClick()
	{
		if (!CloseClicked.HasDelegate)
			return;

		await CloseClicked.InvokeAsync(this);
	}
}