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

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

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

        public async Task Handle(ByeVisitorAndMenuNotification 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), cancellationToken);
            if (model != null)
            {
                model.CallStatus = ECallStatus.Bye;
                model.EndBy = EEndBy.To;
                model.RingOffType = ERingOffType.Normal;
                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,
                    Remark = notification.Menu.Id
                };
                await _callDetailRepository.AddAsync(detail, cancellationToken);

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