import Routing from '../../../../pages/eui/10x/routing-10x.js'
import CookieBannerActions from '../../../../actions/cookieBanner.js'
import EuiDropdownButtonPage from '../../../../pages/eui/10x/eui/eui-dropdown-button-10x.po.js';

describe('eUI - <eui-dropdown-button> Component Object ',async () => {
  let dropdownButtonPage = new EuiDropdownButtonPage();
  let testFolder = './/eui-dropdown-button//';
  it('should navigate to <eui-dropdown-button> demo page',async () => {
    browser.addTestMeta({uuid:"ebbdd7d0-54f0-4b7b-b7fd-cf86900bbc95", zephyrKey: "CCQA-1284", zephyrStep: "413510", reportGroups: ["eUI.10x.eui"]});
    await dropdownButtonPage.open(Routing.getEUICompositionsComponentUrl('eui-dropdown-button'));
    await CookieBannerActions.cookieBanner(true);
    let pgTitle = await $(dropdownButtonPage.pageTitle);
    let appDocPageId = await $(dropdownButtonPage.appDocPageById('eui-dropdown-button'));
    let navigationList = await $(dropdownButtonPage.navigationList);

   await expect(pgTitle).toBeDisplayed({wait:1000, message:'<eui-dropdown-button> page title is not visible on page'});
   await expect(pgTitle).toHaveText('eui-dropdown-button', {wait: 1000, message: 'Page title text is not as expected'});
   await expect(appDocPageId).toExist({wait:1000, message:'Page Id is not present in page'});
   await expect(navigationList).toBeDisplayed({wait:1000, message:'Navigation List -> is not visible on page'});
  });
  it('should verify the section titles and total number of CO on page',async () => {
    browser.addTestMeta({zephyrStep: "413512"});
    let sectionId = 'Default';
    let section = await $(dropdownButtonPage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not visible on page'});

    sectionId = 'type-classes';
    section = await $(dropdownButtonPage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not visible on page'});

    sectionId = 'disabled';
    section = await $(dropdownButtonPage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not visible on page'});

    sectionId = 'outline';
    section = await $(dropdownButtonPage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not visible on page'});

    sectionId = 'sizes';
    section = await $(dropdownButtonPage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not visible on page'});

    sectionId = 'misc-compositions';
    section = await $(dropdownButtonPage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not visible on page'});

    await chaiExpect(await dropdownButtonPage.dropdownButtonCo.defaultComponentsCount()).equal(30, 'Components on page should be 29');
  });
  it('should verify the <eui-dropdown> <eui-list> <eui-list-item>',async () => {
    browser.addTestMeta({zephyrStep: "413513"});
    let sectionId = 'Default';
    let section = await $(dropdownButtonPage.sectionById(sectionId));
    let imageComparisonDropdownButton = await (await dropdownButtonPage.dropdownFromSectionWithIdByIndex(sectionId, 1).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonDropdownButton.scrollIntoView();
      if (browser.config.env.endsWith('DEV')){
        await chaiExpect(await browser.checkElement(imageComparisonDropdownButton, testFolder+'img_DropdownButton_ByIndex1Dev',
        {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_DropdownButton_ByIndex1');
      }
      else{
        await chaiExpect(await browser.checkElement(imageComparisonDropdownButton, testFolder+'img_DropdownButton_ByIndex1',
        {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_DropdownButton_ByIndex1');
      }
    }
    await section.scrollIntoView();
    let co = dropdownButtonPage.dropdownFromSectionWithIdByIndex('Default', 1);

    await expect(await $(co.byTextEquals('Default').currentPath)).toBeDisplayed({wait: 3000, message:'<eui-dropdown> CO by text -> is not visible on page'});
    await expect(await $(co.byTextEquals('Default').matmenu)).toBeExisting({message:'<eui-dropdown> - matmenu -> is not present on page'});
    await (await $(co.euiButtonCO.currentPath)).click();
    await expect(await $(co.euiDropdownContentCO.currentPath)).toBeDisplayed({wait: 3000, message:'<eui-dropdown-button> dropdown content -> is not visible on page'});

    //verify the presence of the first list item
    await expect(await $(dropdownButtonPage.euiListCo.currentPath)).toBeDisplayed({wait: 3000, message:'<eui-list> -> is not visible on page'});
    await expect(await $(dropdownButtonPage.euiListCo.euiListItemCO.currentPath)).toBeDisplayed({wait: 3000, message:'<eui-list-item> -> is not visible on page'});
    await expect(await $(dropdownButtonPage.euiListCo.euiListItemCO.byTextContains('Secondary action 2').currentPath)).toBeDisplayed({wait: 3000,
      message:'<eui-list-item> by text: "Secondary action 1" -> is not visible on page'});

    // verify that option 4 is disabled
    let option5 = dropdownButtonPage.euiListCo.euiListItemCO.byIndex(5);
    let option7 = dropdownButtonPage.euiListCo.euiListItemCO.byIndex(7);
    await chaiExpect(await option5.isDisabled()).equal(false, 'Option 5 should not be disabled');
    await chaiExpect(await option7.isDisabled()).equal(true, 'Option 7 should be disabled');

    //click on the second option
    option = await $(dropdownButtonPage.euiListCo.euiListItemCO.byTextContains('Secondary action 2').currentPath);
    await option.click();
    await expect(await $(dropdownButtonPage.euiListCo.currentPath)).not.toBeDisplayed({wait: 3000, message:'<eui-list> -> is still visible on page'});
  });
  it('should verify the <eui-dropdown> severity method',async () => {
    browser.addTestMeta({zephyrStep: "413516"});
    let sectionId = 'type-classes';
    let section = await $(dropdownButtonPage.sectionById(sectionId));
    let imageComparisonDropdownButton = await (await dropdownButtonPage.dropdownFromSectionWithIdByIndex(sectionId, 1).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonDropdownButton.scrollIntoView();
      if (browser.config.env.endsWith('DEV')){
        await chaiExpect(await browser.checkElement(imageComparisonDropdownButton, testFolder+'img_DropdownButton_typeClasses_ByIndex1Dev',
        {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_DropdownButton_typeClasses_ByIndex1');
      }
      else{
        await chaiExpect(await browser.checkElement(imageComparisonDropdownButton, testFolder+'img_DropdownButton_typeClasses_ByIndex1',
        {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_DropdownButton_typeClasses_ByIndex1');
      }
    }
    await section.scrollIntoView();
    await expect(await (await $(dropdownButtonPage.euiButtonCOfromSectionWithIdByIndex('type-classes', 2).currentPath)).getAttribute('class')).toContain('eui-button--primary');
    await expect(await (await $(dropdownButtonPage.euiButtonCOfromSectionWithIdByIndex('type-classes', 3).currentPath)).getAttribute('class')).toContain('eui-button--secondary');
    await expect(await (await $(dropdownButtonPage.euiButtonCOfromSectionWithIdByIndex('type-classes', 4).currentPath)).getAttribute('class')).toContain('eui-button--success');
    await expect(await (await $(dropdownButtonPage.euiButtonCOfromSectionWithIdByIndex('type-classes', 5).currentPath)).getAttribute('class')).toContain('eui-button--info');
    await expect(await (await $(dropdownButtonPage.euiButtonCOfromSectionWithIdByIndex('type-classes', 6).currentPath)).getAttribute('class')).toContain('eui-button--warning');
    await expect(await (await $(dropdownButtonPage.euiButtonCOfromSectionWithIdByIndex('type-classes', 7).currentPath)).getAttribute('class')).toContain('eui-button--danger');
    await expect(await (await $(dropdownButtonPage.euiButtonCOfromSectionWithIdByIndex('type-classes', 8).currentPath)).getAttribute('class')).toContain('eui-button--accent');
    });
  it('should verify the <eui-button> is disabled method',async () => {
    browser.addTestMeta({zephyrStep: "413517"});
    let sectionId = 'disabled';
    let section = await $(dropdownButtonPage.sectionById(sectionId));
    let imageComparisonDropdownButton = await (await dropdownButtonPage.dropdownFromSectionWithIdByIndex(sectionId, 1).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonDropdownButton.scrollIntoView();
      if (browser.config.env.endsWith('DEV')){
        await chaiExpect(await browser.checkElement(imageComparisonDropdownButton, testFolder+'img_DropdownButton_disabled_ByIndex1Dev',
        {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_DropdownButton_disabled_ByIndex1');
      }
      else{
        await chaiExpect(await browser.checkElement(imageComparisonDropdownButton, testFolder+'img_DropdownButton_disabled_ByIndex1',
        {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_DropdownButton_disabled_ByIndex1');
      }
    }
    await section.scrollIntoView();
    let tcCO = dropdownButtonPage.dropdownFromSectionWithIdByIndex('type-classes', 1)
    let dCO = dropdownButtonPage.dropdownFromSectionWithIdByIndex('disabled', 1);
    await chaiExpect(await tcCO.isDisabled()).equal(false, 'Type-classes dropdown button should not be disabled.');
    await chaiExpect(await dCO.isDisabled()).equal(true, 'Disabled dropdown button should be disabled.');
    imageComparisonDropdownButton = await (await dropdownButtonPage.dropdownFromSectionWithIdByIndex('outline', 1).element());
    if(browser.config.useWdioImageComparison) {
      await imageComparisonDropdownButton.scrollIntoView();
      if (browser.config.env.endsWith('DEV')){
        await chaiExpect(await browser.checkElement(imageComparisonDropdownButton, testFolder+'img_DropdownButton_outline_ByIndex1Dev',
        {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_DropdownButton_outline_ByIndex1');
      }
      else {
        await chaiExpect(await browser.checkElement(imageComparisonDropdownButton, testFolder+'img_DropdownButton_outline_ByIndex1',
        {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for img_DropdownButton_outline_ByIndex1');
      }
    }
  });
});
