using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Radzen.Blazor;
namespace Radzen
{
///
/// A service for displaying tooltips programmatically on UI elements or at specific positions.
/// TooltipService provides methods to show tooltips with text or custom HTML content, with configurable positioning, delays, and durations.
/// To use this service, add it as a scoped service in your application's service collection and place a RadzenTooltip component in your main layout.
/// Manages tooltip lifecycle, automatically closing tooltips on navigation and providing various positioning options (top, bottom, left, right).
/// Tooltips can be shown on mouse enter/leave events or on demand, with configurable delays before showing and auto-close durations.
///
///
/// Show a simple text tooltip:
///
/// @inject TooltipService TooltipService
/// <RadzenButton Text="Hover me" MouseEnter="@(args => TooltipService.Open(args, "This is a tooltip"))" />
///
/// Show a tooltip with HTML content and custom options:
///
/// @inject TooltipService TooltipService
/// <RadzenButton Text="Show tooltip" MouseEnter="@(args => ShowTooltipWithHtml(args))" />
/// @code {
/// void ShowTooltipWithHtml(ElementReference element) => TooltipService.Open(element, ts =>
/// @<div>
/// <b>Bold</b> and <i>italic</i> content
/// </div>,
/// new TooltipOptions { Position = TooltipPosition.Top, Duration = 5000 });
/// }
///
///
public class TooltipService : IDisposable
{
///
/// Gets or sets the navigation manager.
///
/// The navigation manager.
NavigationManager? navigationManager { get; set; }
///
/// Initializes a new instance of the class.
///
/// The URI helper.
public TooltipService(NavigationManager uriHelper)
{
navigationManager = uriHelper;
if (navigationManager != null)
{
navigationManager.LocationChanged += UriHelper_OnLocationChanged;
}
}
///
/// Handles the OnLocationChanged event of the UriHelper control.
///
/// The source of the event.
/// The instance containing the event data.
private void UriHelper_OnLocationChanged(object? sender, Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs e)
{
if (this.OnNavigate != null)
{
this.OnNavigate();
}
}
///
/// Occurs when [on navigate].
///
public event Action? OnNavigate;
///
/// Raises the Close event.
///
public event Action? OnClose;
///
/// Occurs when [on open].
///
public event Action? OnOpen;
///
/// Occurs when [on open chart tooltip].
///
internal event Action? OnOpenChartTooltip;
///
/// Opens a tooltip with custom HTML content near the specified element.
/// The tooltip will be positioned according to the options and can contain any Blazor markup.
///
/// The HTML element reference near which the tooltip will be displayed.
/// A render fragment that defines the custom HTML content of the tooltip. Receives the TooltipService as context.
/// Optional tooltip configuration including position, duration, delay, and styling. If null, default options are used.
public void Open(ElementReference element, RenderFragment childContent, TooltipOptions? options = null)
{
var tooltipOptions = options ?? new TooltipOptions();
tooltipOptions.ChildContent = childContent;
OpenTooltip