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.CallState
{
    [Injection(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
    public class AlertExtToExtNotificationHandler : INotificationHandler<AlertExtToExtNotification>
    {
        private readonly IRealtimeService _realtimeService;
        private readonly IUserCacheManager _userCacheManager;

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

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