using XF.Domain.Exceptions;
using XF.Domain.Queues;

namespace Hotline.CallCenter.Tels;

/// <summary>
/// 分机组分机队列
/// </summary>
public class TelGroupTelQueue
{
    private readonly IQueue _queue;
    private string _telGroupId;

    public TelGroupTelQueue(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 telNo) => _queue.EnqueueAsync(GetQueueName(), telNo);

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

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

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

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