using DotNetBrowser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/**
 * The sample demonstrates how to configure Browser instance to use custom proxy settings.
 * By default Browser instance uses system proxy settings.
 */
namespace ProxySample
{
    class Program
    {
        static void Main(string[] args)
        {
            String dataDir = Path.GetFullPath("chromium-data");
            BrowserContextParams contextParams = new BrowserContextParams(dataDir);

            // Browser will automatically detect proxy settings.
            //    contextParams.ProxyConfig = new AutoDetectProxyConfig();

            // Browser will not use a proxy server.
            //    contextParams.ProxyConfig = new DirectProxyConfig();

            // Browser will use proxy settings received from proxy auto-config (PAC) file.
            //contextParams.ProxyConfig = new URLProxyConfig("<pac-file-url>");

            // Browser will use custom user's proxy settings.
            String proxyRules = "http=foo:80;https=foo:80;ftp=foo:80;socks=foo:80";
            String exceptions = "<local>";  // bypass proxy server for local web pages
            contextParams.ProxyConfig = new CustomProxyConfig(proxyRules, exceptions);

            // Creates Browser instance with context configured to use specified proxy settings.
            Browser browser = BrowserFactory.Create(new BrowserContext(contextParams));
            // Handle proxy authorization.
            browser.Context.NetworkService.NetworkDelegate = new MyNetworkDelegate();
        }

        private class MyNetworkDelegate : DefaultNetworkDelegate
        {
            public override bool OnAuthRequired(AuthRequiredParams parameters)
            {
                // If proxy server requires login/password, provide it programmatically.
                if (parameters.IsProxy)
                {
                    parameters.Username = "proxy-username";
                    parameters.Password = "proxy-password";
                    return false;
                }
                return true;
            }
        }
    }

}