using Managing.Application.Abstractions.Services;
using Managing.Application.Hubs;
using Microsoft.AspNetCore.SignalR;

namespace Managing.Application.Shared;

public class StreamService : IStreamService
{
    private readonly IExchangeStream _exchangeStream;
    private readonly IHubContext<CandleHub> _hubContext;


    public StreamService(IExchangeStream exchangeStream, IHubContext<CandleHub> hubContext)
    {
        _exchangeStream = exchangeStream;
        _hubContext = hubContext;
    }

    public async Task SubscribeCandle()
    {
        await _exchangeStream.StartBinanceWorker(Common.Enums.Ticker.BTC, async (candle) => {
            await _hubContext.Clients.All.SendAsync(candle.Ticker, candle);
        });
    }

    public async Task UnSubscribeCandle()
    {
        await _exchangeStream.StopBinanceWorker();
    }
}
