using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Exceptions;
using XF.Domain.Queues;

namespace Hotline.CallCenter.Tels
{
    /// <summary>
    /// 分机组呼叫队列
    /// </summary>
    public class TelGroupCallQueue
    {
        private readonly IQueue _queue;
        private string _telGroupId;

        public TelGroupCallQueue(IQueue queue)
        {
            _queue = queue;
        }

        public void Init(string telGroupId)
        {
            if (string.IsNullOrEmpty(telGroupId))
                throw new ArgumentNullException(nameof(telGroupId));
            _telGroupId = telGroupId;
        }

        public void Init(TelGroup telGroup)
        {
            if (string.IsNullOrEmpty(telGroup.Id))
                throw new ArgumentNullException(nameof(telGroup.Id));
            _telGroupId = telGroup.Id;
        }

        public Task EnqueueAsync(string callId) => _queue.EnqueueAsync(GetQueueName(), callId);

        public Task<string> DequeueAsync() => _queue.DequeueAsync<string>(GetQueueName());

        public Task RemoveAsync(string callId) => _queue.RemoveAsync(GetQueueName(), callId);

        public long Count() => _queue.Count(GetQueueName());

        private string GetQueueName()
        {
            if (string.IsNullOrEmpty(_telGroupId))
                throw new UserFriendlyException("先调用Init", "未指定分机组");
            return $"TelGroupTelQueue_{_telGroupId}";
        }
    }
}
