@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Localization
@using Microsoft.AspNetCore.SignalR.Client
@using Microsoft.Extensions.Localization
@inject StreamingService               StreamingService
@inject GlobalComponentSvc             GlobalComponentSvc
@inject IStringLocalizer<Localization> Loc

<div class="connection-status @(_hubConnectionState == HubConnectionState.Connected ? "fadeout" : "")">
    @switch (_hubConnectionState)
    {
        case HubConnectionState.Connected:
            <div class="state">
                @Loc["Connected"]
            </div>
            break;
        case HubConnectionState.Connecting:
            <div class="state">
                <LoadingSpinner/> @Loc["Connecting"]
            </div>
            break;
        case HubConnectionState.Disconnected:
            <div @onclick="Reconnect" class="state disconnected">
                @Loc["Disconnected"]
            </div>
            break;
        case HubConnectionState.Reconnecting:
            <div class="state">
                <LoadingSpinner /> @Loc["Reconnecting"]
            </div>
            break;
    }

</div>

@code {
    private HubConnectionState _hubConnectionState = HubConnectionState.Connected;

    protected override async Task OnInitializedAsync()
    {
        await StreamingService.ConnectAsync();
        StreamingService.OnConnectionChange += HandleConnectionChange;
    }

    private void HandleConnectionChange(object? _, HubConnectionState hubConnectionState)
    {
        _hubConnectionState = hubConnectionState;
        StateHasChanged();
    }

    private async Task Reconnect()
    {
        await StreamingService.ReconnectAsync();
    }
}