using Managing.Application.Abstractions.Services;
using Managing.Application.Hubs;
using Managing.Application.Workers;
using Managing.Application.Workers.Abstractions;
using Microsoft.AspNetCore.SignalR;
using static Managing.Common.Enums;

namespace Managing.Api.Workers.Workers;

public class PositionFetcher : BaseWorker<PositionFetcher>
{
    private static readonly WorkerType _workerType = WorkerType.PositionFetcher;
    private readonly ITradingService _tradingService;
    private readonly IHubContext<PositionHub> _hubContext;
    private readonly ILogger<PositionFetcher> _logger;

    public PositionFetcher(
        ILogger<PositionFetcher> logger,
        IWorkerService workerService,
        ITradingService tradingService,
     
        IHubContext<PositionHub> hubContext) : base(
        _workerType,
        logger,
        TimeSpan.FromSeconds(10),
        workerService)
    {
        _logger = logger;
        _tradingService = tradingService;
        _hubContext = hubContext;
    }

    protected override async Task Run(CancellationToken cancellationToken)
    {
        var positions = _tradingService.GetPositions().Where(p => p.Initiator != PositionInitiator.PaperTrading);
        await _hubContext.Clients.All.SendAsync("Positions", positions);
    }
}
