using DataSharing.Share.Dtos.Province;
using Microsoft.Extensions.DependencyInjection;
using XF.Domain.Cache;
using XF.Domain.Dependency;

namespace DataSharing.Province
{
    public class XieTongClient : ProvinceClient, ISingletonDependency, ISelfDependency
    {
        private readonly IServiceScopeFactory _scopeFactory;

        public XieTongClient(IServiceScopeFactory scopeFactory) : base(scopeFactory)
        {
            _scopeFactory = scopeFactory;
        }

        public Task<TResponse?> RequestAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
            where TRequest : IProvinceRequest, new()
        {
            using var scope = _scopeFactory.CreateScope();
            var provider = scope.ServiceProvider;
            var channelconfigManager = provider.GetRequiredService<IChannelConfigurationManager>();

            var configProvince = channelconfigManager.GetConfigurationProvince();
            return base.RequestAsync<TRequest, TResponse>(request, configProvince.XieTong, configProvince, cancellationToken);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="url"></param>
        /// <param name="httpMethod"></param>
        /// <param name="stringContent"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<TResponse?> RequestAsync<TResponse>(string url, string httpMethod, string? stringContent = null, CancellationToken cancellationToken = default)
        {
            using var scope = _scopeFactory.CreateScope();
            var provider = scope.ServiceProvider;
            var channelconfigManager = provider.GetRequiredService<IChannelConfigurationManager>();
            //var httpInvoker = provider.GetRequiredService<IHttpInvoker>();
            //var cacheToken = provider.GetRequiredService<ITypedCache<TokenInfo>>();

            var configProvince = channelconfigManager.GetConfigurationProvince();
            return base.RequestAsync<TResponse>(configProvince.XieTong, configProvince, url, httpMethod, stringContent, cancellationToken);
        }

        /// <summary>
        /// 附件获取Token
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> GetTokenAsync(CancellationToken cancellationToken)
        {
            using var scope = _scopeFactory.CreateScope();
            var provider = scope.ServiceProvider;
            var channelconfigManager = provider.GetRequiredService<IChannelConfigurationManager>();

            var configProvince = channelconfigManager.GetConfigurationProvince();
            var token = await GetToken(configProvince.HuiJu, cancellationToken);
            return token?.AccessToken ?? string.Empty;
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> GetTokenXieTongAsync(CancellationToken cancellationToken)
        {
            using var scope = _scopeFactory.CreateScope();
            var provider = scope.ServiceProvider;
            var channelconfigManager = provider.GetRequiredService<IChannelConfigurationManager>();

            var configProvince = channelconfigManager.GetConfigurationProvince();
            var token = await GetTokenAsync(configProvince.XieTong, cancellationToken);
            var cacheToken = provider.GetRequiredService<ITypedCache<TokenInfo>>();
            string cacheKey = "KeyToken" + "_XieTong";

            await cacheToken.SetAsync(cacheKey, token, TimeSpan.FromMinutes(28));
            var info = cacheToken.GetAsync(cacheKey, cancellationToken).GetAwaiter().GetResult().AccessToken;
            return info;

        }
    }
}
