using Hcs.WebApp.Data.Hcs;
using System.Collections.Concurrent;

namespace Hcs.WebApp.BackgroundServices
{
    public class ResultWaitState
    {
        private readonly ConcurrentQueue<Operation> operationsInQueue = new();
        private readonly HashSet<Operation> operationsInProcess = [];

        public bool Ready { get; set; }

        public void EnqueueOperation(Operation operation)
        {
            operationsInQueue.Enqueue(operation);
        }

        public bool TryDequeueOperation(out Operation operation)
        {
            return operationsInQueue.TryDequeue(out operation);
        }

        public void SetProcessingOperation(Operation operation)
        {
            operationsInProcess.Add(operation);
        }

        public void UnsetProcessingOperation(Operation operation)
        {
            operationsInProcess.Remove(operation);
        }

        public bool HasCampaignOperation(int campaignId)
        {
            return operationsInQueue.Any(x => x.CampaignId == campaignId) || operationsInProcess.Any(x => x.CampaignId == campaignId);
        }
    }
}
