using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PARR.Core.Common.Interfaces;
using PARR.Core.Common.Interfaces.RabbitServices;
using PARR.DAL.InfluxDbServices;
using PARR.Domain.Settings;
using PARR.Infrastructure.Interval;
using PARR.Infrastructure.Rabbit;
using PARR.Infrastructure.Redis;
using StackExchange.Redis;

namespace PARR.Infrastructure
{
    public static class DependencyInjection
    {
        /// <summary>
        /// Регистрация инфраструктурных сервисов
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
        {
            // тут регистрируем все внешние сервисы, Rabbit, Redis, Email...

            #region Rabbit

            services.AddHttpClient<IRabbitAdminService, RabbitAdminService>();
            services.AddTransient<IRabbitService, RabbitService>();

            #endregion

            #region Interval

            services.AddTransient<IIntervalService, IntervalService>();

            #endregion

            #region Redis

            services.AddSingleton<IConnectionMultiplexer>(sp =>
            {
                // IConnectionMultiplexer - для нативных операций Redis
                var connectionString = configuration.GetConnectionString("RedisConnection");
                return ConnectionMultiplexer.Connect(connectionString);
            });

            services.AddStackExchangeRedisCache(opt =>
            {
                opt.Configuration = configuration.GetConnectionString("RedisConnection");
            });

            services.AddSingleton<IRedisCacheService, RedisCacheService>();

            #endregion


            #region InfluxDb

            var influxDbSettings = new InfluxDbSettings();
            configuration.GetSection(nameof(InfluxDbSettings)).Bind(influxDbSettings);
            services.AddSingleton(influxDbSettings);

            services.AddTransient<IInfluxDbService, InfluxDbService>();

            #endregion


            //services.AddTransient<IEmailService, EmailService>();
            //services.AddTransient<IRabbitService, RabbitService>();
            ////Redis (singleton)

            return services;
        }
    }
}
