using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.PacketIntercept
{
    /// 
    /// dns拦截后台服务
    /// 
    [SupportedOSPlatform("windows")]
    sealed class DnsInterceptHostedService : BackgroundService
    {
        private readonly IDnsInterceptor dnsInterceptor;
        private readonly IEnumerable conflictSolvers;
        private readonly ILogger logger;
        private readonly IHost host;
        /// 
        /// dns拦截后台服务
        /// 
        /// 
        /// 
        /// 
        /// 
        public DnsInterceptHostedService(
            IDnsInterceptor dnsInterceptor,
            IEnumerable conflictSolvers,
            ILogger logger,
            IHost host)
        {
            this.dnsInterceptor = dnsInterceptor;
            this.conflictSolvers = conflictSolvers;
            this.logger = logger;
            this.host = host;
        }
        /// 
        /// 启动时处理冲突
        /// 
        /// 
        /// 
        public override async Task StartAsync(CancellationToken cancellationToken)
        {          
            foreach (var solver in this.conflictSolvers)
            {
                await solver.SolveAsync(cancellationToken);
            }
            await base.StartAsync(cancellationToken);
        }
        /// 
        /// 停止时恢复冲突
        /// 
        /// 
        /// 
        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            foreach (var solver in this.conflictSolvers)
            {
                await solver.RestoreAsync(cancellationToken);
            }
            await base.StopAsync(cancellationToken);
        }
        /// 
        /// dns后台
        /// 
        /// 
        /// 
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                await this.dnsInterceptor.InterceptAsync(stoppingToken);
            }
            catch (OperationCanceledException)
            {
            }
            catch (Win32Exception ex) when (ex.NativeErrorCode == 995)
            {
            }
            catch (Exception ex)
            { 
                this.logger.LogError(ex, "dns拦截器异常");
                await this.host.StopAsync(stoppingToken);
            }
        }
    }
}