package com.sdl.selenium.web.utils.browsers;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ChromeConfigReader extends AbstractBrowserConfigReader {

    private static final String DEFAULT_CONFIG = "" +
            "\n browser=chrome" +
            "\n browser.driver.path=src\\\\test\\\\resources\\\\drivers\\\\chromedriver.exe" +
            "\n browser.download.dir=src\\\\test\\\\resources\\\\download\\\\";

    public ChromeConfigReader() {
        this(null);
    }

    public ChromeConfigReader(String resourcePath) {
        super(resourcePath, DEFAULT_CONFIG);
    }

    @Override
    public WebDriver createDriver() throws IOException {
        String driverPath = getProperty("browser.driver.path");
        if (!"".equals(driverPath)) {
            System.setProperty("webdriver.chrome.driver", driverPath);
        }
        ChromeOptions options = new ChromeOptions();
        //options.addArguments("--start-maximized");
        options.addArguments("--lang=en");
        options.addArguments("--allow-running-insecure-content");
        options.addArguments("--enable-logging --v=1");
        options.addArguments("--test-type");
        Map<String, Object> prefs = new HashMap<String, Object>();
        String property = getProperty("browser.download.dir");
        File file = new File(property);
        String downloadDir = file.getCanonicalPath();
        if (!"".equals(downloadDir)) {
            prefs.put("download.default_directory", downloadDir);
        }
        setProfilePreferences(prefs);
        options.setExperimentalOption("prefs", prefs);
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        return new ChromeDriver(capabilities);
    }

    @Override
    public boolean isSilentDownload() {
        return !"".equals(getProperty("browser.download.dir"));
    }

    @Override
    public String getDownloadPath() {
        File file = new File(getProperty("browser.download.dir"));
        return file.getAbsolutePath();
    }

    private void setProfilePreferences(Map<String, Object> prefs) {
        for (Map.Entry<Object, Object> entry : entrySet()) {
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (key.startsWith("profile.preference.")) {
                String preferenceKey = key.substring(19);

                if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
                    prefs.put(preferenceKey, Boolean.valueOf(value));
                } else {
                    try {
                        int intValue = Integer.parseInt(value);
                        prefs.put(preferenceKey, intValue);
                    } catch (NumberFormatException e) {
                        prefs.put(preferenceKey, value);
                    }
                }
            }
        }
    }
}

@@ -234,16 +234,75 @@
 lities;%0D
+%0Aimport org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory;%0D
 %0A%0D%0Aimpor
@@ -469,16 +469,109 @@
 eader %7B%0D
+%0A    private static final Logger LOGGER = LoggerFactory.getLogger(ChromeConfigReader.class);%0D
 %0A%0D%0A    p
@@ -829,16 +829,309 @@
 oad%5C%5C%5C%5C%22
+ +%0D%0A            %22%5Cn options.arguments=--lang=en --allow-running-insecure-content --enable-logging --v=1 --test-type%22 +%0D%0A            %22%5Cn options.experimental.profile.default_content_settings.multiple-automatic-downloads=1%22 +%0D%0A            %22%5Cn options.experimental.download.prompt_for_download=1%22
 ;%0D%0A%0D%0A   
@@ -1627,704 +1627,36 @@
     
-//options.addArguments(%22--start-maximized%22);%0D%0A        options.addArguments(%22--lang=en%22);%0D%0A        options.addArguments(%22--allow-running-insecure-content%22);%0D%0A        options.addArguments(%22--enable-logging --v=1%22);%0D%0A        options.addArguments(%22--test-type%22);%0D%0A        Map%3CString, Object%3E prefs = new HashMap%3CString, Object%3E();%0D%0A        String property = getProperty(%22browser.download.dir%22);%0D%0A        File file = new File(property);%0D%0A        String downloadDir = file.getCanonicalPath();%0D%0A        if (!%22%22.equals(downloadDir)) %7B%0D%0A            prefs.put(%22download.default_directory%22, downloadDir);%0D%0A        %7D%0D%0A        setProfilePreferences(prefs);%0D%0A        options.setExperimentalOption(%22prefs%22, pref
+setProfilePreferences(option
 s);%0D
@@ -2203,36 +2203,121 @@
 ces(
-Map%3CString, Object%3E prefs) %7B
+ChromeOptions options) throws IOException %7B%0D%0A        Map%3CString, Object%3E prefs = new HashMap%3CString, Object%3E();%0D%0A
 %0D%0A  
@@ -2518,26 +2518,137 @@
 th(%22
-profile.preference
+options.arguments%22)) %7B%0D%0A                options.addArguments(value);%0D%0A            %7D else if (key.startsWith(%22options.experimental
 .%22))
@@ -2708,10 +2708,10 @@
 ing(
+2
 1
-9
 );%0D%0A
@@ -3216,32 +3216,32 @@
 %0A            %7D%0D%0A
-
         %7D%0D%0A    %7D
@@ -3231,22 +3231,356 @@
 %0A        %7D%0D%0A
+        String property = getProperty(%22browser.download.dir%22);%0D%0A        File file = new File(property);%0D%0A        String downloadDir = file.getCanonicalPath();%0D%0A        if (!%22%22.equals(downloadDir)) %7B%0D%0A            prefs.put(%22download.default_directory%22, downloadDir);%0D%0A        %7D%0D%0A        options.setExperimentalOption(%22prefs%22, prefs);%0D%0A
     %7D%0D%0A%7D%0D%0A

add ChromeConfigReader possibility to add options.arguments and option.experimental