package ru.ibs.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class DashboardPage {
    private final WebDriver driver;
    private final WebDriverWait wait;

    @FindBy(xpath = "//h1[@class='oro-subtitle' and contains(text(), 'Панель быстрого запуска')]")
    private WebElement dashboardTitle;

    @FindBy(xpath = "//span[text()='Расходы']/ancestor::li")
    private WebElement expensesMenu;

    @FindBy(xpath = "//a[@href='/business-trip/']/span[text()='Командировки']")
    private WebElement businessTripLink;

    public DashboardPage(WebDriver driver) {
        this.driver = driver;
        this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        PageFactory.initElements(driver, this);
    }

    public boolean isDashboardLoaded() {
        return wait.until(ExpectedConditions.visibilityOf(dashboardTitle)).isDisplayed();
    }

    public BusinessTripPage goToBusinessTrip() {
        Actions actions = new Actions(driver);
        actions.moveToElement(expensesMenu).perform();
        wait.until(ExpectedConditions.elementToBeClickable(businessTripLink)).click();
        return new BusinessTripPage(driver);
    }
}
