using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.Scanner
{
    /// 
    /// 域名与ip关系工厂
    /// 
    [Service(ServiceLifetime.Singleton)]
    sealed class DomainAddressFacotry
    {
        private readonly IEnumerable providers;
        /// 
        /// 域名与ip关系工厂
        /// 
        /// 
        public DomainAddressFacotry(IEnumerable providers)
        {
            this.providers = providers.OrderBy(item => item.Order);
        }
        /// 
        /// 创建域名与ip的关系
        /// 
        /// 
        public async Task> CreateDomainAddressesAsync(CancellationToken cancellationToken)
        {
            var hashSet = new HashSet();
            foreach (var provider in this.providers)
            {
                var domainAddresses = await provider.CreateDomainAddressesAsync(cancellationToken);
                foreach (var item in domainAddresses)
                {
                    hashSet.Add(item);
                }
            }
            return hashSet;
        }
    }
}