import Routing from '../../../pages/eui/14x/routing.js'
import * as CookieBannerActions from '../../../actions/cookieBanner.js'
import EuiDropdownButtonPage from '../../../pages/eui/14x/eui-dropdown-button-menu.po.js';
import BaseActions from '../../../actions/V14/base-actions.js';

describe('eUI - <eui-dropdown-button-menu> Component Object ', async () => {
  let page = new EuiDropdownButtonPage();
  let testFolder = './/v14x//eui-dropdown-button-menu//';
  let pageName = 'eui-dropdown-button-menu';
  it('should navigate to <eui-dropdown-button-menu> demo page', async () => {
    browser.addTestMeta({ uuid: "f3d6e050-170d-11ed-861d-0242ac120002", zephyrKey: "CCQA-2226", zephyrStep: "510219", reportGroups: ["eUI.14x.eui"] });
    await page.open(Routing.getEUIComponentUrl('eui-dropdown-button-menu'));
    await CookieBannerActions.cookieBanner(true);
    await BaseActions.verifyPageTitle(pageName);
    let navigationList = await $(page.navigationList);
    await expect(navigationList).toBeDisplayed({ wait: 1000, message: 'Navigation List -> is not displayed on page' });
  });
  it('should verify the presence and number of component objects <eui-dropdown-button-menu> on page', async () => {
    browser.addTestMeta({ zephyrStep: "510220" });
    let sectionId = 'Default';
    await BaseActions.verifySection(sectionId, pageName);
    let index = 1;
    let text = 'Open Menu';
    let menuByIndex1 = await (await page.dropdownButtonMenuByIndex(index).element());
    let menuByText = await (await page.dropdownButtonMenuByText(text).element());
    let menuByTextAndIndex = await (await page.dropdownButtonMenuByTextEqualsAndIndex(text, index).element());
    await expect(await $(page.sectionById(sectionId))).toBeDisplayed({ message: '<Default> section -> is not displayed on page' });
    await expect(menuByIndex1).toBeDisplayed({ message: 'Dropdown-button-menu by index ' + index + ' is not displayed on page' });
    await expect(menuByText).toBeDisplayed({ message: 'Dropdown-button-menu by text ' + text + ' is not displayed on page' });
    await expect(menuByTextAndIndex).toBeDisplayed({ message: 'Dropdown-button-menu by text ' + text + ' and index ' + index + ' is not displayed on page' });
    await chaiExpect(await page.dropdownButtonMenuByIndex(index).defaultComponentsCount()).equal(8, 'There should be 8 <eui-dropdown-button-menu> components on the page.');
    if (browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(menuByIndex1, testFolder + 'img_menuByIndex1', { hideElements: [await $('//eui-app-toolbar')] })).equal(0, 'Image comparison failed for menuByIndex1');
      await chaiExpect(await browser.checkElement(menuByText, testFolder + 'img_menuByText', { hideElements: [await $('//eui-app-toolbar')] })).equal(0, 'Image comparison failed for menuByText');
      await chaiExpect(await browser.checkElement(menuByTextAndIndex, testFolder + 'img_menuByTextAndIndex', { hideElements: [await $('//eui-app-toolbar')] })).equal(0, 'Image comparison failed for menuByTextAndIndex');
    }
  });
  it('should verify the <eui-dropdown-button-menu> is Disabled', async () => {
    browser.addTestMeta({ zephyrStep: "510221" });
    let sectionId = 'disabled';
    await BaseActions.verifySection(sectionId, pageName);
    let menu = page.dropdownButtonMenuFromSectionWithIdByIndex(sectionId, 1);
    let disabledButton = await $(menu.buttonCO.currentPath);
    await expect(await $(menu.currentPath)).toBeDisplayed({ message: '<eui-dropdown-button-menu> element from Disabled section is not displayed on page' });
    await expect(disabledButton).toBeDisplayed({ message: '<eui-dropdown-button-menu> - button from Disabled section  -> is not displayed on page' });
    await chaiExpect(await menu.isDisabled()).equal(true, 'Button should be disabled');
    if (browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(disabledButton, testFolder + 'img_disabledButton', { hideElements: [await $('//eui-app-toolbar')] })).equal(0, 'Image comparison failed for disabledButton');
    }
    sectionId = 'Default';
    await BaseActions.verifySection(sectionId, pageName);
    menu = page.dropdownButtonMenuFromSectionWithIdByIndex(sectionId, 1);
    let enabledButton = await $(menu.buttonCO.currentPath);
    await expect(await $(menu.currentPath)).toBeDisplayed({ message: '<eui-dropdown-button-menu> element from Default section is not displayed on page' });
    await expect(enabledButton).toBeDisplayed({ message: '<eui-dropdown-button-menu> - button from Default section -> is not displayed on page' });
    await chaiExpect(await menu.isDisabled()).equal(false, 'Button should not be disabled');
    if (browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(enabledButton, testFolder + 'img_enabledButton', { hideElements: [await $('//eui-app-toolbar')] })).equal(0, 'Image comparison failed for enabledButton');
    }
  });
  it('should verify the <eui-dropdown-button-menu> mat-menu', async () => {
    browser.addTestMeta({ zephyrStep: "510222" });
    let sectionId = 'isDropDownRightAligned';
    await BaseActions.verifySection(sectionId, pageName);
    let menu = page.dropdownButtonMenuFromSectionWithIdByIndex(sectionId, 1);
    await expect(await $(menu.currentPath)).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu> is not displayed on page' });
    await expect(await $(menu.matMenuCO.currentPath)).toBeExisting({ message: '<eui-dropdown-button-menu> - matmenuCO -> is not existing on page' });
  });
  it('should verify the <eui-dropdown-button-menu> menu Items ', async () => {
    browser.addTestMeta({ zephyrStep: "510223" });
    let sectionId = 'Default';
    await BaseActions.verifySection(sectionId, pageName);
    let menu = page.dropdownButtonMenuFromSectionWithIdByIndex(sectionId, 1);
    let button = await $(menu.buttonCO.currentPath)
    await expect(await $(menu.currentPath)).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu> is not displayed on page' });
    await expect(button).toBeDisplayed({ message: '<eui-dropdown-button-menu> - button -> is not displayed on page' });
    await button.click();

    let matMenuContent = page.euiDropdownButtonMenuItemsCO.byTextContains('Item 1');
    let menuItems = await ($(matMenuContent.currentPath));
    await expect(menuItems).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu -> menu Items -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await menuItems.scrollIntoView();
      await chaiExpect(await browser.checkElement(menuItems, testFolder + 'img_menuItems', { hideElements: [await $('//eui-app-toolbar')] })).within(0, 9, 'Image comparison failed for menuItems');
    }
    let item1Button = await ($(matMenuContent.buttonCO.byIndex(1).currentPath));
    await expect(item1Button).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu -> item1Button -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await item1Button.scrollIntoView();
      await chaiExpect(await browser.checkElement(item1Button, testFolder + 'img_item1Button', { hideElements: [await $('//eui-app-toolbar')] })).within(0, 4, 'Image comparison failed for item1Button');
    }
    await chaiExpect(await matMenuContent.hasChildren()).equal(false, 'hasChildren should not be false');
    let categoryIcon = await ($(matMenuContent.categoryIcon));
    await expect(categoryIcon).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu-Items> categoryIcon -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(categoryIcon, testFolder + 'img_categoryIcon', { hideElements: [await $('//eui-app-toolbar')] })).equal(0, 'Image comparison failed for categoryIcon');
    }
    await chaiExpect(await matMenuContent.hasIconClass()).equal(true, 'hasIconClass should not be true');
    await item1Button.click();
  });

  it('should verify the <eui-dropdown-button-child-menu> dynamic menu Items ', async () => {
    browser.addTestMeta({ zephyrStep: "510224" });
    let sectionId = 'dynamic-submenus';
    await BaseActions.verifySection(sectionId, pageName);
    let menu = page.dropdownButtonMenuFromSectionWithIdByIndex(sectionId, 1);
    let button = await $(menu.buttonCO.currentPath)
    await expect(await $(menu.currentPath)).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu> is not displayed on page' });
    await expect(button).toBeDisplayed({ message: '<eui-dropdown-button-menu> - button -> is not displayed on page' });
    await button.click();

    let matMenuContent = page.euiDropdownButtonMenuItemsCO.byTextContains('Item 4');
    let dynamicMenuItems = await ($(matMenuContent.currentPath));
    await expect(dynamicMenuItems).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu -> dynamic menu Items -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await dynamicMenuItems.scrollIntoView();
      await chaiExpect(await browser.checkElement(dynamicMenuItems, testFolder + 'img_dynamicMenuItems', { hideElements: [await $('//eui-app-toolbar')] })).within(0, 5, 'Image comparison failed for dynamicMenuItems');
    }
    let dynamicItem4Button = await ($(matMenuContent.buttonCO.byIndex(4).currentPath));
    await expect(dynamicItem4Button).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu -> dynamicItem4Button -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await dynamicItem4Button.scrollIntoView();
      await chaiExpect(await browser.checkElement(dynamicItem4Button, testFolder + 'img_dynamicItem4Button', { hideElements: [await $('//eui-app-toolbar')] })).within(0, 4, 'Image comparison failed for dynamicItem4Button');
    }

    await chaiExpect(await matMenuContent.hasChildren()).equal(true, 'hasChildren should not be true');
    let dynamicCategoryIcon = await ($(matMenuContent.categoryIcon));
    await expect(dynamicCategoryIcon).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu-Items> dynamic categoryIcon -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(dynamicCategoryIcon, testFolder + 'img_dynamicCategoryIcon', { hideElements: [await $('//eui-app-toolbar')] })).equal(0, 'Image comparison failed for dynamicCategoryIcon');
    }
    await chaiExpect(await matMenuContent.hasIconClass()).equal(true, 'hasIconClass should not be true');

    let childMenuItems = await ($(matMenuContent.euiDropdownButtonChildMenuCO.currentPath));
    await expect(childMenuItems).toBeExisting({ wait: 3000, message: '<eui-dropdown-button-child-menu -> child menu Items -> is not existing on page' });
    await dynamicItem4Button.click();

    matMenuContent = page.euiDropdownButtonMenuItemsCO.byTextContains('Item 5');
    let dynamicMenuItems5 = await ($(matMenuContent.currentPath));
    await expect(dynamicMenuItems5).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu -> menu Items 5 -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await dynamicMenuItems5.scrollIntoView();
      await chaiExpect(await browser.checkElement(dynamicMenuItems5, testFolder + 'img_dynamicMenuItems5', { hideElements: [await $('//eui-app-toolbar')] })).within(0, 9, 'Image comparison failed for dynamicMenuItems5');
    }
    let dynamicItem5Button = await ($(matMenuContent.buttonCO.byIndex(1).currentPath));
    await expect(dynamicItem5Button).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu -> dynamicItem5Button -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await dynamicItem5Button.scrollIntoView();
      await chaiExpect(await browser.checkElement(dynamicItem5Button, testFolder + 'img_dynamicItem5Button', { hideElements: [await $('//eui-app-toolbar')] })).within(0, 4, 'Image comparison failed for dynamicItem5Button');
    }

    await chaiExpect(await matMenuContent.hasChildren()).equal(true, 'hasChildren should not be true');
    let dynamicCategoryIcoItem5 = await ($(matMenuContent.categoryIcon));
    await expect(dynamicCategoryIcoItem5).toBeDisplayed({ wait: 3000, message: '<eui-dropdown-button-menu-Items> dynamic categoryIconItem5 -> is not displayed on page' });
    if (browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(dynamicCategoryIcoItem5, testFolder + 'img_dynamicCategoryIcoItem5', { hideElements: [await $('//eui-app-toolbar')] })).equal(0, 'Image comparison failed for dynamicCategoryIcoItem5');
    }
    await chaiExpect(await matMenuContent.hasIconClass()).equal(true, 'hasIconClass should not be true');

    childMenuItems = await ($(matMenuContent.euiDropdownButtonChildMenuCO.currentPath));
    await expect(childMenuItems).toBeExisting({ wait: 3000, message: '<eui-dropdown-button-child-menu -> child menu Items -> is not existing on page' });

  });
});
