package tests;

import core.BaseTest;
import core.Browser;
import core.BrowserArgumentProvider;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import pages.InboxPage;
import pages.ProcessesPage;
import pages.TestProcessPage;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openqa.selenium.support.PageFactory.initElements;

/**
 * @author kote
 */
public class ProcessesIT extends BaseTest {

  private static final String ADMINISTRATION = "Администрирование";
  private static final String PROCESSES_INSTANCES = "Экземпляры процессов";
  private static final String COMPLETED = "Завершенные";
  private static final String ABORTED = "Прерванные";
  private static final String TEST_PROCESS = "test_process";
  private static final String TEST_PAGE_TITLE = "Тестовая страница";

  public ProcessesIT() {
    super();
  }

  @ParameterizedTest
  @ArgumentsSource(BrowserArgumentProvider.class)
  public void checkActionsWithProcessesInstances(Browser browser) {
    InboxPage inboxPage = loginInAppByAdmin(browser);
    assertTrue(inboxPage.pageLoadSuccessfully());

    //check start process:
    inboxPage.startProcess(TEST_PROCESS);
    TestProcessPage testProcessPage = initElements(driver, TestProcessPage.class);
    assertTrue(testProcessPage.pageLoadSuccessfully());
    assertTrue(testProcessPage.isPageTitleVisible(TEST_PAGE_TITLE));

    String processId = testProcessPage.getProcessId();

    //check active processes:
    inboxPage.clickOnMenu(ADMINISTRATION);
    inboxPage.clickOnSubMenu(PROCESSES_INSTANCES);
    ProcessesPage processesPage = initElements(driver, ProcessesPage.class);
    assertTrue(processesPage.pageLoadSuccessfully());
    assertTrue(processesPage.isTitleVisible("Процессы"));
    assertTrue(processesPage.isTabSelected("Активные"));
    assertTrue(processesPage.isProcessVisibleInGrid(processId));

    //check view process instance:
    processesPage.clickOnProcessInGrid(processId);
    processesPage.clickOnButton("Просмотреть");
    assertTrue(processesPage.pageLoadSuccessfully());
    assertTrue(processesPage.isTitleVisible("Экземпляр процесса"));

    processesPage.clickOnLink("Текст");
    assertTrue(processesPage.thereIsNoError());

    processesPage.clickOnLink("Изображение");
    assertTrue(processesPage.thereIsNoError());

    processesPage.clickOnButton("Закрыть");
    assertTrue(processesPage.pageLoadSuccessfully());

    //check aborted processes:
    processesPage.clickOnProcessInGrid(processId);
    processesPage.clickOnButton("Прервать");
    assertTrue(processesPage.isDialogOpened("Подтверждение действия"));

    processesPage.clickOnButton("Да");
    assertTrue(processesPage.pageLoadSuccessfully());
    assertFalse(processesPage.isProcessVisibleInGrid(processId));

    processesPage.clickOnTab(ABORTED);
    assertTrue(processesPage.pageLoadSuccessfully());
    assertTrue(processesPage.isTabSelected(ABORTED));
    assertTrue(processesPage.isProcessVisibleInGrid(processId));

    //check complete process:
    inboxPage.startProcess(TEST_PROCESS);
    assertTrue(testProcessPage.pageLoadSuccessfully());

    String newProcessId = testProcessPage.getProcessId();
    testProcessPage.clickOnButton("Завершить");
    assertTrue(inboxPage.pageLoadSuccessfully());
    assertFalse(testProcessPage.isPageTitleVisible(TEST_PAGE_TITLE));

    //check completed processes:
    inboxPage.clickOnMenu(ADMINISTRATION);
    inboxPage.clickOnSubMenu(PROCESSES_INSTANCES);
    assertTrue(processesPage.pageLoadSuccessfully());
    assertFalse(processesPage.isProcessVisibleInGrid(newProcessId));

    processesPage.clickOnTab(COMPLETED);
    assertTrue(processesPage.pageLoadSuccessfully());
    assertTrue(processesPage.isTabSelected(COMPLETED));
    assertTrue(processesPage.isProcessVisibleInGrid(newProcessId));
  }
}