using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network;

namespace Titanium.Web.Proxy.IntegrationTests.Setup
{
    public class TestProxyServer : IDisposable
    {
        public ProxyServer ProxyServer { get; private set; }
        public int ListeningPort => ProxyServer.ProxyEndPoints[0].Port;
        public CertificateManager CertificateManager => ProxyServer.CertificateManager;

        public TestProxyServer(bool isReverseProxy, ProxyServer upStreamProxy = null)
        {
            ProxyServer = new ProxyServer();
            ProxyEndPoint explicitEndPoint = isReverseProxy ?
                (ProxyEndPoint)new TransparentProxyEndPoint(IPAddress.Any, 0, true) :
                new ExplicitProxyEndPoint(IPAddress.Any, 0, true);

            ProxyServer.AddEndPoint(explicitEndPoint);

            if (upStreamProxy != null)
            {
                ProxyServer.UpStreamHttpProxy = new ExternalProxy()
                {
                    HostName = "localhost",
                    Port = upStreamProxy.ProxyEndPoints[0].Port
                };

                ProxyServer.UpStreamHttpsProxy = new ExternalProxy()
                {
                    HostName = "localhost",
                    Port = upStreamProxy.ProxyEndPoints[0].Port
                };
            }

            ProxyServer.Start();
        }

        public void Dispose()
        {
            ProxyServer.Stop();
            ProxyServer.Dispose();
        }
    }
}
