using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Configs;
using Hotline.CallCenter.Devices;
using Hotline.CallCenter.Tels;
using Hotline.Configurations;
using Hotline.DI;
using Hotline.Realtimes;
using Hotline.Share.Enums.CallCenter;
using Hotline.Share.Notifications.NewRockCallCenter;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using XF.Domain.Cache;
using XF.Domain.Exceptions;

namespace Hotline.Application.Handlers.CallCenter.ExtState
{
    [Injection(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
    public class IdleNotificationHandler : INotificationHandler<IdleNotification>
    {
        private readonly ITelRepository _telRepository;
        private readonly ITelCacheManager _telCacheManager;
        private readonly ITypedCache<Tel> _typedCache;
        private readonly IUserCacheManager _userCacheManager;
        private readonly IRealtimeService _realtimeService;
        private readonly INewRockDeviceManager _newRockDeviceManager;
        private readonly ITelRestRepository _telRestRepository;
        private readonly IOptionsSnapshot<AppConfiguration> _appOptions;
        private readonly IOptionsSnapshot<CallCenterConfiguration> _callcenterOptions;

        public IdleNotificationHandler(
            IServiceProvider serviceProvider,
            ITelRepository telRepository, ITelCacheManager telCacheManager, 
            ITypedCache<Tel> typedCache,IUserCacheManager userCacheManager,
            IRealtimeService realtimeService,
            ITelRestRepository telRestRepository,
            IOptionsSnapshot<AppConfiguration> appOptions,
            IOptionsSnapshot<CallCenterConfiguration> callcenterOptions)
        {
            _telRepository = telRepository;
            _telCacheManager = telCacheManager;
            _typedCache = typedCache;
            _userCacheManager = userCacheManager;
            _realtimeService = realtimeService;
            _telRestRepository = telRestRepository;
            _appOptions = appOptions;
            _callcenterOptions = callcenterOptions;
            if (appOptions.Value.GetDefaultAppScopeConfiguration().CallCenterType == AppDefaults.CallCenterType.XunShi)
            {
                _newRockDeviceManager = serviceProvider.GetRequiredService<INewRockDeviceManager>();
            }
        }

        public async Task Handle(IdleNotification notification, CancellationToken cancellationToken)
        {
            var telModel = _telCacheManager.GetTel(notification.TelNo);
            telModel.TelStatus = ETelStatus.Ready;
            //await _telRepository.UpdateAsync(telModel, cancellationToken);
            _typedCache.Set(notification.TelNo, telModel);



            var iswork = await _userCacheManager.IsWorkingByTelAsync(notification.TelNo, cancellationToken);


            if (!iswork)
                throw new UserFriendlyException(notification.TelNo + "未查询到工作记录");


            var workModel = _userCacheManager.GetWorkByTel(notification.TelNo);
            if (workModel != null)
            {
                await _realtimeService.IdleAsync(workModel.UserId, cancellationToken);
            }

            var restingTel = await _telRestRepository.GetAsync(d => d.TelId == telModel.Id && !d.EndTime.HasValue && d.ApplyStatus== ETelRestApplyStatus.Resting, cancellationToken);
            if (restingTel == null)
            {
                foreach (var group in telModel.Groups)
                {
                    await _newRockDeviceManager.AssginConfigGroupAsync(_callcenterOptions.Value.NewRock, group.No, group.Distribution, new List<string>() { notification.TelNo }, group.Voice, cancellationToken);
                }
            }
        }
    }
}
