import Routing from '../../../pages/eui/14x/routing.js'
import EuiChipListPage from '../../../pages/eui/14x/eui-chip-list.po.js'
import EuiChipPage from '../../../pages/eui/14x/eui-chip.po.js'
import * as CookieBannerActions from '../../../actions/cookieBanner.js';
import BaseActions from '../../../actions/V14/base-actions.js';

describe('eUI - <eui-chip-list> Component Object ', async () => {
  let page = new EuiChipListPage();
  let chipPage = new EuiChipPage();
  let testFolder = './/v14x//eui-chip-list//';
  let pageName = 'eui-chip-list';
  it('should navigate to <eui-chip-list> demo page', async () => {
    browser.addTestMeta({uuid:"dcee92ca-d34d-11eb-b8bc-0242ac130003", zephyrKey: "CCQA-1123", zephyrStep: "410984", reportGroups: ["eUI.14x.eui"]});
    await page.open(Routing.getEUIComponentUrl('eui-chip-list'));
    await CookieBannerActions.cookieBanner(true);
    await BaseActions.verifyPageTitle(pageName);
    await expect(await $(page.navigationList)).
      toBeDisplayedInViewport({wait: 2000, message:'Navigation List -> is not displayed on page'});
  });
  it('should verify the presence and number of component objects <eui-chip-list> on page ', async () => {
    browser.addTestMeta({zephyrStep: "410985"});
    let sectionId = 'Default';
    await BaseActions.verifySection(sectionId, pageName);
    let chipList = page.chipListByIndex(1);
    await expect(await $(chipList.currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list by index "1" is not displayed on page'});
    await expect(await $(page.chipListByTextContains('Chip label').currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list by text "Chip label" is not displayed on page'});
    await expect(await $(page.chipListByTextEqualsAndIndex('Chip label', 1).currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list by text "Chip label" and index 1 is not displayed on page'});
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    await expect(await $(page.sectionById(sectionId))).
      toBeDisplayedInViewport({wait: 2000, message:'<Default> section -> is not displayed on page'});
    await chaiExpect(await chipList.defaultComponentsCount()).equal(25,
      'There should be 25 <eui-chip-list> components on the page.');
  });
  it('should verify the methods container, wrapper, matChipListCO, euiChipListChipCO ', async () => {
    browser.addTestMeta({zephyrStep: "410986"});
    let sectionId = 'Default';
    await BaseActions.verifySection(sectionId, pageName);
    let imageComparisonChipList = await (await page.chipListFromSectionById(sectionId).byIndex(1).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonChipList.scrollIntoView();
      await chaiExpect(await browser.checkElement(imageComparisonChipList, testFolder+'img_chipListByIndex1',
      {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_chipListByIndex1');
    }
    let chipList = page.chipListFromSectionById(sectionId);
    await expect(await $(page.sectionById(sectionId))).
      toBeDisplayedInViewport({wait: 2000, message:'<Default> section -> is not displayed on page'});
    await expect(await $(chipList.container)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list container is not displayed on page'});
    await expect(await $(chipList.wrapper)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list wrapper is not displayed on page'});
    await expect(await $(chipList.chipListList)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list chipListList currentPath is not displayed on page'});
    await expect(await $(chipList.euiChipListChipCO.currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list euiChipListChipCO currentPath is not displayed on page'});
  });
  it('should verify the methods expandButtonWrapper, hasExpandButton, buttonCO, angelRightIcon, AngelLeftIcon, toggleLinkMoreLabel, toggleLinkLessLabel ', async () => {
    browser.addTestMeta({zephyrStep: "410987"});
    let sectionId = 'maxVisibleTags';
    await BaseActions.verifySection(sectionId, pageName);
    let chipList = page.chipListFromSectionWithIdByIndex(sectionId,1);
    await expect(await $(chipList.expandButtonWrapper)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list expandButtonWrapper is not displayed on page'});
    await chaiExpect(await chipList.hasExpandButton()).equal(true, 'ChipList should have expand button.');
    await expect(await $(chipList.expandButton)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list expandButton is not displayed on page'});
    await expect(await $(chipList.angelRightIcon.currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list angelRightIcon is not displayed on page'});
    await chaiExpect(await chipList.hasMoreLabel()).equal(true, 'ChipList should have label.');
    chipList = page.chipListFromSectionWithIdByIndex(sectionId,2);
    await expect(await $(chipList.angelLeftIcon.currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list angelLeftIcon is not displayed on page'});
    await chaiExpect(await chipList.isMaxVisibleChipsOpened()).equal(true, 'ChipList should have max visible chips opened.');
    await chaiExpect(await chipList.hasLessLabel()).equal(true, 'ChipList should have label.');
    chipList = page.chipListFromSectionWithIdByIndex(sectionId,3);
    await expect(await $(chipList.euiLabelCO.currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list euiLabel is not displayed on page'});
  });
  it('should verify euiChipListChipCO methods title, hasChipsLabelTruncateCount, euiChipCO, euiIconCO', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'truncateTagsLabel';
    await BaseActions.verifySection(sectionId, pageName);
    let chipList = page.chipListFromSectionWithIdByIndex(sectionId,1);
    await expect(await $(chipList.euiChipListChipCO.title)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list euiChipListChipCO method title is not displayed on page'});
    await chaiExpect(await chipList.euiChipListChipCO.hasChipsLabelTruncateCount()).equal(true, 'ChipList should have ChipsLabelTruncateCount.');
    await expect(await $(chipList.euiChipListChipCO.euiChipCO.currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list euiChipListChipCO euiChipCO currentPath is not displayed on page'});
  });

  it('should verify icon class', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'iconClass';
    await BaseActions.verifySection(sectionId, pageName);
    let imageComparisonChipListIconClass_1 = await (await page.chipListFromSectionById(sectionId).byIndex(1).element());
    let imageComparisonChipListIconClass_2 = await (await page.chipListFromSectionById(sectionId).byIndex(2).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonChipListIconClass_1.scrollIntoView();
      await chaiExpect(await browser.checkElement(imageComparisonChipListIconClass_1, testFolder+'img_chipList_iconClass_ByIndex1',
      {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_chipList_iconClass_ByIndex1');
      await chaiExpect(await browser.checkElement(imageComparisonChipListIconClass_2, testFolder+'img_chipList_iconClass_ByIndex2'
      ,
      {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_chipList_iconClass_ByIndex2');
    }
    await expect(await $(page.sectionById(sectionId))).
      toBeDisplayedInViewport({wait: 2000, message:'<iconClass> section -> is not displayed on page'});
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).euiChipListChipCO.euiIconCO.currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list euiChipListChipCO.euiChipCO.currentPath is not displayed on page'});
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 2).euiChipListChipCO.euiChipCO.container + "//span//span")).
      toHaveAttrContaining('class', 'eui-flag-icon');
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 3).euiChipListChipCO.euiChipCO.container + "//span//span")).
      toHaveAttrContaining('class', 'flag-icon--rounded');
  });

  it('should verify Badge Sizes', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'sizes';
    await BaseActions.verifySection(sectionId, pageName);
    await expect(await $(page.sectionById(sectionId))).
      toBeDisplayedInViewport({wait: 2000, message:'<sizes> section -> is not displayed on page'});
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).currentPath)).toHaveAttrContaining('class', 'eui-chip-list--size-s');
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId,2).currentPath)).toHaveAttrContaining('class', 'eui-chip-list--size-m');
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId,3).currentPath)).toHaveAttrContaining('class', 'eui-chip-list--size-l');
  });
  it('should verify isDeletable ', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'isDeletable';
    await BaseActions.verifySection(sectionId, pageName);
    let imageComparisonChipListIsDeletable = await (await page.chipListFromSectionById(sectionId).byIndex(1).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonChipListIsDeletable.scrollIntoView();
      await chaiExpect(await browser.checkElement(imageComparisonChipListIsDeletable, testFolder+'img_chipList_isDeletable_ByIndex1',
      {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_chipList_isDeletable_ByIndex1');
    }
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list by section and id 1 is not displayed on page'});
    await chaiExpect(await page.chipListFromSectionWithIdByIndex(sectionId, 1).euiChipListChipCO.euiChipCO.isChipRemovable()).equal(true, 'ChipList should be removable.');
    await (await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).euiChipListChipCO.euiChipCO.removeIcon.currentPath)).click();
    await expect(await $(page.chipListByTextContains('Chip label accent').currentPath)).not.
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list by text "Chip label accent" is displayed on page'});
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list by section and id 1 is not displayed on page'});
  });
  it('should verify isSquared', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'isSquared';
    await BaseActions.verifySection(sectionId, pageName);
    await expect(await $(page.sectionById(sectionId))).
      toBeDisplayedInViewport({wait: 2000, message:'<isSquared> section -> is not displayed on page'});
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).currentPath)).toHaveAttr('issquared');
  });
  it('should verify Events', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'events';
    await BaseActions.verifySection(sectionId, pageName);
    let imageComparisonChipListEvents = await (await page.chipListFromSectionById(sectionId).byIndex(1).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonChipListEvents.scrollIntoView();
      await chaiExpect(await browser.checkElement(imageComparisonChipListEvents, testFolder+'img_chipList_Events_ByIndex1',
      {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_chipList_Events_ByIndex1');
    }
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    await chaiExpect(await chipPage.chipFromSectionById(sectionId).defaultComponentsCount()).equal(7,
        'There should be 7 <eui-chip> components on the page.');
    await (await $(page.buttonComponentByTextContains('Add chip').currentPath)).click();
    await chaiExpect(await chipPage.chipFromSectionById(sectionId).defaultComponentsCount()).equal(8,
      'There should be 8 <eui-chip> components on the page.');
  });
  it('should verify Max visible tags', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'maxVisibleTags';
    await BaseActions.verifySection(sectionId, pageName);
    await chaiExpect((await $$(page.chipListFromSectionWithIdByIndex(sectionId,1).euiChipListChipCO.euiChipCO.currentPath)).length).equal(2,'There should be 2 chips in chip list.');
    await chaiExpect(await page.chipListFromSectionWithIdByIndex(sectionId,1).isMaxVisibleChipsOpened()).equal(false, 'Chiplist should not have MaxVisibleChipsOpened.');
    await chaiExpect(await page.chipListFromSectionWithIdByIndex(sectionId,1).hasMoreLabel()).equal(true, 'Chiplist should have label.');
    await (await $(page.chipListFromSectionWithIdByIndex(sectionId,1).angelRightIcon.currentPath)).click();
    await chaiExpect((await $$(page.chipListFromSectionWithIdByIndex(sectionId,1).euiChipListChipCO.euiChipCO.currentPath)).length).equal(7,
      'There should be 7 chips in chip list.');

    await chaiExpect((await $$(page.chipListFromSectionWithIdByIndex(sectionId,2).euiChipListChipCO.euiChipCO.currentPath)).length).equal(7,
        'There should be 7 chips in chip list.');
    await chaiExpect(await page.chipListFromSectionWithIdByIndex(sectionId,2).isMaxVisibleChipsOpened()).equal(true, 'Chiplist should have MaxVisibleChipsOpened.');
    await chaiExpect(await page.chipListFromSectionWithIdByIndex(sectionId,2).hasMoreLabel()).equal(false, 'Chiplist should not have label.');
    await (await $(page.chipListFromSectionWithIdByIndex(sectionId,2).angelLeftIcon.currentPath)).click();
    await chaiExpect((await $$(page.chipListFromSectionWithIdByIndex(sectionId,2).euiChipListChipCO.euiChipCO.currentPath)).length).equal(2,'There should be 2 chips in chip list.');
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId,3).euiLabelCO.currentPath)).
      toBeDisplayedInViewport({wait: 2000, message:'Chip-list euiLabel is not displayed on page'});
    await expect(await $(page.chipListByTextContains('More').euiLabelCO.currentPath)).toBeDisplayedInViewport({wait: 2000, message:'Chip-list euiLabel More is not displayed on page'});
  });
  it('should verify outline chip-list', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'typeClass-outline';
    await BaseActions.verifySection(sectionId, pageName);
    let imageComparisonChipListOutline = await (await page.chipListFromSectionById(sectionId).byIndex(1).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonChipListOutline.scrollIntoView();
      await chaiExpect(await browser.checkElement(imageComparisonChipListOutline, testFolder+'img_chipList_Outline_ByIndex1',
      {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_chipList_Outline_ByIndex1');
    }
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).currentPath)).toHaveAttrContaining('class', 'eui-chip-list--outline');
  });
  it('should verify sorting of chip-list', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'sorting';
    await BaseActions.verifySection(sectionId, pageName);
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).currentPath)).toHaveAttrContaining('chipssortorder', 'ASC');
    await expect(await $(chipPage.chipFromSectionWithIdByIndex(sectionId, 1).currentPath)).toHaveText('Chip A');
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 2).currentPath)).toHaveAttrContaining('chipssortorder', 'DESC');
    await expect(await $(chipPage.chipFromSectionWithIdByIndex(sectionId, 9).currentPath)).toHaveText('Chip F');
  });
  it('should verify truncated labels of chip-list', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'truncateTagsLabel';
    await BaseActions.verifySection(sectionId, pageName);
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).euiChipListChipCO.euiChipCO.currentPath)).toHaveText('Chip…');
  });
  it('should verify ischipsdraganddrop directive for chip-list', async () => {
    browser.addTestMeta({zephyrStep: ""});
    let sectionId = 'drag-n-drop';
    await BaseActions.verifySection(sectionId, pageName);
    await expect(await $(page.chipListFromSectionWithIdByIndex(sectionId, 1).currentPath)).toHaveAttr('ischipsdraganddrop', '');
  });
});
