import Routing from '../../../../pages/eui/10x/routing-10x.js'
import CookieBannerActions from '../../../../actions/cookieBanner.js'
import EuiDialogPage from '../../../../pages/eui/10x/eui/eui-dialog-10x.po.js';

describe('eUI - <eui-dialog> Component Object ',async () => {
  let dialogPage = new EuiDialogPage();
  let testFolder = './/eui-dialog//';
  it('should navigate to <eui-dialog> demo page',async () => {
    browser.addTestMeta({uuid:"ba2b86d8-aff4-46b8-9ee1-ee463734549d", zephyrKey: "CCQA-1392", zephyrStep: "414333", reportGroups: ["eUI.10x.eui"]});
    await dialogPage.open(Routing.getEUIComponentUrl('eui-dialog'));
    await CookieBannerActions.cookieBanner(true);
    let pgTitle = await $(dialogPage.pageTitle);
    await pgTitle.scrollIntoView();
    await expect(await $(pgTitle)).toBeDisplayedInViewport({wait:1000,message:'<eui-dialog> page title is not displayed on page'});
    await expect(await $(dialogPage.appDocPageById('eui-dialog'))).toExist({wait:1000,message:'Page Id is not existing on page'});
    await expect(pgTitle).toHaveText('eui-dialog', {wait:1000,message:'Page title text is not as expected'});
    await expect(await $(dialogPage.navigationList)).toBeDisplayed({wait:1000,message:'Navigation List -> is not displayed on page'});
  });
  it('should verify the section titles on page',async () => {
    browser.addTestMeta({zephyrStep: "414334"});
    let sectionId = 'Default';
    let section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'component-injection';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'scrolling';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'message-dialog';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'message-dialog-no-buttons';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'icon-button';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'typeclass';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'handle-events-manually';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'sizes';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'draggable';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'noBodyPadding';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
    sectionId = 'misc-compositions';
    section = await $(dialogPage.sectionById(sectionId));
    await expect(section).toBeDisplayed({wait: 3000, message:'Section ' + sectionId + '-> is not displayed on page'});
  });
  it('should verify the <eui-dialog> main methods',async () => {
    browser.addTestMeta({zephyrStep: "414335"});
    let sectionId = 'Default';
    section = await $(dialogPage.sectionById(sectionId));
    await section.scrollIntoView();
    let bt = dialogPage.buttonFromSectionWithIdByIndex(sectionId, 'Open Dialog', 1);
    await (await $(bt.currentPath)).click();

    let dialog = dialogPage.euiDialogCO.byTextContains('Dialog title')
    let dialogByText = await $(dialog.currentPath);
    let dialogHeader = await $(dialog.header);
    let dialogBody = await $(dialog.body);
    let dialogFooter = await $(dialog.footer);
    await expect(dialogByText).toBeDisplayed({wait: 3000, message:'<eui-dialog> dialog by text "Dialog title"-> should be displayed on page'});
    await expect(dialogHeader).toBeDisplayed({wait: 3000, message:'<eui-dialog> dialogHeader - Header -> should be displayed on page'});
    await expect(dialogBody).toBeDisplayed({wait: 3000, message:'<eui-dialog> dialogBody -> should be displayed on page'});
    await expect(dialogFooter).toBeDisplayed({wait: 3000, message:'<eui-dialog> dialogFooter -> should be displayed on page'});
    if(browser.config.useWdioImageComparison) {
      await dialogByText.scrollIntoView();
      await chaiExpect(await browser.checkElement(dialogByText, testFolder+'img_dialogByText', {hideElements: [await $('//eui-app-toolbar')]})).within(0,0.3, 'Image comparison failed for dialogByText');
      await dialogHeader.scrollIntoView();
      await chaiExpect(await browser.checkElement(dialogHeader, testFolder+'img_dialogHeader', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dialogHeader');
      await dialogBody.scrollIntoView();
      await chaiExpect(await browser.checkElement(dialogBody, testFolder+'img_dialogBody', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dialogBody');
      await dialogFooter.scrollIntoView();
      await chaiExpect(await browser.checkElement(dialogFooter, testFolder+'img_dialogFooter', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dialogFooter');
    }
  });
  it('should verify the <eui-dialog> title, close button, footer buttons and severity methods',async () => {
    browser.addTestMeta({zephyrStep: "414336"});
    let dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    let dialogHeaderTitle = await $(dialog.headerTitle);
    let modalCloseButton = await $(dialog.modalCloseButtonCO.currentPath);
    let dialogBodyText = await $(await dialog.bodyText());
    let dismissButton = await $(dialog.dismissButton.currentPath);
    let acceptButton = await $(dialog.acceptButton.currentPath);
    await expect(dialogHeaderTitle).toBeDisplayed({wait: 3000, message:
      '<eui-dialog> dialogHeaderTitle-> should be displayed on page'});
    await expect(dialogHeaderTitle).toHaveText('Dialog title', {wait: 3000, message:'<eui-dialog> CO - Header Title -> is not as expected'});
    await expect(modalCloseButton).toBeDisplayed({wait: 3000, message:'<eui-dialog> modalCloseButton -> is not displayed on page'});
    await expect(dialogBodyText).toBeDisplayed({wait: 3000, message:'<eui-dialog> dialogBodyText -> should be displayed on page'});
    await expect(dialogBodyText).toHaveText('Dialog message', {wait: 3000, message:'<eui-dialog> dialogBodyText -> is not as expected'});
    await expect(dismissButton).toBeDisplayed({wait: 3000, message:'<eui-dialog> dismissButton -> should be displayed in the modal footer'});
    await expect(await $(await dialog.dismissButton.span())).toHaveText('Cancel', {wait: 3000, message:'<eui-dialog> CO by text "Dialog title" - Dismiss Button -> should contain specific text'});
    await expect(acceptButton).toBeDisplayed({wait: 3000, message:'<eui-dialog> acceptButton -> should be displayed in the modal footer'});
    await expect(await $(await dialog.acceptButton.span())).toHaveText('OK', {wait: 3000, message:'<eui-dialog> CO by text "Dialog title" - Accept Button -> should contain specific text'});
    await chaiExpect(await dialog.hasSeverity()).equal(false, 'hasSeverity should be false');
    await expect(await dialog.severity()).toBe('Dialog doesn.\'t have severity');
    if(browser.config.useWdioImageComparison) {
      await dialogHeaderTitle.scrollIntoView();
      await chaiExpect(await browser.checkElement(dialogHeaderTitle, testFolder+'img_dialogHeaderTitle', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dialogHeaderTitle');
      await modalCloseButton.scrollIntoView();
      await chaiExpect(await browser.checkElement(modalCloseButton, testFolder+'img_modalCloseButton', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for modalCloseButton');
      await dialogBodyText.scrollIntoView();
      await chaiExpect(await browser.checkElement(dialogBodyText, testFolder+'img_dialogBodyText', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dialogBodyText');
      await dismissButton.scrollIntoView();
      await chaiExpect(await browser.checkElement(dismissButton, testFolder+'img_dismissButton', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dismissButton');
      await acceptButton.scrollIntoView();
      await chaiExpect(await browser.checkElement(acceptButton, testFolder+'img_acceptButton', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for acceptButton');
    }

    await (await $(dialog.acceptButton.currentPath)).click();
    await expect(await $(dialog.currentPath)).not.toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO by text "Dialog title" -> should not be displayed on page after cliking on OK button'});
  });
  it('should verify the <eui-dialog> severity',async () => {
    browser.addTestMeta({zephyrStep: "414337"});
    let sectionId = 'typeclass';
    section = await $(dialogPage.sectionById(sectionId));
    await section.scrollIntoView();
    let questionTypeBt = await $(dialogPage.buttonFromSectionWithIdByIndex('typeclass', 'Open Dialog Question', 1).currentPath);
    await questionTypeBt.click();
    let dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    await chaiExpect(await dialog.hasSeverity()).equal(true, 'hasSeverity should be true');
    await chaiExpect(await dialog.severity()).equal('question', 'Severity should be question');
    await (await $(dialog.acceptButton.currentPath)).click();
    infoTypeBt = await $(dialogPage.buttonFromSectionWithIdByIndex('typeclass', 'Open Dialog Info', 1).currentPath);
    await infoTypeBt.click();
    dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    await chaiExpect(await dialog.hasSeverity()).equal(true, 'hasSeverity should be true');
    await chaiExpect(await dialog.severity()).equal('info', 'Severity should be info');
    await (await $(dialog.acceptButton.currentPath)).click();
    successTypeBt = await $(dialogPage.buttonFromSectionWithIdByIndex('typeclass', 'Open Dialog Success', 1).currentPath);
    await successTypeBt.click();
    dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    await chaiExpect(await dialog.hasSeverity()).equal(true, 'hasSeverity should be true');
    await chaiExpect(await dialog.severity()).equal('success', 'Severity should be success');
    await (await $(dialog.acceptButton.currentPath)).click();
    warningTypeBt = await $(dialogPage.buttonFromSectionWithIdByIndex('typeclass', 'Open Dialog Warning', 1).currentPath);
    await warningTypeBt.click();
    dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    await chaiExpect(await dialog.hasSeverity()).equal(true, 'hasSeverity should be true');
    await chaiExpect(await dialog.severity()).equal('warning', 'Severity should be warning');
    await (await $(dialog.acceptButton.currentPath)).click();
    dangerTypeBt = await $(dialogPage.buttonFromSectionWithIdByIndex('typeclass', 'Open Dialog Danger', 1).currentPath);
    await dangerTypeBt.click();
    dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    await chaiExpect(await dialog.hasSeverity()).equal(true, 'hasSeverity should be true');
    await chaiExpect(await dialog.severity()).equal('danger', 'Severity should be danger');
    await (await $(dialog.acceptButton.currentPath)).click();
    await expect(await $(dialog.currentPath)).not.toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO by text "Dialog title" -> should NOT be displayed on page'});
    if(browser.config.useWdioImageComparison) {
      await questionTypeBt.scrollIntoView();
      await chaiExpect(await browser.checkElement(questionTypeBt, testFolder+'img_questionTypeBt', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for questionTypeBt');
      await infoTypeBt.scrollIntoView();
      await chaiExpect(await browser.checkElement(infoTypeBt, testFolder+'img_infoTypeBt', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for infoTypeBt');
      await successTypeBt.scrollIntoView();
      await chaiExpect(await browser.checkElement(successTypeBt, testFolder+'img_successTypeBt', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for successTypeBt');
      await warningTypeBt.scrollIntoView();
      await chaiExpect(await browser.checkElement(warningTypeBt, testFolder+'img_warningTypeBt', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for warningTypeBt');
      await dangerTypeBt.scrollIntoView();
      await chaiExpect(await browser.checkElement(dangerTypeBt, testFolder+'img_dangerTypeBt', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dangerTypeBt');
      }
  });
  it('should verify the <eui-dialog> no buttons',async () => {
    browser.addTestMeta({zephyrStep: "414338"});
    let sectionId = 'message-dialog-no-buttons';
    section = await $(dialogPage.sectionById(sectionId));
    await section.scrollIntoView();
    await expect(section).toBeDisplayedInViewport({wait: 3000, message:
      'Section is not displayed on page'});
    let noDismissBt = await $(dialogPage.buttonFromSectionWithIdByIndex('message-dialog-no-buttons', 'Open Dialog - No dismiss button', 1).currentPath);
    await noDismissBt.click();
    await browser.deductedWait(3000);
    let dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    let noDismissDialog = await $(dialog.currentPath);
    if(browser.config.useWdioImageComparison) {
      await noDismissDialog.scrollIntoView();
      await chaiExpect(await browser.checkElement(noDismissDialog, testFolder+'img_noDismissDialog', {hideElements: [await $('//eui-app-toolbar')]})).within(0, 2, 'Image comparison failed for noDismissDialog');
    }
    await expect(await $(dialog.dismissButton.currentPath)).not.toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO by text "Dialog title" - Dismiss Button -> should NOT be displayed in the modal footer'});
    await expect(await $(dialog.acceptButton.currentPath)).toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO by text "Dialog title" - Accept Button -> should be displayed in the modal footer'});
    await expect(await $(dialog.modalCloseButtonCO.currentPath)).not.toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO - Close button -> should NOT be displayed in the modal'});
    await (await $(dialog.acceptButton.currentPath)).click();
    noAcceptBt = await $(dialogPage.buttonFromSectionWithIdByIndex('message-dialog-no-buttons', 'Open Dialog - No accept button', 1).currentPath);
    await noAcceptBt.click();
    await browser.deductedWait(3000);
    dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    let noAcceptDialog = await $(dialog.currentPath);
    if(browser.config.useWdioImageComparison) {
      await noAcceptDialog.scrollIntoView();
      await chaiExpect(await browser.checkElement(noAcceptDialog, testFolder+'img_noAcceptDialog', {hideElements: [await $('//eui-app-toolbar')]})).within(0, 2, 'Image comparison failed for noAcceptDialog');
    }
    await expect(await $(dialog.dismissButton.currentPath)).toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO by text "Dialog title" - Dismiss Button -> should be displayed in the modal footer'});
    await expect(await $(dialog.acceptButton.currentPath)).not.toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO by text "Dialog title" - Accept Button -> should NOT be displayed in the modal footer'});
    await expect(await $(dialog.modalCloseButtonCO.currentPath)).not.toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO - Close button -> should NOT be displayed in the modal'});
    await (await $(dialog.dismissButton.currentPath)).click();
    noButtons = await $(dialogPage.buttonFromSectionWithIdByIndex('message-dialog-no-buttons', 'Open Dialog - No buttons', 1).currentPath);
    await noButtons.click();
    await browser.deductedWait(3000);
    dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    let noButtonsDialog = await $(dialog.currentPath);
    if(browser.config.useWdioImageComparison) {
      await noButtonsDialog.scrollIntoView();
      await chaiExpect(await browser.checkElement(noButtonsDialog, testFolder+'img_noButtonsDialog', {hideElements: [await $('//eui-app-toolbar')]})).within(0, 3, 'Image comparison failed for noButtonsDialog');
    }
    await expect(await $(dialog.dismissButton.currentPath)).not.toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO by text "Dialog title" - Dismiss Button -> should NOT be displayed in the modal footer'});
    await expect(await $(dialog.acceptButton.currentPath)).not.toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO by text "Dialog title" - Accept Button -> should NOT be displayed in the modal footer'});
    await expect(await $(dialog.modalCloseButtonCO.currentPath)).toBeDisplayed({wait: 3000, message:
      '<eui-dialog> CO - Close button -> should be displayed in the modal'});
    await (await $(dialog.modalCloseButtonCO.currentPath)).click();

  });
  it('should verify the <eui-dialog> draggable',async () => {
    browser.addTestMeta({zephyrStep: "414339"});
    let sectionId = 'draggable';
    section = await $(dialogPage.sectionById(sectionId));
    await section.scrollIntoView();

    let bt = dialogPage.buttonFromSectionWithIdByIndex(sectionId, 'Open Dialog', 1);
    await (await $(bt.currentPath)).click();
    let dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    await chaiExpect(await dialog.isDraggable()).equal(true, 'isDraggable should be true');
    await (await $(dialog.dismissButton.currentPath)).click();
    sectionId = 'noBodyPadding';
    section = await $(dialogPage.sectionById(sectionId));
    await section.scrollIntoView();
    bt = dialogPage.buttonFromSectionWithIdByIndex(sectionId, 'Open Dialog', 1);
    await (await $(bt.currentPath)).click();
    dialog = dialogPage.euiDialogCO.byTextContains('Dialog title');
    await chaiExpect(await dialog.isDraggable()).equal(false, 'isDraggable should be false');
    await (await $(dialog.dismissButton.currentPath)).click();
  });

});
