using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Configs;
using Hotline.CallCenter.Devices;
using Hotline.CallCenter.Ivrs;
using Hotline.CallCenter.Tels;
using Hotline.Configurations;
using Hotline.DI;
using Hotline.Share.Notifications.NewRockCallCenter;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Hotline.Application.Handlers.CallCenter.System
{
    [Injection(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
    public class BootupNotificationHandler : INotificationHandler<BootupNotification>
    {
        private readonly ITelGroupRepository _telGroupRepository;
        private readonly IIvrRepository _ivrRepository;
        private readonly IOptionsSnapshot<CallCenterConfiguration> _callcenterOptions;
        private readonly INewRockDeviceManager _newRockDeviceManager;
        private readonly IUserCacheManager _userCacheManager;

        public BootupNotificationHandler(
            IServiceProvider serviceProvider,
            ITelGroupRepository telGroupRepository,
            IUserCacheManager userCacheManager, IIvrRepository ivrRepository,
            IOptionsSnapshot<AppConfiguration> appOptions,
            IOptionsSnapshot<CallCenterConfiguration> callcenterOptions)
        {
            _telGroupRepository = telGroupRepository;
            _userCacheManager = userCacheManager;
            _ivrRepository = ivrRepository;
            _callcenterOptions = callcenterOptions;
            if (appOptions.Value.GetDefaultAppScopeConfiguration().CallCenterType == AppDefaults.CallCenterType.XunShi)
            {
                _newRockDeviceManager = serviceProvider.GetRequiredService<INewRockDeviceManager>();
            }
        }

        public async Task Handle(BootupNotification notification, CancellationToken cancellationToken)
        {
            #region 还原所有分机组配置
            //查询所有分机组
            var list = await _telGroupRepository.QueryExtAsync(d => true, d => d.Includes(x => x.Tels));
            foreach (var groupItem in list)
            {
                List<string> exts = new List<string>();
                foreach (var ext in groupItem.Tels)
                {
                    var iswork = await _userCacheManager.IsWorkingByTelAsync(ext.No, cancellationToken);
                    if (iswork)
                        exts.Add(ext.No);

                }
                //轮循还原设备分机组信息
                await _newRockDeviceManager.AssginConfigGroupAsync(_callcenterOptions.Value.NewRock, groupItem.No, groupItem.Distribution, exts, groupItem.Voice,
                    cancellationToken);
            }

            #endregion

            #region 还原所有语音菜单配置
            //查询所有IVR
            var ivrlist = await _ivrRepository.QueryAsync();
            foreach (var item in ivrlist)
            {
                await _newRockDeviceManager.AssginConfigMenuAsync(_callcenterOptions.Value.NewRock, item.No, item.Voice, item.Repeat.ToString(), item.InfoLength.ToString(), item.Exit, cancellationToken);
            }
            #endregion

            //TODO 有新还原的设置加在后面
        }
    }
}
