import Routing from '../../../pages/eui/14x/routing.js'
import EuiMessageBoxPage from '../../../pages/eui/14x/eui-message-box.po.js'
import * as CookieBannerActions from '../../../actions/cookieBanner.js';
import BaseActions from '../../../actions/V14/base-actions.js';

describe('eUI - <eui-message-box> Component Object ', async () => {
  let page = new EuiMessageBoxPage();
  let testFolder = './/v14x//eui-message-box//';
  let pageName = 'eui-message-box'
  it('should navigate to <eui-message-box> demo page', async () => {
    browser.addTestMeta({uuid:"aadceb31-0d6a-4dfe-862f-98cdbca684ed", zephyrKey: "CCQA-1805", zephyrStep: "444701", reportGroups: ["eUI.14x.eui"]});
    await page.open(Routing.getEUIComponentUrl('eui-message-box'));
    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-message-box> on page ', async () => {
    browser.addTestMeta({zephyrStep: "444702"});
    let sectionId= 'default';
    await BaseActions.verifySection(sectionId, pageName);

    let messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Open Message Box').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    let messageBox = await(await page.euiDialogContainerCo.element());
    await expect(messageBox).toBeDisplayedInViewport({wait: 2000, message:'Message Box window is not displayed on page'});

    await browser.pause(1000);
    if(browser.config.useWdioImageComparison) {
      await (await $(page.euiDialogContainerCo.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(page.euiDialogContainerCo.content), testFolder+'img_MessageBoxByIndex1',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxByIndex1');
    }
  });
 it('should verify the Message Box header, content, accept and dismiss methods', async () => {
    browser.addTestMeta({zephyrStep: "444703"});
    let messageBox = page.euiDialogContainerCo;
    await expect(await $(messageBox.headerTitle)).toBeDisplayedInViewport({wait: 2000, message:'Message Box header is not displayed on page'});
    await expect(await $(messageBox.headerTitle)).toHaveText('Message box title', {wait: 2000, message:'Message Box header content is not as expected'});

    await chaiExpect(await messageBox.hasHeaderTilte()).equal(true, 'Message Box header should have title');
    await expect(await $(messageBox.euiButtonCO.currentPath)).not.toBeDisplayedInViewport({wait: 2000, message:'Message Box button in header is displayed'});
    await expect(await $(messageBox.closeIconCO.currentPath)).not.toBeDisplayedInViewport({wait: 2000, message:'Message Box close icon is displayed'});
    await expect(await $(messageBox.body)).toBeDisplayedInViewport({wait: 2000, message:'Message Box Body content is not displayed'});
    await expect(await $(messageBox.dismissButton.currentPath)).toBeDisplayedInViewport({wait: 2000, message:'Message Box dismiss button is not displayed'});
    await expect(await $(messageBox.acceptButton.currentPath)).toBeDisplayedInViewport({wait: 2000, message:'Message Box accept button is not displayed'});
    if(browser.config.useWdioImageComparison) {
      await (await $(page.euiDialogContainerCo.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(messageBox.dismissButton.currentPath), testFolder+'img_MessageBoxDismissButton',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxDismissButton');
      await chaiExpect(await browser.checkElement(await $(messageBox.acceptButton.currentPath), testFolder+'img_MessageBoxAcceptButton',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxAcceptButton');
    }
    await (await (await messageBox.acceptButton.element())).click();

    let sectionId = 'default';
    await BaseActions.verifySection(sectionId, pageName);
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'});
    let messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Open Message Box').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await browser.pause(1000);
    await expect(await (await messageBox.element())).toBeDisplayedInViewport({wait: 3000, message:'Message Box window is not displayed on page'});

    if(browser.config.useWdioImageComparison) {
      await (await $(page.euiDialogContainerCo.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(page.euiDialogContainerCo.content), testFolder+'img_MessageBoxNoCloseButton',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxNoCloseButton');
    }

    await expect(await $(messageBox.closeIconCO.currentPath)).not.toBeDisplayedInViewport({wait: 2000, message:'Message Box close icon is displayed'});
    await (await $(messageBox.dismissButton.currentPath)).click();
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 3000, message:'Message Box should not be displayed after clicking on close button'});

    sectionId = 'draggable';
    await BaseActions.verifySection(sectionId, pageName);
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'});
    messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Open Message Box').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await chaiExpect(await messageBox.isDraggable()).equal(true, 'Message Box isDraggable should be true');
    await expect(await  $(messageBox.dismissButton.currentPath)).toBeDisplayedInViewport({wait: 2000, message:'Message Box dismissButton is not displayed on page'});
    await (await $(messageBox.dismissButton.currentPath)).click();
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 5000, message:'Message Box should not be displayed after clicking on close button'});

    sectionId = 'disable-buttons';
    await BaseActions.verifySection(sectionId, pageName);
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'});
    messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Open MessageBox - No dismiss button').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await expect(await $(messageBox.dismissButton.currentPath)).not.toBeDisplayedInViewport({wait: 2000, message:'Message Box dismiss button is displayed'});
    await expect(await $(messageBox.acceptButton.currentPath)).toBeDisplayedInViewport({wait: 2000, message:'Message Box accept button is not displayed'});
    await (await $(messageBox.acceptButton.currentPath)).click();
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 15000, message:'Message Box should not be displayed after clicking on close button'});

    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'});
    messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Open MessageBox - No accept button').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await expect(await $(messageBox.dismissButton.currentPath)).toBeDisplayedInViewport({wait: 2000, message:'Message Box dismiss button is not displayed'});
    await expect(await $(messageBox.acceptButton.currentPath)).not.toBeDisplayedInViewport({wait: 2000, message:'Message Box accept button is displayed'});
    await (await $(messageBox.dismissButton.currentPath)).click();
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 15000, message:'Message Box should not be displayed after clicking on close button'});
  });

  it('should verify the Message Box body severity warning', async () => {
    browser.addTestMeta({zephyrStep: "444704"});
    let sectionId = 'typeclass';
    await BaseActions.verifySection(sectionId, pageName);
    let messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Warning').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await browser.pause(1000);
    let messageBox = page.euiDialogContainerCo;
    await expect(await (await messageBox.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box window is not displayed on page'});
    await chaiExpect(await messageBox.severity()).equal('warning or second', 'Message Box severity is not as expected');

    if(browser.config.useWdioImageComparison) {
      await (await $(messageBox.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(messageBox.content), testFolder+'img_MessageBoxWarning',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxWarning');
    }

    await (await $(messageBox.dismissButton.currentPath)).click();
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'})
  });
  it('should verify the Message Box body severity danger', async () => {
    browser.addTestMeta({zephyrStep: "444705"});
    let sectionId= 'typeclass';
    await BaseActions.verifySection(sectionId, pageName);

    let messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Danger').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await browser.pause(1000);
    let messageBox = page.euiDialogContainerCo;
    await expect(await (await messageBox.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box window is not displayed on page'});
    await chaiExpect(await messageBox.severity()).equal('danger', 'Message Box severity is not as expected');

    if(browser.config.useWdioImageComparison) {
      await (await $(messageBox.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(messageBox.content), testFolder+'img_MessageBoxDanger',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxDanger');
    }

    await (await $(messageBox.dismissButton.currentPath)).click();
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'})
  });
  it('should verify the Message Box body severity success', async () => {
    browser.addTestMeta({zephyrStep: "444706"});
    let sectionId= 'typeclass';
    await BaseActions.verifySection(sectionId, pageName);

    let messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Success').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await browser.pause(1000);
    let messageBox = page.euiDialogContainerCo;
    await expect(await (await messageBox.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box window is not displayed on page'});
    await chaiExpect(await messageBox.severity()).equal('success', 'Message Box severity is not as expected');

    if(browser.config.useWdioImageComparison) {
      await (await $(messageBox.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(messageBox.content), testFolder+'img_MessageBoxSuccess',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxSuccess');
    }

    await (await $(messageBox.dismissButton.currentPath)).click();
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'})
  });
  it('should verify the Message Box body severity Info', async () => {
    browser.addTestMeta({zephyrStep: "444707"});
    let sectionId= 'typeclass';
    await BaseActions.verifySection(sectionId, pageName);

    let messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Info').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await browser.pause(1000);
    let messageBox = page.euiDialogContainerCo;
    await expect(await (await messageBox.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box window is not displayed on page'});
    await chaiExpect(await messageBox.severity()).equal('info', 'Message Box severity is not as expected');

    if(browser.config.useWdioImageComparison) {
      await (await $(messageBox.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(messageBox.content), testFolder+'img_MessageBoxInfo',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxInfo');
    }

    await (await $(messageBox.dismissButton.currentPath)).click();
    await expect( await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'})
  });
  xit('should verify the Message Box body severity Question', async () => {
    browser.addTestMeta({zephyrStep: "444708"});
    let sectionId= 'typeclass';
    await BaseActions.verifySection(sectionId, pageName);

    let messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Question').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await browser.pause(1000);
    let messageBox = page.euiDialogContainerCo;
    await expect(await (await messageBox.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box window is not displayed on page'});
    await chaiExpect(await messageBox.severity()).equal('question', 'Message Box severity is not as expected');

    if(browser.config.useWdioImageComparison) {
      await (await $(messageBox.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(messageBox.content), testFolder+'img_MessageBoxQuestion',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6, 'Image comparison failed for img_MessageBoxQuestion');
    }

    await (await $(messageBox.dismissButton.currentPath)).click();
    await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'})

    sectionId= 'button-label';
    await BaseActions.verifySection(sectionId, pageName);

    messageBoxButton = page.euiButtonCOFromSectionWithId(sectionId).byTextContains('Open Message Box').andByIndex(1);
    await expect(await (await messageBoxButton.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box button is not displayed on page'});
    await (await $(messageBoxButton.currentPath)).click();
    await browser.pause(1000);
    messageBox = page.euiDialogContainerCo;
    await expect( await (await messageBox.element())).toBeDisplayedInViewport({wait: 2000, message:'Message Box window is not displayed on page'});

    if(browser.config.useWdioImageComparison) {
      await (await $(messageBox.content)).scrollIntoView();
      await chaiExpect(await browser.checkElement(await $(messageBox.content), testFolder+'img_MessageBoxButtonLabel',
      {hideElements: [await $('//eui-app-toolbar')]})).within(0, 6.2, 'Image comparison failed for img_MessageBoxButtonLabel');
    }

    await (await $(messageBox.dismissButton.currentPath)).click();
   await expect(await (await messageBox.element())).not.toBeDisplayed({wait: 2000, message:'Message Box should not be displayed after clicking on close button'})

  });

});
