/*
 * Copyright (c) 2015 - present Nebula Bay.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tascape.reactor.webui.comm;

import com.tascape.reactor.SystemConfiguration;
import java.io.File;
import org.apache.commons.lang3.SystemUtils;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
 * @author linsong wang
 */
public class IE extends WebBrowser {
    private static final Logger LOG = LoggerFactory.getLogger(IE.class);
    
    public static final String SYSPROP_DRIVER = "webdriver.ie.driver";
    
    public static final String DRIVER_NAME = "IEDriverServer.exe";
    
    static {
        if (!SystemUtils.IS_OS_WINDOWS) {
            throw new RuntimeException("Cannot run IE browser on non-Windows platforms.");
        }
        
        String driver = System.getProperty(SYSPROP_DRIVER);
        if (driver == null) {
            File d = SystemConfiguration.HOME_PATH.resolve(DRIVER_DIRECTORY).resolve(DRIVER_NAME).toFile();
            if (d.exists() && d.isFile()) {
                LOG.info("Use " + DRIVER_NAME + " at {}", d.getAbsolutePath());
                System.setProperty(SYSPROP_DRIVER, d.getAbsolutePath());
            } else {
                LOG.warn("Cannot find " + DRIVER_NAME + " file");
                throw new RuntimeException("Cannot find " + DRIVER_NAME + ". Please set system property "
                        + SYSPROP_DRIVER + ", or download " + DRIVER_NAME + " into directory " + d.getParent()
                        + ". Check download page http://selenium-release.storage.googleapis.com/index.html");
            }
        } else {
            LOG.info("Use driver specified by system property {}={}", SYSPROP_DRIVER, driver);
        }
    }
    
    public IE() {
        System.setProperty("webdriver.ie.driver.logfile", super.getLogPath().getParent().resolve(DRIVER_NAME).toString());
        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setJavascriptEnabled(true);
        capabilities.setCapability("ie.ensureCleanSession", true);
        capabilities.setCapability("acceptSslCerts", true);
        capabilities.setCapability("unexpectedAlertBehaviour", "accept");
        capabilities.setCapability("logFile", super.getLogPath().getParent().resolve(DRIVER_NAME + ".log").toString());
        super.setProxy(capabilities);
        super.setLogging(capabilities);
        
        super.setWebDriver(new InternetExplorerDriver(capabilities));
    }

    /**
     * Not support yet.
     *
     * @param url NA
     *
     * @return NA
     *
     * @throws Exception NA
     */
    @Override
    public int getPageLoadTimeMillis(String url) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * Not support yet.
     *
     * @param ajax NA
     *
     * @return NA
     *
     * @throws Exception NA
     */
    @Override
    public int getAjaxLoadTimeMillis(Ajax ajax) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

@@ -1023,16 +1023,19 @@
 rDriver%0A
+ *%0A
  * @auth
@@ -1050,16 +1050,16 @@
 ng wang%0A
-
  */%0Apubl
@@ -1164,21 +1164,17 @@
 class);%0A
-    
 %0A
+
     publ
@@ -1236,21 +1236,17 @@
 river%22;%0A
-    
 %0A
+
     publ
@@ -1304,21 +1304,17 @@
 r.exe%22;%0A
-    
 %0A
+
     stat
@@ -1461,25 +1461,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -2413,21 +2413,17 @@
 %7D%0A    %7D%0A
-    
 %0A
+
     publ
@@ -2485,24 +2485,40 @@
 er.logfile%22,
+%0A               
  super.getLo
@@ -3102,24 +3102,16 @@
 ities);%0A
-        
 %0A       

update IE log file path