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;
using Serilog.Enrichers.Span;

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

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

        public async Task Handle(ByeVisitorOffNotification 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);
            if (model != null)
            {
                model.CallStatus = ECallStatus.Bye;
                model.EndBy = EEndBy.To;
                model.RingOffType = ERingOffType.Normal;
                model.ByeTime = DateTime.Now;
                await _callRepository.UpdateAsync(model, cancellationToken);
                var detail = new CallDetail()
                {
                    CallId = model.Id,
                    CallStatus = ECallStatus.Bye,
                    OMCallId = notification.Visitor.CallId,
                    ConversationId = notification.Visitor.Id,
                    EventName = notification.Attribute,
                    FromNo = notification.Visitor.From,
                    ToNo = notification.Visitor.To,
                    Recording = notification.Recording,
                };
                await _callDetailRepository.AddAsync(detail, cancellationToken);

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

                //查询应答分机号
                var callDetailAnswer = await _callDetailRepository.GetAsync(x => x.CallId == model.Id && x.EventName == "ANSWER", true, x => x.CreationTime, cancellationToken);
                if (callDetailAnswer != null)
                {
                    //调用业务通知 通知前端
                    var workModel = _userCacheManager.GetWorkByTel(callDetailAnswer.AnswerNo);
                    if (workModel != null)
                    {
                        await _realtimeService.ByeAsync(workModel.UserId, new Share.Dtos.Realtime.ByeDto() { Id = model.Id }, cancellationToken);
                    }
                }
            }
        }
    }
}
