package ru.ibs.tests;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class TestTrainingAppline {

    private WebDriver driver;
    private WebDriverWait wait;
    Duration default_timeout = Duration.ofSeconds(10);
    Duration default_sleep = Duration.ofMillis(500);
    String fieldXPath = "//input[@id='%s']";

    @Before
    public void before() {
        // win
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");

        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
        driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(30));

        wait = new WebDriverWait(driver, default_timeout, default_sleep);

        String baseUrl = "http://training.appline.ru/user/login";
        driver.get(baseUrl);
    }

    @Test
    public void applineLoginScenario() throws InterruptedException {

        WebElement usernameField = driver.findElement(By.xpath(String.format(fieldXPath, "prependedInput")));
        fillInputField(usernameField, "Irina Filippova");

        WebElement passwordField = driver.findElement(By.xpath(String.format(fieldXPath, "prependedInput2")));
        fillInputField(passwordField, "testing");

        // Нажать на кнопку Войти
        String buttonLocator = "//button[@type='submit']";
        WebElement loginButton = driver.findElement(By.xpath(buttonLocator));
        scrollToElementJs(loginButton);
        waitUtilElementToBeClickable(loginButton);
        loginButton.click();

        Thread.sleep(5000);
    }

    @After
    public void after() {
        driver.quit();
    }

    private void scrollToElementJs(WebElement element) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
        javascriptExecutor.executeScript("arguments[0].scrollIntoView(true);", element);
    }

    private void waitUtilElementToBeClickable(WebElement element) {
        wait.until(ExpectedConditions.elementToBeClickable(element));
    }

    private void waitUtilElementToBeVisible(By locator) {
        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

    private void waitUtilElementToBeVisible(WebElement element) {
        wait.until(ExpectedConditions.visibilityOf(element));
    }

    private void fillInputField(WebElement element, String value) {
        scrollToElementJs(element);
        waitUtilElementToBeClickable(element);
        element.click();
        element.clear();
        element.sendKeys(value);
        boolean checkFlag = wait.until(ExpectedConditions.attributeContains(element, "value", value));
        Assert.assertTrue("Поле было заполнено некорректно", checkFlag);
    }

}
