package com.overloop.automation.pages;

import com.overloop.automation.framework.AbstractPage;
import org.openqa.selenium.*;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.yandex.qatools.allure.annotations.Attachment;

import java.util.List;
import java.util.Objects;

public class WorkstationPage extends AbstractPage {

    private String WORKSTATION_URL = "https://workstation.theorchard.com";
    private String PROJECT_URL = "project/3954968/product/2351281";
    private final int TIMEOUT = 10;

    private String results="";
    private String runUser="";

    @FindBy(id = "edit-username")
    WebElement usernameField;
    @FindBy(id = "edit-password")
    WebElement passwordField;
    @FindBy(id = "edit-submit")
    WebElement submit;
    @FindBy(className = "Opm-digital-product-navigation-steps")
    WebElement step;
    @FindBy(className = "LoadingIndicator-outer")
    WebElement loading;



    public WorkstationPage(WebDriver driver) {
        super(driver);
        PageFactory.initElements(driver, this);
    }


    public void navigatesToWorkstation() {
        getDriver().navigate().to(WORKSTATION_URL);
    }

    public void navigatesToProject() {
        getDriver().navigate().to(WORKSTATION_URL + "/" + PROJECT_URL);
        waitUntilElementNotDisplayed(loading, getDriver());
    }

    public void loginToWorkstation(String username, String password) {
        runUser=username;
        usernameField.click();
        usernameField.sendKeys(username);
        passwordField.click();
        passwordField.sendKeys(password);
        long start = System.currentTimeMillis();
        submit.click();
        long finish = System.currentTimeMillis();
        long totalTime = finish - start;
        attachMetrics("Login time", String.valueOf(totalTime));
    }

    public void clickOnTabs() {
        StringBuilder sb = new StringBuilder();
        List<WebElement> steps = step.findElements(By.className("Opm-digital-product-navigation-step"));
        for (WebElement element : steps) {
            long start = System.currentTimeMillis();
            element.click();
            waitUntilElementNotDisplayed(loading, getDriver());
            long finish = System.currentTimeMillis();
            long totalTime = finish - start;
            System.out.println("Total " + element.getText() + " tab load time - " + totalTime + " milliseconds");
            sb.append("Total " + element.getText() + " tab load time - " + totalTime + " milliseconds \n");
        }
        results = sb.toString();
        attachMetrics(runUser, results);
    }


    public void waitUntilElementNotDisplayed(final WebElement webElement, WebDriver driver) {
        WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);
        ExpectedCondition elementIsDisplayed = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver arg0) {
                try {
                    webElement.isDisplayed();
                    return false;
                } catch (NoSuchElementException e) {
                    return true;
                } catch (StaleElementReferenceException f) {
                    return true;
                }
            }
        };
        wait.until(elementIsDisplayed);
    }

    public void reload() {
        navigatesToProject();
    }

    @Attachment(value = "{0}", type = "text/plain")
    public String attachMetrics(String title, String time) {
        return time;
    }
}
