import Routing from '../../../../pages/eui/10x/routing-10x.js'
import CookieBannerActions from '../../../../actions/cookieBanner.js'
import EuiTablePage from '../../../../pages/eui/10x/eui/eui-table-10x.po.js'

describe('eUI - <eui-table> Component Object ',async () => {
  let tablePage = new EuiTablePage();
  it('should navigate to <eui-table> demo page',async () => {
    browser.addTestMeta({uuid:"c8e4d39c-97ce-4fca-8208-ee32eedab3fa", zephyrKey: "CCQA-1550", zephyrStep: "419073", reportGroups: ["eUI.10x.eui"]});
    await tablePage.open(Routing.getEUIComponentUrl('eui-table'));
    await CookieBannerActions.cookieBanner(true);
    let pgTitle = await $(tablePage.pageTitle);
    await pgTitle.scrollIntoView();
    await expect(await $(pgTitle)).toBeDisplayed({message:'<eui-table> page title is not visible on page'});
    await expect(await $(tablePage.appDocPageById('eui-table'))).toExist({message:'Page Id is not existing on page'});
    await expect(pgTitle).toHaveText('eui-table', {wait: 10000, message: 'Page title is not as expected'});
    await expect(await $(tablePage.navigationList)).toBeDisplayed({message:'Navigation List -> is not visible on page'});
  });
  it('should verify the section titles',async () => {
    browser.addTestMeta({zephyrStep: "419074"});
    let sectionId = 'Default';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayedInViewport({message:'<' + sectionId + '> section -> is not visible on page'});

    sectionId = 'pagination';
    section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayedInViewport({message:'<' + sectionId + '> section -> is not visible on page'});

    sectionId = 'sortable-cols';
    section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayedInViewport({message:'<' + sectionId + '> section -> is not visible on page'});

    sectionId = 'selectable-row';
    section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayedInViewport({message:'<' + sectionId + '> section -> is not visible on page'});
    await chaiExpect(await tablePage.euiTableCO.byIndex(1).defaultComponentsCount()).equal(30, 'There should be 30 <eui-table> components on the page.');
  });
  it('should verify the <eui-table> identification methods',async () => {
    browser.addTestMeta({zephyrStep: "419075"});
    let sectionId = 'hide-show-cols';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayedInViewport({message:'<' + sectionId + '> section -> is not visible on page'});
    let table = tablePage.tableFromSectionId(sectionId).byIndex(1);
    await expect(await $(table.currentPath)).toExist({wait: 3000, message: '<eui-table> co by index 1 is not present in page'});
    await expect(await $(table.tbody)).toExist({wait: 3000, message: '<eui-table> co by index 1 - table body is not present'});
    await expect(await $(table.thead)).toExist({wait: 3000, message: '<eui-table> co by index 1 - table head is not present'});
    await expect(await $(table.tfoot)).toExist({wait: 3000, message: '<eui-table> co by index 1 - table footer is not present'});
    await expect(await $(table.bodyRowCO.currentPath)).toExist({wait: 3000, message: '<eui-table> co by index 1 - table Body row is not present'});
    await expect(await $(table.bodyCellCO.currentPath)).toExist({wait: 3000, message: '<eui-table> co by index 1 - table Body cell is not present'});
    let bodyRowCount = await (await $$(table.bodyRowCO.currentPath)).length;
    await expect(bodyRowCount).toBe(10);
    let bodyCellCount = await (await $$(table.bodyCellCO.currentPath)).length;
    await expect(bodyCellCount).toBe(50);
    let footerRowCount = await (await $$(table.footerRowCO.currentPath)).length;
    await expect(footerRowCount).toBe(1);
    let footerCellCount = await (await $$(table.footerCellCO.currentPath)).length;
    await expect(footerCellCount).toBe(3);
  });
  it('should verify the <eui-table-paginator> methods',async () => {
    browser.addTestMeta({zephyrStep: "419076"});
    let sectionId = 'hide-show-cols';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    // paginator
    let table = tablePage.tablePaginatorFromSectionId(sectionId).byIndex(1);
    await expect(await $(table.currentPath)).toBeDisplayed({wait: 3000, message: '<eui-table-paginator> co by index 1 is not displayed in page'});
    // paginator page size selector text
    await expect(await $(table.pageSizeSelector)).toHaveTextContaining('Items per page', {wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Page size selector is not as expected'});
    // paginator dropdown button
    await expect(await $(table.dropdownCO.currentPath)).toBeDisplayed({wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Dropdown Buttton CO is not visible on the page'});
    // paginator arrows
    await expect(await $(table.caretFirst)).toBeDisplayed({wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Arrow First is not visible on the page'});
    await expect(await $(table.caretLeft)).toBeDisplayed({wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Arrow left is not visible on the page'});
    await expect(await $(table.caretRight)).toBeDisplayed({wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Arrow right is not visible on the page'});
    await expect(await $(table.caretLast)).toBeDisplayed({wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Arrow last is not visible on the page'});
    // paginator range text
    await expect(await $(table.pageRange)).toHaveTextContaining('Showing 1 - 10 of 28 items', {wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Page range text is not as expected'});
  });
  it('should verify the <eui-table-paginator> dropdown actions',async () => {
    browser.addTestMeta({zephyrStep: "419077"});
    let sectionId = 'hide-show-cols';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    let button = await $(tablePage.tablePaginatorFromSectionId(sectionId).byIndex(1).dropdownCO.euiButtonCO.currentPath)
    await button.click();

    await expect(await $(tablePage.listCO.currentPath)).toExist({ wait: 3000,
      message: 'Overlay list doesn\'t exists after clicking paginator dropdown button' });
    await expect(await $(tablePage.listCO.euiListItemCO.byIndex(1).itemContainer)).toHaveText('5', {wait: 3000,
      message: '<eui-table-paginator> list item 1 is not as expected'});
    await expect(await $(tablePage.listCO.euiListItemCO.byIndex(2).itemContainer)).toHaveText('10', {wait: 3000,
      message: '<eui-table-paginator> list item 2 is not as expected'});
    await expect(await $(tablePage.listCO.euiListItemCO.byIndex(3).itemContainer)).toHaveText('25', {wait: 3000,
      message: '<eui-table-paginator> list item 3 is not as expected'});
    await expect(await $(tablePage.listCO.euiListItemCO.byIndex(4).itemContainer)).toHaveText('50', {wait: 3000,
      message: '<eui-table-paginator> list item 4 is not as expected'});
    await expect(await $(tablePage.listCO.euiListItemCO.byIndex(5).itemContainer)).toHaveText('100', {wait: 3000,
      message: '<eui-table-paginator> list item 5 is not as expected'});
    await (await $(tablePage.listCO.euiListItemCO.byIndex(1).currentPath)).click();
    await expect(await $(tablePage.listCO.currentPath)).not.toExist({ wait: 3000,
      message: 'Overlay list still exists after clicking one option from paginator' });
    let bodyRowCount = await (await $$(tablePage.tableFromSectionId(sectionId).byIndex(1).bodyRowCO.currentPath)).length;
    await expect(bodyRowCount).toBe(5);
    await expect(await $(tablePage.tablePaginatorFromSectionId(sectionId).byIndex(1).pageRange)).toHaveTextContaining('Showing 1 - 5 of 28 items', {wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Page range text is not as expected'});
  });
  it('should verify the <eui-table-paginator> arrows actions',async () => {
    browser.addTestMeta({zephyrStep: "419078"});
    let sectionId = 'hide-show-cols';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    let tablePaginator = tablePage.tablePaginatorFromSectionId(sectionId).byIndex(1);
    await expect(await $(tablePaginator.caretFirst)).toBeDisplayed({ wait: 3000, message: 'Paginator -> Arrow first is not visible on page' });
    await expect(await $(tablePaginator.caretLeft)).toBeDisplayed({ wait: 3000, message: 'Paginator -> Arrow left is not visible on page' });
    await expect(await $(tablePaginator.caretRight)).toBeDisplayed({ wait: 3000, message: 'Paginator -> Arrow right is not visible on page' });
    await expect(await $(tablePaginator.caretLast)).toBeDisplayed({ wait: 3000, message: 'Paginator -> Arrow last is not visible on page' });
    await (await $(tablePaginator.caretRightButton)).click();
    await expect(await $(tablePaginator.pageRange)).toHaveTextContaining('Showing 6 - 10 of 28 items', {wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Page range text is not as expected'});

    await expect(await $(tablePaginator.caretFirstButton)).not.toHaveAttributeContaining('disabled');
    await expect(await $(tablePaginator.caretLeftButton)).not.toHaveAttributeContaining('disabled');
    await expect(await $(tablePaginator.caretRightButton)).not.toHaveAttributeContaining('disabled');
    await expect(await $(tablePaginator.caretLastButton)).not.toHaveAttributeContaining('disabled');

    await (await $(tablePaginator.caretLastButton)).click()
    await expect(await $(tablePaginator.pageRange)).toHaveTextContaining('Showing 26 - 28 of 28 items', {wait: 3000,
      message: '<eui-table-paginator> co by index 1 - Page range text is not as expected'});

    await expect(await $(tablePaginator.caretFirstButton)).not.toHaveAttributeContaining('disabled');
    await expect(await $(tablePaginator.caretLeftButton)).not.toHaveAttributeContaining('disabled');
    await expect(await $(tablePaginator.caretRightButton)).toHaveAttributeContaining('disabled', 'true')
    await expect(await $(tablePaginator.caretLastButton)).toHaveAttributeContaining('disabled', 'true')
  });
  it('should verify the <eui-table-filter> methods',async () => {
    browser.addTestMeta({zephyrStep: "419079"});
    let sectionId = 'global-filter';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    let tableFilter = tablePage.tableFilterFromSectionId(sectionId);
    let rowNumber = await tablePage.tableRowCOfromSectionID(sectionId).defaultComponentsCount();
    await expect(rowNumber).toBeGreaterThan(3);
    await expect(await (await tableFilter.element())).toExist({wait: 3000, message: '<eui-table-filter> co is not present in page'});
    await expect(await $(tableFilter.currentPath)).toBeDisplayed({wait: 3000, message: '<eui-table-filter> co is not displayed on page'});

    await expect(await $(tableFilter.filterInput)).toBeDisplayed({wait: 3000, message: '<eui-table-filter> Input - is not displayed on page'});
    await expect(await $(tableFilter.searchIcon)).toBeDisplayed({wait: 3000, message: '<eui-table-filter> Search Icon is not displayed on page'});
    await expect(await $(tableFilter.clearIcon)).not.toExist({wait: 3000, message: '<eui-table-filter> Clear button should not be dispalyed on page'});

    await (await $(tableFilter.filterInput)).setValue('Belgium');
    let tablePaginator = tablePage.tablePaginatorFromSectionId(sectionId).byIndex(1).pageRange
    await browser.waitUntil(async () => await (await $(tablePaginator)).getText() != 'Showing 1 - 10 of 28 items', { timeout: 5000, timeoutMsg: 'expected text to be different after 5s'});
    await expect(await $(tableFilter.clearIcon)).toExist({wait: 3000, message: '<eui-table-filter> Clear button is not visible after user inputs a value inside search field'});
    rowNumber = await tablePage.tableRowCOfromSectionID(sectionId).defaultComponentsCount();
    await expect(rowNumber).toBe(21);
    await (await $(tableFilter.clearIcon)).click();
    await browser.waitUntil(async () => await (await $(tablePaginator)).getText() === 'Showing 1 - 10 of 28 items', { timeout: 5000, timeoutMsg: 'expected text to be different after 5s'});
    rowNumber = await tablePage.tableRowCOfromSectionID(sectionId).defaultComponentsCount();
    await expect(rowNumber).toBeGreaterThan(3);
  });
  it('should verify the <eui-table-selectable-header> methods',async () => {
    browser.addTestMeta({zephyrStep: "419726"});
    let sectionId = 'selectable-row';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    let selHeader = tablePage.tableSelHeaderCOfromSectionID(sectionId);
    let selRow = tablePage.tableSelRowCOfromSectionID(sectionId);

    await expect(await $(selHeader.currentPath)).toBeDisplayed({wait: 5000, message:'Selectable header is not displayed in table under section id ' + sectionId});
    await expect(await $(selHeader.euiIconCO.currentPath)).toHaveAttrContaining('aria-label', 'Check');
    await expect(await selHeader.isChecked()).toBe(false);
    await expect(await (await selRow.byIndex(1).element())).toBeDisplayed({wait: 5000, message:'Selectable row by index 1 is not visible in table under section id ' + sectionId});
    let selRowsNumber = await selRow.defaultComponentsCount()
    await expect(selRowsNumber).toBe(20);
    await expect(await selRow.byIndex(1).isChecked()).toBe(false);


    await (await $(selHeader.euiIconCO.currentPath)).click();
    await expect(await $(selHeader.euiIconCO.currentPath)).toHaveAttrContaining('aria-label', 'Uncheck');
    await expect(await selHeader.isChecked()).toBe(true);
    await expect(await selRow.byIndex(1).isChecked()).toBe(true);
    await expect(await selRow.byIndex(10).isChecked()).toBe(true);
  });
  it('should verify the <eui-table-filter> sortable column',async () => {
    browser.addTestMeta({zephyrStep: "419727"});
    let sectionId = 'sortable-cols';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    let sortCol = tablePage.tableSortColCOfromSectionID(sectionId);

    await expect(await (await sortCol.element())).toExist({wait: 5000, message:'Sortable column is not present on page'});
    let sortColNumber = await sortCol.defaultComponentsCount();
    await expect(sortColNumber).toBe(23);
    await expect(await $(sortCol.sortableIcon)).toExist({wait: 10000, message:'Sortable icon is not present in table'});
    await (await $(sortCol.sortableIcon)).click();
    let cellBadge = tablePage.tableCellCOfromSectionID(sectionId).byIndex(1).currentPath + '/span'
    await expect(await $(cellBadge)).toHaveText('1', {wait: 3000, message: 'Cell text is not as expected'});
    await expect(await $(sortCol.ascSortableIcon)).toExist({wait: 3000, message:'Ascending Sortable icon is not present in table'});
    await expect(await $(sortCol.descSortableIcon)).not.toExist({wait: 3000, message:'Descending Sortable should not be present in table'});

    await (await $(sortCol.sortableIcon)).click();
    await expect(await $(cellBadge)).toHaveText('28', {wait: 3000, message: 'Cell text is not as expected'});
    await expect(await $(sortCol.descSortableIcon)).toExist({wait: 3000, message:'Descending Sortable is not present in table'});
    await expect(await $(sortCol.ascSortableIcon)).not.toExist({wait: 3000, message:'Ascending Sortable icon should not be present in table'});
  });
  it('should verify the <eui-table-expandable-row> CO',async () => {
    browser.addTestMeta({zephyrStep: "419727"});
    let sectionId = 'expandable-row';
    let section = await $(tablePage.sectionById(sectionId));
    await section.scrollIntoView();
    let expRow = tablePage.tableExpandableRowCOfromSectionID(sectionId);

    let expandToggle = await $(tablePage.expandToggleFromTableWithSectionId(sectionId, 2));
    await expandToggle.click();
    await expect(await (await expRow.element())).toExist({wait: 5000, message:'Expandable row is not present on page'});
  });
});
