package old.page;

import java.time.Duration;
import java.util.List;

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

import static org.junit.jupiter.api.Assertions.assertTrue;

public class InboxPage extends BaseComponentPage {

  private static final String XPATH_FOR_MENU_ITEM = "//button[@class='dropdown-item'][contains(.,'%s')]";
  private static final By PROCESS_LIST_BY = By.xpath("//div[@id='process-instance-list']");
  private static final By PROCESS_DATE_BY = By.xpath("//span[contains(.,'Дата запуска')]");
  private static final By MENU_ADMINISTRATION = By.xpath("//button[contains(.,'Администрирование')]");
  private static final By START_PAGE = By.xpath("//div[@class='logo']");

  @FindBy(xpath = "//button[@id='adminDropdownMenu']")
  public WebElement adminList;
  @FindBy(xpath = "//div[@class='logout']//button[contains(@class,'nav-link')]")
  public WebElement userMenuButton;
  @FindBy(xpath = "//button[contains(.,'Выход')]")
  public WebElement exitButton;
  @FindBy(xpath = "//button[@id='startProcessDropdownMenu']")
  private WebElement startButton;
  @FindBy(xpath = "//button[@routerlink='/user-management/groups']")
  private WebElement groupPage;
  @FindBy(xpath = "//button[@routerlink='/user-management/roles']")
  private WebElement rolePage;
  @FindBy(xpath = "//button[@routerlink='/user-management/users']")
  private WebElement userPage;
  @FindBy(xpath = "//button[@routerlink='/user-management/org-units']")
  private WebElement orgPage;
  @FindBy(xpath = "//button[@routerlink='/user-management/authorities']")
  private WebElement authoritiesPage;
  @FindBy(xpath = "//button[@routerlink='/process/instance']")
  private WebElement processPage;
  @FindBy(xpath = "//div[@class='user-info']//div[@class='user-fio']")
  private WebElement userFIO;

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

  //выход из приложения
  public void exitApp() {
    waitProgressBarNotVisible();
    userMenuButton.click();
    exitButton.click();
    assertTrue(thereIsNoError());
  }

  //открыть Экземпляры процессов
  public AdminProcessPage openAdminProcessPage() {
    waitForPageLoad();
    adminList.click();
    waitForElementToBeClickable(processPage, Duration.ofSeconds(10));
    processPage.click();
    waitForPageLoad();
    waitForVisibilityOfElementLocated(PROCESS_LIST_BY);
    waitForVisibilityOfElementLocated(PROCESS_DATE_BY);
    return PageFactory.initElements(driver, AdminProcessPage.class);
  }

  //начать процесс
  public ProcessesPage startProcess(String processName) {
    waitForPageLoad();
    startButton.click();
    By menuItem = By.xpath(String.format(XPATH_FOR_MENU_ITEM, processName));
    WebElement process = waitForVisibilityOfElementLocated(menuItem);
    process.click();
    waitForPageLoad();
    return PageFactory.initElements(driver, ProcessesPage.class);
  }

  public AdminUserPage startAdminUser() {
    adminList.click();
    userPage.click();
    waitForPageLoad();
    return PageFactory.initElements(driver, AdminUserPage.class);
  }

  public AdminGroupPage startAdminGroup() {
    adminList.click();
    groupPage.click();
    waitForPageLoad();
    return PageFactory.initElements(driver, AdminGroupPage.class);
  }

  public AdminRolePage startAdminRole() {
    adminList.click();
    rolePage.click();
    waitForPageLoad();
    return PageFactory.initElements(driver, AdminRolePage.class);
  }

  public AdminOrgPage startAdminOrganization() {
    adminList.click();
    orgPage.click();
    waitForPageLoad();
    return PageFactory.initElements(driver, AdminOrgPage.class);
  }

  public List<WebElement> getListOfAdminPanels() {
    return findElements(MENU_ADMINISTRATION);
  }

  public String getUserFIOFromPage() {
    return userFIO.getText();
  }

  public List<WebElement> getEmailInputsFrom(WebDriver element) {
    return element.findElements(EMAIL_INPUT_BY);
  }

  public void waitEmailInput() {
    waitForVisibilityOfElementLocated(EMAIL_INPUT_BY);
  }

  public boolean startPageDisplay() {
    waitForPageLoad();
    return waitForVisibilityOfElementLocated(START_PAGE).isDisplayed();
  }
}