using Hotline.Caching.Interfaces;
using Hotline.DI;
using Hotline.Realtimes;
using Hotline.Share.Dtos.Realtime;
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 AnswerExtToExtNoificationHandler : INotificationHandler<AnswerExtToExtNotification>
    {
        private readonly IUserCacheManager _userCacheManager;
        private readonly IRealtimeService _realtimeService;


        public AnswerExtToExtNoificationHandler(IUserCacheManager userCacheManager, IRealtimeService realtimeService)
        {
            _userCacheManager = userCacheManager;
            _realtimeService = realtimeService;
        }

        public async Task Handle(AnswerExtToExtNotification notification, CancellationToken cancellationToken)
        {
            //通知前端主叫
            //获取主叫工作信息
            var fromWork = _userCacheManager.GetWorkByTel(notification.FromTelNo);
            if (fromWork != null)
            {
                await _realtimeService.AnsweredAsync(fromWork.UserId, new AnsweredDto() { Id = notification.FromTelNo, From = notification.FromTelNo, To = notification.ToTelNo, CallType = ECallType.ExtToExt }, cancellationToken);
            }
            //通知前端被叫
            var toWork = _userCacheManager.GetWorkByTel(notification.ToTelNo);
            if (toWork != null)
            {
                await _realtimeService.AnsweredAsync(toWork.UserId, new AnsweredDto() { Id = notification.ToTelNo, From = notification.FromTelNo, To = notification.ToTelNo, CallType = ECallType.ExtToExt }, cancellationToken);
            }
        }
    }
}
