package ru.ibs.tests;

import org.openqa.selenium.*;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.*;

import java.time.Duration;

public class BusinessTripPage {
    private WebDriver driver;
    private WebDriverWait wait;

    @FindBy(xpath = "//h1[contains(text(), 'Командировки')]")
    private WebElement tripsHeader;

    @FindBy(css = "div.pull-left.btn-group.icons-holder")
    private WebElement createTripButton;

    @FindBy(xpath = "//h1[@class='user-name' and contains(text(), 'Создать командировку')]")
    private WebElement createTripHeader;

    @FindBy(xpath = "//select[contains(@name,'businessUnit')]")
    private WebElement departmentSelect;

    @FindBy(id = "company-selector-show")
    private WebElement companySelectorShow;

    @FindBy(css = "span.select2-chosen")
    private WebElement organizationSpan;

    @FindBy(css = "input.select2-input")
    private WebElement organizationInput;

    @FindBy(xpath = "//label[contains(text(),'Заказ билетов')]/preceding-sibling::input")
    private WebElement ticketsCheckbox;

    @FindBy(xpath = "//button[contains(text(),'Сохранить и закрыть')]")
    private WebElement saveAndCloseButton;

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

    public boolean isTripsPageLoaded() {
        return wait.until(ExpectedConditions.visibilityOf(tripsHeader)).isDisplayed();
    }

    public void openCreateTripForm() {
        wait.until(ExpectedConditions.elementToBeClickable(createTripButton)).click();
        wait.until(ExpectedConditions.visibilityOf(createTripHeader));
    }

    public void selectDepartment(String departmentName) {
        Select select = new Select(departmentSelect);
        select.selectByVisibleText(departmentName);
    }

    public void selectOrganization(String orgName) {
        companySelectorShow.click();
        wait.until(ExpectedConditions.elementToBeClickable(organizationSpan)).click();
        wait.until(ExpectedConditions.visibilityOf(organizationInput)).sendKeys(orgName);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='select2-match' and text()='" + orgName + "']"))).click();
    }

    public void setTicketsCheckbox(boolean checked) {
        if (ticketsCheckbox.isSelected() != checked) {
            ticketsCheckbox.click();
        }
    }

    public void fillCity(String city) {
        By cityInputLocator = By.xpath("//input[contains(@id,'arrivalCity')]");
        WebElement cityInput = wait.until(ExpectedConditions.visibilityOfElementLocated(cityInputLocator));
        scrollToElement(cityInput);
        cityInput.clear();
        cityInput.sendKeys(city);
        // Optionally assert
    }

    public void fillDate(By dateLocator, String dateValue) {
        WebElement dateInput = wait.until(ExpectedConditions.elementToBeClickable(dateLocator));
        scrollToElement(dateInput);
        dateInput.clear();
        dateInput.sendKeys(dateValue);
        dateInput.sendKeys(Keys.TAB);
        // Optionally assert
    }

    public void saveAndClose() {
        scrollToElement(saveAndCloseButton);
        saveAndCloseButton.click();
    }

    public boolean isErrorDisplayed(String errorText) {
        By errorLocator = By.xpath("//span[@class='validation-failed' and contains(text(), '" + errorText + "')]");
        return wait.until(ExpectedConditions.visibilityOfElementLocated(errorLocator)).isDisplayed();
    }

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