using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Calls;
using Hotline.DI;
using Hotline.Realtimes;
using Hotline.Share.Enums.CallCenter;
using Hotline.Share.Notifications.NewRockCallCenter;
using MediatR;

namespace Hotline.Application.Handlers.CallCenter.FlowControl
{
    [Injection(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
    public class AnsweredVisitorToExtNotificationHandler : INotificationHandler<AnsweredVisitorToExtNotification>
    {
        private readonly ICallRepository _callRepository;
        private readonly ICallDetailRepository _callDetailRepository;
        private readonly IUserCacheManager _userCacheManager;
        private readonly IRealtimeService _realtimeService;
        private readonly ICallCacheManager _callCacheManager;

        public AnsweredVisitorToExtNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository, IUserCacheManager userCacheManager, IRealtimeService realtimeService, ICallCacheManager callCacheManager)
        {
            _callRepository = callRepository;
            _callDetailRepository = callDetailRepository;
            _userCacheManager = userCacheManager;
            _realtimeService = realtimeService;
            _callCacheManager = callCacheManager;
        }

        public async Task Handle(AnsweredVisitorToExtNotification notification, CancellationToken cancellationToken)
        {
            var model = await _callRepository.GetAsync(
                x => x.ConversationId == notification.Visitor.Id &&
                     x.FromNo == notification.Visitor.From && x.CreationTime >= DateTime.Now.AddHours(-2), true, x => x.CreationTime, cancellationToken);
            var workModel = _userCacheManager.GetWorkByTel(notification.TelNo);
            if (model != null)
            {
                model.CallStatus = ECallStatus.Answered;
                model.ToNoExt = notification.TelNo;
                model.AnsweredTime = DateTime.Now;
                model.UserId = workModel.UserId;
                model.UserName = workModel.UserName;
                await _callRepository.UpdateAsync(model, cancellationToken);
                var detail = new CallDetail()
                {
                    CallId = model.Id,
                    CallStatus = ECallStatus.Answered,
                    OMCallId = notification.Visitor.CallId,
                    ConversationId = notification.Visitor.Id,
                    EventName = notification.Attribute,
                    AnswerNo = notification.TelNo,
                    FromNo = notification.Visitor.From,
                    ToNo = notification.Visitor.To,
                };
                await _callDetailRepository.AddAsync(detail, cancellationToken);

                //处理排队
                _callCacheManager.RemoveCallCache(model.Id);
                await _realtimeService.CallQueueAsync(_callCacheManager.GetCallQueueList(), cancellationToken);

                var callDetail = _callDetailRepository.GetAsync(x => x.CallId == model.Id && x.EventName == "ANSWER");
                if (callDetail == null)
                {
                    //调用业务通知 通知前端
                    await _realtimeService.AnsweredAsync(workModel.UserId, new Share.Dtos.Realtime.AnsweredDto() { Id = model.Id, From = notification.Visitor.From, To = notification.TelNo }, cancellationToken);
                }
            }
        }
    }
}
