using DigitalData.Core.Client.Interface;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System.Net;

//TODO: Update to use IHttpClientFactory (see also: https://learn.microsoft.com/tr-tr/dotnet/core/extensions/httpclient-factory)
namespace DigitalData.Core.Client
{
    public static class DIExtensions
    {
        internal static IServiceCollection AddHttpClientServiceDefaults(this IServiceCollection services)
        {
            services.TryAddSingleton<HttpClient>();
            services.TryAddSingleton<CookieContainer>();
            return services;
        }

        public static IServiceCollection AddHttpClientService<THttpClientOptions>(this IServiceCollection services, IConfigurationSection section)
            where THttpClientOptions : class, IHttpClientOptions
        {
            services.AddHttpClientServiceDefaults();            
            services.TryAddSingleton<IHttpClientService<THttpClientOptions>, HttpClientService<THttpClientOptions>>();
            return services.Configure<THttpClientOptions>(section);
        }

        public static IServiceCollection AddHttpClientService<THttpClientOptions>(this IServiceCollection services, THttpClientOptions options)
            where THttpClientOptions : class, IHttpClientOptions
        {
            services.AddHttpClientServiceDefaults();
            services.TryAddSingleton<IHttpClientService<THttpClientOptions>, HttpClientService<THttpClientOptions>>();
            services.TryAddSingleton(Options.Create(options));
            return services;
        }
    }
}