import Routing from '../../../../pages/eui/10x/routing-10x.js'
import CookieBannerActions from '../../../../actions/cookieBanner.js'
import AlertPage from '../../../../pages/eui/10x/ux/ux-alert-10x.po.js'

describe('eUI - <ux-alert> Component Object ', async() => {
  let alertPage = new AlertPage();
  it('should navigate to <ux-alert> component', async() => {
    browser.addTestMeta({uuid:"a5265ba0-71f7-11eb-9439-0242ac130002", zephyrKey: "CCQA-557", zephyrStep: "318986", reportGroups: ["eUI.10x.ux"]});
    await alertPage.open(Routing.getUxComponentUrl('ux-alert'));
    await CookieBannerActions.cookieBanner(true);
    let pgTitle = await $(alertPage.pageTitle);
    let appDocPageId = await $(alertPage.appDocPageById('ux-alert'));
    let navigationList = await $(alertPage.navigationList);
    await pgTitle.scrollIntoView();
    await expect(pgTitle).toBeDisplayed({wait:1000, message:'<ux-alert> page title is not visible on page'});
    await expect(pgTitle).toHaveText('ux-alert', {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 of the component object page', async() => {
    browser.addTestMeta({zephyrStep: "318987"});
    let sectionId = 'Default';
    let defaultSection = await $(alertPage.sectionById(sectionId));
    await defaultSection.scrollIntoView();
    await expect(defaultSection).
      toBeDisplayedInViewport({message:'<' + sectionId + '> section -> is not visible on page'});

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

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

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

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

    sectionId = 'content-togglable';
    let toggableSection = await $(alertPage.sectionById(sectionId));
    await toggableSection.scrollIntoView();
    await expect(toggableSection).
      toBeDisplayedInViewport({message:'<' + sectionId + '> section -> is not visible on page'});
    await chaiExpect(await alertPage.alertByIndex(1).defaultComponentsCount()).equal(18, 'There should be 12 <ux-alert> components on the page.')
  });

  it('should verify the <ux-alert> identification methods', async() => {
    browser.addTestMeta({zephyrStep: "376362"});
    let index = 1;
    let alertByIndex = await $(alertPage.alertByIndex(index).currentPath);
    await expect(alertByIndex).toExist({message:'Alert with index ' + index + ' not existing on page'});
    await alertByIndex.scrollIntoView();

    let text = 'this is the content of the alert strong text';
    let alertByTextContains = await $(alertPage.alertByTextContains(text).currentPath);
    await expect(alertByTextContains).
      toExist({message:'Alert with text ' + text + ' not existing on page'});
    await alertByTextContains.scrollIntoView();

    let sectionId = 'Default';
    let alertFromSection = await $(alertPage.alertFromSectionWithIdByIndex(sectionId, 1).currentPath);
    await expect(alertFromSection).
      toExist({message:'Alert from section "' + sectionId + '" and with index: ' + index + 'not existing on page'});
    await alertFromSection.scrollIntoView();
  });
  it('should verify the <ux-alert> typeClass', async() => {
    browser.addTestMeta({zephyrStep: "376403"});
    let sectionId = 'typeClass';
    await(await $(alertPage.sectionById(sectionId))).scrollIntoView();
    await expect(await $(alertPage.alertFromSectionWithIdByIndex(sectionId, 1).dataE2E)).toHaveAttrContaining('class', 'ux-alert--info');
    await expect(await $(alertPage.alertFromSectionWithIdByIndex(sectionId, 2).dataE2E)).toHaveAttrContaining('class', 'ux-alert--success');
    await expect(await $(alertPage.alertFromSectionWithIdByIndex(sectionId, 3).dataE2E)).toHaveAttrContaining('class', 'ux-alert--warning');
    await expect(await $(alertPage.alertFromSectionWithIdByIndex(sectionId, 4).dataE2E)).toHaveAttrContaining('class', 'ux-alert--danger');
  });
  it('should verify the closeableState of a specific CO and the presence of close button', async() => {
    browser.addTestMeta({zephyrStep: "318989"});
    let sectionId = 'typeClass';
    await expect(await alertPage.alertFromSectionWithIdByIndex(sectionId, 1).isCloseable()).toBe(false);

    sectionId = 'isCloseable';
    await (await $(alertPage.sectionById(sectionId))).scrollIntoView();
    await expect(await alertPage.alertFromSectionWithIdByIndex(sectionId, 1).isCloseable()).toBe(true);

    // alert can be closed by pressing the close button
    let alert = alertPage.alertFromSectionWithId(sectionId);
    let countBefore = await(await $$(alert.dataE2E)).length;
    await (await $(alert.alertClose)).click();
    let countAfter = await (await $$(alert.dataE2E)).length;
    await expect(countBefore).toBe(countAfter+1);
  });
  it('should verify if <ux-alert> isMuted', async() => {
    browser.addTestMeta({zephyrStep: "376425"});
    let sectionId = 'isMuted';
    await (await $(alertPage.sectionById(sectionId))).scrollIntoView();

    await expect(await alertPage.alertFromSectionWithIdByIndex(sectionId, 1).isMuted()).toBe(true);
    await expect(await alertPage.alertFromSectionWithIdByIndex(sectionId, 2).isMuted()).toBe(true);
    await expect(await alertPage.alertFromSectionWithIdByIndex(sectionId, 3).isMuted()).toBe(true);
    await expect(await alertPage.alertFromSectionWithIdByIndex(sectionId, 4).isMuted()).toBe(true);
  });
  it('should verify the presence of Alert Icon on specific CO', async() => {
    browser.addTestMeta({zephyrStep: "318990"});
    let iconAlert = alertPage.alertFromSectionWithIdByIndex('iconClass', 1);
    await expect(await $(iconAlert.currentPath)).toExist({message:'Alert with icon not existing on page.'});
    await (await $(iconAlert.currentPath)).scrollIntoView();

    await expect(await $(iconAlert.alertIcon)).toBeDisplayedInViewport({message:'Icon of the alert is not displayed on page.'});
  });
  it('should verify the presence of a Link in a specific CO', async() => {
    browser.addTestMeta({zephyrStep: "318991"});
    let al = alertPage.alertFromSectionWithIdByIndex('iconClass', 1);
    await expect(await $(al.getAlertLinkByText('link'))).
      toBeDisplayedInViewport({message:'Link is not displayed in the alert component.'});
  });
  it('should verify <ux-alert> component is toggable', async() => {
    browser.addTestMeta({zephyrStep: "318999"});
    let sectionId = 'content-togglable';
    let toggableAlert = alertPage.alertFromSectionWithIdByIndex(sectionId, 1);
    await expect(await $(toggableAlert.currentPath)).
      toExist({message:'Toggable <ux-alert> not existing on page.'});
    await (await $(toggableAlert.currentPath)).scrollIntoView();

    // alert content can be shown or hidden if we click on a link
    await expect(await $(toggableAlert.toggableContent)).not.
      toBeDisplayedInViewport({message:'Toggable content is displayed, and it should be hidden.'});
    await (await $(toggableAlert.getAlertLinkByText('Toggle content'))).click();
    await expect(await $(toggableAlert.toggableContent)).
      toBeDisplayedInViewport({message:'Toggable content is not displayed after cliking the link to display it.'});
  });
});
