using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.Dns
{
    /// 
    /// dns后台服务
    /// 
    sealed class DnsOverUdpHostedService : BackgroundService
    {
        private readonly DnsOverUdpServer dnsOverUdpServer;
        private readonly IEnumerable conflictValidators;
        private readonly ILogger logger;
        /// 
        /// dns后台服务
        /// 
        /// 
        /// 
        /// 
        public DnsOverUdpHostedService(
            DnsOverUdpServer dnsOverUdpServer,
            IEnumerable conflictValidators,
            ILogger logger)
        {
            this.dnsOverUdpServer = dnsOverUdpServer;
            this.conflictValidators = conflictValidators;
            this.logger = logger;
        }
        /// 
        /// 启动dns
        /// 
        /// 
        /// 
        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            try
            {
                const int DNS_PORT = 53;
                this.dnsOverUdpServer.Listen(IPAddress.Any, DNS_PORT);
                this.logger.LogInformation($"已监听udp端口{DNS_PORT},DNS服务启动完成");
            }
            catch (Exception ex)
            {
                var builder = new StringBuilder().AppendLine($"DNS服务启动失败({ex.Message}),你可以选择如下的一种操作:");
                builder.AppendLine($"1. 关闭占用udp53端口的进程然后重新打开本程序");
                builder.AppendLine($"2. 配置系统或浏览器使用DNS over HTTPS:https://127.0.0.1/dns-query");
                builder.AppendLine($"3. 向系统hosts文件添加github相关域名的ip为127.0.0.1");
                builder.Append($"4. 在局域网其它设备上运行本程序,然后将本机DNS设置为局域网设备的IP");
                this.logger.LogError(builder.ToString());
            }
            foreach (var item in this.conflictValidators)
            {
                await item.ValidateAsync();
            }
            await base.StartAsync(cancellationToken);
        }
        /// 
        /// dns后台
        /// 
        /// 
        /// 
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            return this.dnsOverUdpServer.HandleAsync(stoppingToken);
        }
        /// 
        /// 停止dns服务
        /// 
        /// 
        /// 
        public override Task StopAsync(CancellationToken cancellationToken)
        {
            this.dnsOverUdpServer.Dispose();
            return base.StopAsync(cancellationToken);
        }
    }
}