package old.page;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessesPage extends BaseComponentPage {

  private static final String TEXT = "//div[@class='form-group']//*[text()='%s']";

  @FindBy(xpath = "//navigation-button[@object-name='NavigationButton']//button[contains(.,'Завершить')]")
  public WebElement exitButton;

  @FindBy(xpath = "//div[@id='page']")
  public WebElement pageID;

  public ProcessesPage(WebDriver browser) {
    super(browser, "/#/process/");
  }

  public String getTestProcessText() {
    return getWebElementTexts(driver.findElements(By.xpath(String.format(TEXT, "Тестовая страница")))).get(0);
  }

  //----- different
  public String getProcessId() {
    String str = driver.getCurrentUrl();
    Pattern p = Pattern.compile("process\\/(\\d+)");
    Matcher m = p.matcher(str);
    m.find();
    return m.group(1);
  }

  public void exitProcess() {
    waitForElementToBeClickable(exitButton);
    exitButton.click();
    waitForPageLoadWithoutCheckingForError();
  }
}