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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotline.Application.Handlers.CallCenter.CallState
{
    public class RingExtToExtNotificationHandler : INotificationHandler<RingExtToExtNotification>
    {
        private readonly IRealtimeService _realtimeService;
        private readonly IUserCacheManager _userCacheManager;


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


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