using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using StellaOps.Concelier.Connector.Cccs.Configuration;
using StellaOps.Concelier.Connector.Cccs.Internal;
using StellaOps.Concelier.Connector.Common.Http;
using StellaOps.Concelier.Connector.Common.Html;

namespace StellaOps.Concelier.Connector.Cccs;

public static class CccsServiceCollectionExtensions
{
    public static IServiceCollection AddCccsConnector(this IServiceCollection services, Action<CccsOptions> configure)
    {
        ArgumentNullException.ThrowIfNull(services);
        ArgumentNullException.ThrowIfNull(configure);

        services.AddOptions<CccsOptions>()
            .Configure(configure)
            .PostConfigure(static options => options.Validate());

        services.AddSourceHttpClient(CccsOptions.HttpClientName, static (sp, clientOptions) =>
        {
            var options = sp.GetRequiredService<IOptions<CccsOptions>>().Value;
            clientOptions.UserAgent = "StellaOps.Concelier.Cccs/1.0";
            clientOptions.Timeout = options.RequestTimeout;
            clientOptions.AllowedHosts.Clear();

            foreach (var feed in options.Feeds.Where(static feed => feed.Uri is not null))
            {
                clientOptions.AllowedHosts.Add(feed.Uri!.Host);
            }

            clientOptions.AllowedHosts.Add("www.cyber.gc.ca");
            clientOptions.AllowedHosts.Add("cyber.gc.ca");
        });

        services.TryAddSingleton<HtmlContentSanitizer>();
        services.TryAddSingleton<CccsDiagnostics>();
        services.TryAddSingleton<CccsHtmlParser>();
        services.TryAddSingleton<CccsFeedClient>();
        services.AddTransient<CccsConnector>();
        return services;
    }
}
