using FastGithub.Configuration;
using FastGithub.HttpServer.TcpMiddlewares;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace FastGithub.HttpServer.HttpMiddlewares
{
    /// 
    /// http代理策略中间件
    /// 
    sealed class HttpProxyPacMiddleware
    {
        private readonly FastGithubConfig fastGithubConfig;
        /// 
        /// http代理策略中间件
        /// 
        ///  
        public HttpProxyPacMiddleware(FastGithubConfig fastGithubConfig)
        {
            this.fastGithubConfig = fastGithubConfig;
        }
        /// 
        /// 处理请求
        /// 
        /// 
        /// 
        /// 
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            // http请求经过了httpProxy中间件
            var proxyFeature = context.Features.Get();
            if (proxyFeature != null && proxyFeature.ProxyProtocol == ProxyProtocol.None)
            {
                var proxyPac = this.CreateProxyPac(context.Request.Host);
                context.Response.ContentType = "application/x-ns-proxy-autoconfig";
                context.Response.Headers.Add("Content-Disposition", $"attachment;filename=proxy.pac");
                await context.Response.WriteAsync(proxyPac);
            }
            else
            {
                await next(context);
            }
        }
        /// 
        /// 创建proxypac脚本
        /// 
        /// 
        /// 
        private string CreateProxyPac(HostString proxyHost)
        {
            var buidler = new StringBuilder();
            buidler.AppendLine("function FindProxyForURL(url, host){");
            buidler.AppendLine($"    var fastgithub = 'PROXY {proxyHost}';");
            foreach (var domain in fastGithubConfig.GetDomainPatterns())
            {
                buidler.AppendLine($"    if (shExpMatch(host, '{domain}')) return fastgithub;");
            }
            buidler.AppendLine("    return 'DIRECT';");
            buidler.AppendLine("}");
            return buidler.ToString();
        }
    }
}