package com.sdl.selenium.utils.browsers;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class IExplorerConfigReader extends AbstractBrowserConfigReader {
    private static final Logger LOGGER = LoggerFactory.getLogger(IExplorerConfigReader.class);

    private static final String DEFAULT_CONFIG = "" +
            "\n browser=iexplore" +
            "\n browser.driver.path=src\\\\test\\\\resources\\\\drivers\\\\IEDriverServer.exe";

    public IExplorerConfigReader() {
        this(null);
    }

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

    @Override
    public WebDriver createDriver() throws IOException {
        DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
        ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        ieCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        String driverPath = getProperty("browser.driver.path");
        if (!"".equals(driverPath)) {
            System.setProperty("webdriver.ie.driver", driverPath);
        }
        //setExtraProperty();
        return new InternetExplorerDriver(ieCapabilities);
    }

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

    @Override
    public String getDownloadPath() {
        //http://stackoverflow.com/questions/18510965/how-to-set-internetexplorerdriver-download-directory
        return "";
    }

    private void setExtraProperty() {
        for (Map.Entry<Object, Object> entry : entrySet()) {
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            overWriteWebLocatorConfig(key, value);
        }
        LOGGER.info("The properties was load with success: {}", toString());
    }
}

@@ -1434,24 +1434,26 @@
      %7D%0D%0A
+//
         
 //setExt
@@ -1444,18 +1444,16 @@
         
-//
 setExtra
@@ -1851,24 +1851,26 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+//
     private 
@@ -1892,24 +1892,26 @@
 operty() %7B%0D%0A
+//
         for 
@@ -1956,24 +1956,26 @@
 rySet()) %7B%0D%0A
+//
             
@@ -2009,24 +2009,26 @@
 .getKey();%0D%0A
+//
             
@@ -2066,24 +2066,26 @@
 etValue();%0D%0A
+//
             
@@ -2120,24 +2120,26 @@
 y, value);%0D%0A
+//
         %7D%0D%0A 
@@ -2137,16 +2137,18 @@
      %7D%0D%0A
+//
         
@@ -2213,22 +2213,24 @@
 String());%0D%0A
+//
     %7D%0D%0A%7D%0D%0A

add ieCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);