package io.enmasse.systemtest;

import com.paulhammant.ngwebdriver.ByAngular;
import com.paulhammant.ngwebdriver.NgWebDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertNotNull;

public abstract class SeleniumTestBase extends TestBaseWithDefault {
    private WebDriver driver;
    private NgWebDriver angularDriver;
    private WebBrowserType browserType;
    private String baseWindowID;

    public SeleniumTestBase() {
        this.browserType = getWebBrowserType();
    }

    protected abstract WebBrowserType getWebBrowserType();

    @Before
    public void setupDriver() {
        switch (browserType) {
            case FIREFOX:
                FirefoxOptions firefoxOpts = new FirefoxOptions();
                firefoxOpts.setHeadless(true);
                this.driver = new FirefoxDriver(firefoxOpts);
                break;
            case CHROME:
                ChromeOptions chromeOpts = new ChromeOptions();
                chromeOpts.setHeadless(true);
                this.driver = new ChromeDriver(chromeOpts);
                break;
            default:
                break;
        }
        angularDriver = new NgWebDriver((JavascriptExecutor) driver);
    }

    @After
    public void tearDownDrivers() {
        driver.close();
        try {
            driver.quit();
        } catch (Exception ex) {
            Logging.log.warn("Raise exception on quit: ");
            ex.printStackTrace();
        }
        Logging.log.info("Driver is closed");
        driver = null;
        angularDriver = null;
        baseWindowID = null;
    }

    protected WebDriver getDriver() {
        return this.driver;
    }

    protected String getConsoleRoute() {
        String consoleRoute = String.format("https://%s:%s@%s", username, password,
                openShift.getRouteEndpoint(defaultAddressSpace.getNamespace(), "console"));
        Logging.log.info(consoleRoute);
        return consoleRoute;
    }

    //================================== Common console methods ======================================
    protected void openConsolePageWebConsole() throws Exception {
        driver.get(getConsoleRoute());
        angularDriver.waitForAngularRequestsToFinish();
        Logging.log.info("Console page opened");
    }

    protected WebElement getLeftMenuItemWebConsole(String itemText) throws Exception {
        List<WebElement> items = driver.findElements(ByAngular.exactRepeater("item in items"));
        assertNotNull(items);
        WebElement returnedItem = null;
        for (WebElement item : items) {
            Logging.log.info("Got item: " + item.getText());
            if (item.getText().equals(itemText))
                returnedItem = item;
        }
        return returnedItem;
    }

    protected void openAddressesPageWebConsole() throws Exception {
        WebElement addressesItem = getLeftMenuItemWebConsole("Addresses");
        assertNotNull(addressesItem);
        addressesItem.click();
        Logging.log.info("Clicked on addresses button");
        angularDriver.waitForAngularRequestsToFinish();
        Logging.log.info("Page addresses loaded");
    }

    protected void switchToSubWindow() throws Exception {
        baseWindowID = driver.getWindowHandle();
        String subWindowHandler = null;
        Set<String> windows = driver.getWindowHandles();
        Iterator<String> iterator = windows.iterator();

        while (iterator.hasNext()) {
            subWindowHandler = iterator.next();
        }
        driver.switchTo().window(subWindowHandler);
        angularDriver.waitForAngularRequestsToFinish();
        Logging.log.info("Switched to sub window");
    }

    protected void createAddressWebConsole(Destination destination) throws Exception {
        //get console page
        openConsolePageWebConsole();

        //get addresses item from left panel view
        openAddressesPageWebConsole();

        //click on create button
        WebElement createButton = driver.findElement(ByAngular.buttonText("Create"));
        Logging.log.info("Get the button: " + createButton.getText());
        createButton.click();
        angularDriver.waitForAngularRequestsToFinish();
        Logging.log.info("Clicked on create button");

        //switch to sub window
        switchToSubWindow();

        //fill address name
        WebElement input = ((FirefoxDriver) driver).findElementByCssSelector("#new-name");
        input.sendKeys("test-" + destination.getType());

        //select address type
        WebElement queueInput = ((FirefoxDriver) driver).findElementById(destination.getType());
        queueInput.click();

        //click on next button
        WebElement nextButton = ((FirefoxDriver) driver).findElementById("nextButton");
        nextButton.click();
        angularDriver.waitForAngularRequestsToFinish();
        Logging.log.info("Next button clicked");

        //click on next button
        nextButton.click();
        angularDriver.waitForAngularRequestsToFinish();
        Logging.log.info("Next button clicked");

        //click on create button
        nextButton.click();
        angularDriver.waitForAngularRequestsToFinish();
        Logging.log.info("Create button clicked");

        //switch to base page
        driver.switchTo().window(baseWindowID);

        TestUtils.waitForDestinationsReady(addressApiClient, defaultAddressSpace,
                new TimeoutBudget(5, TimeUnit.MINUTES), destination);
    }
}

@@ -168,16 +168,47 @@
 Before;%0A
+import org.openqa.selenium.By;%0A
 import o
@@ -4841,33 +4841,15 @@
 ut =
- ((FirefoxDriver)
  driver
-)
 .fin
@@ -4860,11 +4860,13 @@
 ment
+(
 By
-C
+.c
 ssSe
@@ -4884,16 +4884,17 @@
 w-name%22)
+)
 ;%0A      
@@ -5010,33 +5010,15 @@
 ut =
- ((FirefoxDriver)
  driver
-)
 .fin
@@ -5025,19 +5025,21 @@
 dElement
+(
 By
-I
+.i
 d(destin
@@ -5050,24 +5050,25 @@
 n.getType())
+)
 ;%0A        qu
@@ -5152,33 +5152,15 @@
 on =
- ((FirefoxDriver)
  driver
-)
 .fin
@@ -5171,11 +5171,13 @@
 ment
+(
 By
-I
+.i
 d(%22n
@@ -5183,24 +5183,25 @@
 nextButton%22)
+)
 ;%0A        ne

Fix made create test universal for all browsers