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


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

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

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