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

namespace Connected.Components;

partial class Avatar : UIComponent, IDisposable
{
	[CascadingParameter]
	protected AvatarGroup? AvatarGroup { get; set; }

	protected CssBuilder CompiledClassList
	{
		get
		{
			return new CssBuilder("avatar")
			  .AddClass($"avatar-{Size}")
			  .AddClass($"avatar-rounded", Kind == AvatarKind.Rounded)
			  .AddClass($"avatar-square", Kind == AvatarKind.Square)
			  .AddClass($"avatar-{Variant}")
			  .AddClass($"avatar-{Variant}-{Color}")
			  .AddClass($"elevation-{Elevation}")
			  .AddClass(ClassList)
			  .AddClass(AvatarGroup?.GetAvatarSpacing() ?? new CssBuilder(), AvatarGroup != null);
		}
	}

	protected StyleBuilder CompiledStyles
	{
		get
		{
			return new StyleBuilder()
				.AddStyle(AvatarGroup?.GetAvatarZindex(this) ?? new StyleBuilder(), AvatarGroup is not null);
		}
	}

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

	/// <summary>
	/// If set (and Image is also set), will add an alt property to the img element
	/// </summary>
	[Parameter]
	public string? ImageAltText { get; set; }

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

	/// <summary>
	/// The color of the component. It supports the theme colors.
	/// </summary>
	[Parameter]
	public ThemeColor Color { get; set; } = ThemeColor.Default;

	/// <summary>
	/// The higher the number, the heavier the drop-shadow.
	/// </summary>
	[Parameter]
	public int Elevation { set; get; } = 0;

	/// <summary>
	/// Link to image, if set a image will be displayed instead of text.
	/// </summary>
	[Parameter]
	public string? Image { get; set; }

	/// <summary>
	/// Sets the avatar appearance
	/// </summary>
	public AvatarKind Kind { get; set; }

	/// <summary>
	/// The Size of the MudAvatar.
	/// </summary>
	[Parameter]
	public Size Size { get; set; } = Size.Medium;

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

	protected override void OnInitialized()
	{
		base.OnInitialized();

		AvatarGroup?.AddAvatar(this);
	}

	public void Dispose() => AvatarGroup?.RemoveAvatar(this);

	internal void ForceRedraw() => StateHasChanged();
}
