@using Microsoft.JSInterop

@implements IDisposable
@inject IJSRuntime JSRuntime

<circle @ref="circleErrorRef" style="fill: red; opacity: 0.6; r: 0.3px;"/>
<circle @ref="circleRef"/>
<text @ref="textRef" visibility="@(ShowName ? "visible" : "hidden")">@Model?.Name</text>

@code {
    [Parameter, EditorRequired]
    public NodeModel Model { get; set; } = null!;

    [Parameter]
    public EventCallback<NodeModel> DoubleClick { get; set; }

    [Parameter]
    public EventCallback<NodeModel?> OnClick { get; set; }

    [CascadingParameter]
    protected bool MapIsActive { get; set; }

    [Parameter]
    public bool ShowName { get; set; }

    private ElementReference circleRef;
    private ElementReference textRef;
    private ElementReference circleErrorRef;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (!firstRender) return;
        await UpdatePosition(Model.X, Model.Y); 
        await JSRuntime.InvokeVoidAsync("ElementSetAttribute", circleErrorRef, "visibility", "hidden");
    }

    private async Task UpdatePosition(double x, double y)
    {
        await JSRuntime.InvokeVoidAsync("SetNodePosition", circleRef, textRef, x, y);
    }

    public void Dispose()
    {
        GC.SuppressFinalize(this);
    }
}
