using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Net;
using System.Net.Http;

namespace DigitalData.Core.Legacy.Client
{
    public static class DIExtensions
    {
        public static IServiceCollection AddHttpClientService(this IServiceCollection services, string uri)
        {
            services.TryAddSingleton<HttpClient>();
            services.TryAddSingleton<CookieContainer>();
            services.AddSingleton<BaseHttpClientService>();
            services.Configure<HttpClientOptions>(opt => opt.Uri = uri);

            return services;
        }

        public static IServiceCollection AddHttpClientService<TClientOptions>(this IServiceCollection services, Action<TClientOptions> clientOptions = null, bool setAsDefaultBase = false)
            where TClientOptions : HttpClientOptions
        {
            services.TryAddSingleton<HttpClient>();
            services.TryAddSingleton<CookieContainer>();
            services.AddSingleton<HttpClientService<TClientOptions>>();

            if(clientOptions != null)
                services.Configure(clientOptions);

            if (setAsDefaultBase)
                services.AddSingleton<BaseHttpClientService, HttpClientService<TClientOptions>>();

            return services;
        }
    }
}