using Hotline.CallCenter.Configs;
using Hotline.CallCenter.Devices;
using Hotline.Configurations;
using Hotline.Share.Requests;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using XF.Domain.Dependency;

namespace Hotline.CallCenter.Manage
{
    public class VoiceFileDomainService:IVoiceFileDomainService, IScopeDependency
    {
        private readonly INewRockDeviceManager _newRockDeviceManager;
        private readonly IOptionsSnapshot<AppConfiguration> _appOptions;
        private readonly IOptionsSnapshot<CallCenterConfiguration> _callcenterOptions;

        public VoiceFileDomainService(
            IServiceProvider serviceProvider,
            IOptionsSnapshot<AppConfiguration> appOptions,
            IOptionsSnapshot<CallCenterConfiguration> callcenterOptions)
        {
            _appOptions = appOptions;
            _callcenterOptions = callcenterOptions;
            if (appOptions.Value.GetDefaultAppScopeConfiguration().CallCenterType == AppDefaults.CallCenterType.XunShi)
            {
                _newRockDeviceManager = serviceProvider.GetRequiredService<INewRockDeviceManager>();
            }
        }

        /// <summary>
        /// 查询语音文件
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<string>> VoiceQueryListAsync(CancellationToken cancellationToken)
        {
            var result = await _newRockDeviceManager.VoiceQueryListAsync(_callcenterOptions.Value.NewRock, cancellationToken);
            var list =  result.Replace(".dat","").Replace(".pcm","").Split("|").ToList();

            return list.Distinct().ToList();
        }

        /// <summary>
        /// 删除语音文件（多个文件使用“|”分隔）
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task RemoveVoiceFileAsync(RemoveVoiceFileRequest request, CancellationToken cancellationToken)
        {
            await _newRockDeviceManager.RemoveVoiceFileAsync(_callcenterOptions.Value.NewRock, request.VoiceFileNames, cancellationToken);
        }


    }
}
