@inject IJSRuntime JSRuntime
@implements IDisposable

<polygon @ref="Ref" fill="@Model.Fill" style="visibility: @(IsShow ? "visible" : "hidden")" />

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

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

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

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

    private ElementReference Ref;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (!firstRender) return;

        await UpdateData();
    }

    private async Task UpdateData()
    {
        string data = $"{Model.X1},{Model.Y1} {Model.X2},{Model.Y2} {Model.X3},{Model.Y3} {Model.X4},{Model.Y4}";

        await JSRuntime.InvokeVoidAsync("ElementSetAttribute", Ref, "points", data);
    }

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