package com.codeborne.selenide.drivercommands;

import com.codeborne.selenide.Config;
import com.codeborne.selenide.impl.Cleanup;
import com.codeborne.selenide.proxy.SelenideProxyServer;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class CloseDriverCommand {
  private static final Logger log = LoggerFactory.getLogger(CloseDriverCommand.class);

  public void close(Config config, @Nullable WebDriver webDriver, @Nullable SelenideProxyServer selenideProxyServer) {
    long threadId = Thread.currentThread().getId();
    if (config.holdBrowserOpen()) {
      log.info("Hold browser and proxy open: {} -> {}, {}", threadId, webDriver, selenideProxyServer);
    }
    else if (webDriver != null) {
      log.info("Close webdriver: {} -> {}", threadId, webDriver);
      if (selenideProxyServer != null) {
        log.info("Close proxy server: {} -> {}", threadId, selenideProxyServer);
      }

      long start = System.currentTimeMillis();
      close(webDriver, selenideProxyServer);
      long duration = System.currentTimeMillis() - start;
      log.info("Closed webdriver {} in {} ms", threadId, duration);
    }
    else if (selenideProxyServer != null) {
      log.info("Close proxy server: {} -> {}", threadId, selenideProxyServer);
      selenideProxyServer.shutdown();
    }
  }

  private void close(WebDriver webdriver, @Nullable SelenideProxyServer proxy) {
    try {
      log.info("Trying to close the browser {} ...", webdriver.getClass().getSimpleName());
      webdriver.quit();
    }
    catch (UnreachableBrowserException e) {
      // It happens for Firefox. It's ok: browser is already closed.
      log.debug("Browser is unreachable", e);
    }
    catch (WebDriverException cannotCloseBrowser) {
      log.error("Cannot close browser normally: {}", Cleanup.of.webdriverExceptionMessage(cannotCloseBrowser));
    }

    if (proxy != null) {
      log.info("Trying to shutdown {} ...", proxy);
      proxy.shutdown();
    }
  }
}

@@ -476,16 +476,67 @@
 fault;%0A%0A
+import static java.lang.System.currentTimeMillis;%0A%0A
 @Paramet
@@ -990,34 +990,44 @@
 erver);%0A    
-%7D%0A    else
+  return;%0A    %7D%0A%0A   
  if (webDriv
@@ -1032,32 +1032,72 @@
 iver != null) %7B%0A
+      long start = currentTimeMillis();%0A
       log.info(%22
@@ -1113,32 +1113,35 @@
 driver: %7B%7D -%3E %7B%7D
+...
 %22, threadId, web
@@ -1159,51 +1159,32 @@
     
-if (selenideProxyServer != null) %7B%0A 
+close(webDriver);%0A
       
- 
 log.
@@ -1194,39 +1194,39 @@
 o(%22Close
- proxy ser
+d webdri
 ver
-:
  %7B%7D 
--%3E
+in
  %7B%7D
+ ms
 %22, threa
@@ -1234,109 +1234,53 @@
 Id, 
-selenideProxyServer);%0A      %7D%0A%0A      long start = System.currentTimeMillis();%0A      close(webDriver, 
+currentTimeMillis() - start);%0A    %7D%0A%0A    if (
 sele
@@ -1290,26 +1290,35 @@
 eProxyServer
-);
+ != null) %7B
 %0A      long 
@@ -1321,26 +1321,16 @@
 ong 
-duration = System.
+start = 
 curr
@@ -1344,24 +1344,16 @@
 Millis()
- - start
 ;%0A      
@@ -1367,39 +1367,42 @@
 o(%22Close
-d webdri
+ proxy ser
 ver
+:
  %7B%7D 
-in %7B%7D ms
+-%3E %7B%7D...
 %22, threa
@@ -1410,38 +1410,36 @@
 Id, 
-duration);%0A    %7D%0A    else if (
+selenideProxyServer);%0A      
 sele
@@ -1449,35 +1449,36 @@
 eProxyServer
- != null) %7B
+.shutdown();
 %0A      log.i
@@ -1479,32 +1479,33 @@
  log.info(%22Close
+d
  proxy server: %7B
@@ -1501,26 +1501,28 @@
 y server
-:
  %7B%7D 
--%3E
+in
  %7B%7D
+ ms
 %22, threa
@@ -1530,261 +1530,102 @@
 Id, 
-selenideProxyServer);%0A      selenideProxyServer.shutdown();%0A    %7D%0A  %7D%0A%0A  private void close(WebDriver webdriver, @Nullable SelenideProxyServer proxy) %7B%0A    try %7B%0A      log.info(%22Trying to close the browser %7B%7D ...%22, webdriver.getClass().getSimpleName());
+currentTimeMillis() - start);%0A    %7D%0A  %7D%0A%0A  private void close(WebDriver webdriver) %7B%0A    try %7B
 %0A   
@@ -1909,17 +1909,8 @@
 wser
- normally
 : %7B%7D
@@ -1981,107 +1981,82 @@
   %7D%0A
-%0A
     
-if (proxy != null) %7B%0A      log.info(%22Trying to shutdown %7B%7D ...%22, proxy);%0A      proxy.shutdown(
+catch (RuntimeException e) %7B%0A      log.error(%22Cannot close browser%22, e
 );%0A 

simplify CloseDriverCommand even more