using adas_core.Application.Services.Interfaces;
using adas_core.Application.Subscriptions;
using MongoDB.Bson;

namespace adas_core.Application.Services;

public class SubscribersService : ISubscribersService
{
    private readonly List<WsSubscriber> _subscribers = [];


    public List<WsSubscriber> GetSubscribers()
    {
        lock (_subscribers)
        {
            return _subscribers.ToList();
        }
    }


    public WsSubscriber? GetById(string contextConnectionId)
    {
        lock (_subscribers)
        {
            return _subscribers.FirstOrDefault(s => s.Id == contextConnectionId);
        }
    }

    public List<WsSubscriber> GetByPocId(ObjectId pocId)
    {
        lock (_subscribers)
        {
            return _subscribers.Where(s => s.LocationIds.Contains(pocId)).ToList();
        }
    }

    public int RemoveConnectionById(string contextConnectionId)
    {
        lock (_subscribers)
        {
            return _subscribers.RemoveAll(s => s.Id == contextConnectionId);
        }
    }

    public void AddSubscriber(WsSubscriber subscriber)
    {
        lock (_subscribers)
        {
            _subscribers.Add(subscriber);
        }
    }
}