using System.Diagnostics.CodeAnalysis;
using Connected.Extensions;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace Connected.Components;
public partial class Alert : UIComponent
{
[CascadingParameter(Name = "RightToLeft")]
public bool RightToLeft { get; set; }
///
/// Sets the position of the text to the start (Left in LTR and right in RTL).
///
[Parameter]
public HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Left;
///
/// The callback, when the close button has been clicked.
///
[Parameter] public EventCallback CloseGlyphClicked { get; set; }
///
/// Define the icon used for the close button.
///
[Parameter]
public string CloseGlyph { get; set; } = Icons.Material.Filled.Close;
///
/// Sets if the alert shows a close icon.
///
[Parameter]
public bool CloseGlyphVisible { get; set; }
///
/// The higher the number, the heavier the drop-shadow. 0 for no shadow.
///
[Parameter]
public int Elevation { set; get; } = 0;
///
/// If true, rounded corners are disabled.
///
[Parameter]
public bool Square { get; set; }
///
/// If true, compact padding will be used.
///
[Parameter]
public bool Dense { get; set; }
///
/// If true, no alert icon will be used.
///
[Parameter]
public bool GlyphVisible { get; set; }
///
/// The severity of the alert. This defines the color and icon used.
///
[Parameter]
public Severity Severity { get; set; } = Severity.Normal;
///
/// The variant to use.
///
[Parameter]
public Variant Variant { get; set; } = Variant.Text;
///
/// Child content of the component.
///
[Parameter]
public RenderFragment? ChildContent { get; set; }
///
/// Custom icon, leave unset to use the standard icon which depends on the Severity
///
[Parameter]
public string Glyph { get; set; } = default!;
///
/// Raised when the alert is clicked
///
[Parameter]
public EventCallback OnClick { get; set; }
protected string ClassList
{
get
{
return new CssBuilder("alert")
.AddClass($"alert-{Variant.ToDescriptionString()}-{Severity.ToDescriptionString()}")
.AddClass($"dense", Dense)
.AddClass($"square", Square)
.AddClass($"elevation-{Elevation}")
.AddClass(Class)
.Build();
}
}
protected string ClassPosition
{
get
{
return new CssBuilder("alert-position")
.AddClass($"justify-sm-{ConvertHorizontalAlignment(Alignment).ToDescriptionString()}")
.Build();
}
}
private HorizontalAlignment ConvertHorizontalAlignment(HorizontalAlignment contentAlignment)
{
return contentAlignment switch
{
HorizontalAlignment.Right => RightToLeft ? HorizontalAlignment.Start : HorizontalAlignment.End,
HorizontalAlignment.Left => RightToLeft ? HorizontalAlignment.End : HorizontalAlignment.Start,
_ => contentAlignment
};
}
internal async Task OnCloseGlyphClick()
{
if (CloseGlyphClicked.HasDelegate)
{
await CloseGlyphClicked.InvokeAsync(this);
return;
}
await Task.CompletedTask;
}
//If we can check this exception can include the coverage again
[ExcludeFromCodeCoverage]
protected override void OnParametersSet()
{
if (!string.IsNullOrEmpty(Glyph))
return;
Glyph = 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)),
};
}
}