using DotNetCore.CAP;
using Hotline.CallCenter.Calls;
using Hotline.Orders;
using Hotline.Share.Dtos.TrCallCenter;
using Hotline.Share.Enums.Order;
using Hotline.Share.Mq;
using Hotline.Share.Tools;
using Polly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace Hotline.Application.Subscribers;
public class CallSubscriber : ICapSubscribe, ITransientDependency
{
    private readonly IRepository<CallNative> _callRepository;
    private readonly IRepository<Order> _orderRepository;
    private readonly IRepository<OrderVisit> _orderVisitrepository;
    private readonly IRepository<OrderVisitDetail> _orderVisitDetailRepository;
    private readonly IOrderVisitRepository _orderVisitRepository;

    public CallSubscriber(IRepository<CallNative> callRepository, IRepository<Order> orderRepository, IRepository<OrderVisit> orderVisitrepository, IRepository<OrderVisitDetail> orderVisitDetailRepository, IOrderVisitRepository orderVisitRepository)
    {
        _callRepository = callRepository;
        _orderRepository = orderRepository;
        _orderVisitrepository = orderVisitrepository;
        _orderVisitDetailRepository = orderVisitDetailRepository;
        _orderVisitRepository = orderVisitRepository;
    }

    /// <summary>
    /// 挂断电话
    /// </summary>
    /// <param name="dto"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    /// <exception cref="UserFriendlyException"></exception>
    //[CapSubscribe(EventNames.HotlineCallBye)]
    //public async Task HotlineCallBye(PublishCallRecrodDto dto, CancellationToken cancellationToken)
    //{
    //    var callId = await _orderRepository.Queryable()
    //        .Where(m => m.Id == dto.Order.Id)
    //        .Select(m => m.CallId)
    //        .FirstAsync() ??
    //        throw new UserFriendlyException($"订单Id:{dto.Order.Id} 的 CallId 为空");
    //    var replyTxt = string.Empty;
    //    RetryHelper.Retry(() =>
    //    {
    //        return false;
    //    }, 5, 1000);
    //}

    /// <summary>
    /// 如果有新的电话回复记录, 就更新回访记录
    /// </summary>
    /// <param name="callSatisfactions"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    //[CapSubscribe(EventNames.HotlineCallSatisfactionAdd)]
    //public async Task HotlineCallSatisfactionAdd(List<CallSatisfaction> callSatisfactions, CancellationToken cancellationToken)
    //{
    //    foreach (var callSatisfaction in callSatisfactions)
    //    {
    //        await _orderVisitRepository.UpdateVoiceReplyAsync(callSatisfaction, cancellationToken);
    //    }
    //}
}
