package org.selenide.examples;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.SelenideConfig;
import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.proxy.SelenideProxyServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.Testcontainers;
import org.testcontainers.containers.BrowserWebDriverContainer;

import java.io.File;
import java.io.FileNotFoundException;

import static com.codeborne.selenide.FileDownloadMode.HTTPGET;
import static com.codeborne.selenide.FileDownloadMode.PROXY;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.files.FileFilters.withName;
import static org.junit.Assert.assertTrue;

public class DownloadTestWithDockerAndProxy {

    private static final int proxyPort = 8864;

    private SelenideProxyServer proxyServer;

    static {
        Testcontainers.exposeHostPorts(proxyPort);
    }

    //if your baseUrl is http, you'll need to use new Proxy().setHttpProxy
    @Rule
    public BrowserWebDriverContainer chrome =
            new BrowserWebDriverContainer()
                    .withCapabilities(new ChromeOptions().setProxy(new Proxy()
                            .setSslProxy("host.testcontainers.internal:" + proxyPort))
                            .setAcceptInsecureCerts(true));

    @Before
    public void setUp() {
        Configuration.proxyHost = "host.testcontainers.internal";
        Configuration.proxyPort = proxyPort;
        Configuration.proxyEnabled = true;
        Configuration.fileDownload = PROXY;

        proxyServer = new SelenideProxyServer(new SelenideConfig(), null);
        proxyServer.start();

        RemoteWebDriver driver = chrome.getWebDriver();
        WebDriverRunner.setWebDriver(driver, proxyServer);
    }

    @Test
    public void search() throws FileNotFoundException {
        open("https://mvnrepository.com/artifact/com.codeborne/selenide/5.24.2");

        File selenideJar = $("#maincontent .grid ").find(By.linkText("jar")).download(withName("selenide-5.24.2.jar"));
        assertTrue(selenideJar.exists());
    }

    @After
    public void tearDown() {
        proxyServer.shutdown();
        WebDriverRunner.closeWebDriver();
        Configuration.fileDownload = HTTPGET;
        Configuration.proxyEnabled = false;
        Configuration.proxyHost = null;
        Configuration.proxyPort = 0;
    }
}

@@ -1660,23 +1660,153 @@
 on.proxy
-Host = 
+Enabled = true;%0A        Configuration.fileDownload = PROXY;%0A%0A        SelenideConfig config = new SelenideConfig()%0A            .proxyHost(
 %22host.te
@@ -1827,17 +1827,17 @@
 nternal%22
-;
+)
 %0A       
@@ -1833,37 +1833,28 @@
 l%22)%0A        
-Configuration
+    
 .proxyPort =
@@ -1851,19 +1851,17 @@
 roxyPort
- = 
+(
 proxyPor
@@ -1861,17 +1861,17 @@
 roxyPort
-;
+)
 %0A       
@@ -1867,37 +1867,28 @@
 rt)%0A        
-Configuration
+    
 .proxyEnable
@@ -1892,62 +1892,16 @@
 bled
- = 
+(
 true
+)
 ;%0A
-        Configuration.fileDownload = PROXY;%0A%0A
     
@@ -1942,36 +1942,22 @@
 yServer(
-new SelenideC
+c
 onfig
-()
 , null);

update test setup after upgrading to Selenide 5.23.0