package pages;

import core.BasePage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

/**
 * @author kote
 */
public class OrganizationsPage extends BasePage {

  private static final String TEXT_ON_PAGE = "//text//div[contains(.,'%s')]";
  private static final String BUTTON_BY_NAME = "//button[contains(.,'%s')]";
  private static final String ROW_BY_TEXTS = "//div[@role='row'][.//span[contains(.,'%s')]][.//span[contains(.,'%s')]]";
  private static final String ARROW_IN_ROW = ROW_BY_TEXTS + "//span[@ref='eContracted']";
  private static final String MODAL_TITLE_BY_NAME = "//*[@class='modal-title'][contains(.,'%s')]";

  @FindBy(xpath = "//div[label[span[contains(.,'Наименование')]]]//input")
  private WebElement orgName;
  @FindBy(xpath = "//div[label[span[contains(.,'Код')]]]//input")
  private WebElement orgCode;
  @FindBy(xpath = "//combo-box//div[contains(@class,'selectize-input')]")
  private WebElement parentOrg;

  public OrganizationsPage(WebDriver browser) {
    super(browser, "/#/user-management/org-units");
  }

  public boolean isTitleVisible(String title) {
    return isPresent(getBy(TEXT_ON_PAGE, title));
  }

  public boolean isOrganizationVisible(String name, String code) {
    return isPresent(getBy(ROW_BY_TEXTS, name, code));
  }

  public void clickOnButton(String name) {
    waitForElementToBeClickable(getBy(BUTTON_BY_NAME, name)).click();
  }

  public void clickOnOrganization(String name, String code) {
    waitForElementToBeClickable(getBy(ROW_BY_TEXTS, name, code)).click();
  }

  public void clickOnArrowInParentOrganization(String name, String code) {
    waitForElementToBeClickable(getBy(ARROW_IN_ROW, name, code)).click();
  }

  public void editOrgName(String value) {
    editField(orgName, value);
  }

  public void editOrgCode(String value) {
    editField(orgCode, value);
  }

  public void selectParentOrg(String value) {
    clickAndSelectValue(parentOrg, value);
  }

  public boolean isDialogOpened(String title) {
    return isPresent(getBy(MODAL_TITLE_BY_NAME, title));
  }
}
