@using DeepDrftPublic.Client.Services
@implements IDisposable
<div class="now-playing">
    <div class="np-label"><span class="np-dot"></span>Now Playing</div>
    <div class="np-title">@(Player?.CurrentTrack?.TrackName ?? "Nothing playing")</div>
    <div class="np-sub">
        @(Player?.CurrentTrack != null
            ? $"{Player.CurrentTrack.Release?.Artist} · {Player.CurrentTrack.Release?.Title ?? "Single"}"
            : "Select a track to begin")
    </div>
</div>


@code {
    [CascadingParameter] public IStreamingPlayerService? Player { get; set; }

    private IStreamingPlayerService? _subscribedPlayer;

    protected override void OnParametersSet()
    {
        if (Player != null && !ReferenceEquals(Player, _subscribedPlayer))
        {
            if (_subscribedPlayer != null)
                _subscribedPlayer.StateChanged -= OnPlayerStateChanged;

            Player.StateChanged += OnPlayerStateChanged;
            _subscribedPlayer = Player;
        }
    }

    private void OnPlayerStateChanged() => InvokeAsync(StateHasChanged);

    public void Dispose()
    {
        if (_subscribedPlayer != null)
        {
            _subscribedPlayer.StateChanged -= OnPlayerStateChanged;
            _subscribedPlayer = null;
        }
    }
}
