@namespace Lumeo

<Popover @bind-Open="_isOpen" @attributes="AdditionalAttributes">
    <span @onclick="Open" class="inline-flex cursor-pointer">
        @ChildContent
    </span>
    <PopoverContent Class="@($"w-72 p-4 {Class}".Trim())">
        <div class="flex flex-col gap-3">
            <div class="flex items-start gap-2">
                @if (Icon != null)
                {
                    <span class="mt-0.5 shrink-0 text-muted-foreground">
                        <Blazicon Svg="@Icon" class="h-4 w-4" />
                    </span>
                }
                <div class="flex flex-col gap-1">
                    <p class="text-sm font-medium leading-none">@EffectiveTitle</p>
                    @if (!string.IsNullOrEmpty(Description))
                    {
                        <p class="text-sm text-muted-foreground">@Description</p>
                    }
                </div>
            </div>
            <div class="flex justify-end gap-2">
                <Button Variant="Button.ButtonVariant.Outline" Size="Button.ButtonSize.Sm" OnClick="HandleCancel">
                    @EffectiveCancelText
                </Button>
                <Button Variant="@(IsDestructive ? Button.ButtonVariant.Destructive : Button.ButtonVariant.Default)"
                        Size="Button.ButtonSize.Sm"
                        OnClick="HandleConfirm">
                    @EffectiveConfirmText
                </Button>
            </div>
        </div>
    </PopoverContent>
</Popover>

@inject Lumeo.Services.Localization.ILumeoLocalizer L

@code {
    [Parameter] public string? Title { get; set; }
    [Parameter] public string? Description { get; set; }
    [Parameter] public string? ConfirmText { get; set; }
    [Parameter] public string? CancelText { get; set; }

    private string EffectiveTitle => Title ?? L["PopConfirm.Title"];
    private string EffectiveConfirmText => ConfirmText ?? L["PopConfirm.Confirm"];
    private string EffectiveCancelText => CancelText ?? L["PopConfirm.Cancel"];
    [Parameter] public bool IsDestructive { get; set; }
    [Parameter] public EventCallback OnConfirm { get; set; }
    [Parameter] public EventCallback OnCancel { get; set; }
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public string Placement { get; set; } = "top";
    [Parameter] public Blazicons.SvgIcon? Icon { get; set; }
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private bool _isOpen;

    private void Open() => _isOpen = true;

    private async Task HandleConfirm()
    {
        _isOpen = false;
        await OnConfirm.InvokeAsync();
    }

    private async Task HandleCancel()
    {
        _isOpen = false;
        await OnCancel.InvokeAsync();
    }
}
