import Routing from '../../../../pages/eui/10x/routing-10x.js'
import CookieBannerActions from '../../../../actions/cookieBanner.js'
import EuiTextareaPage from '../../../../pages/eui/10x/eui/eui-textarea-10x.po.js'
import { elementAttributeContains } from '../../../../core/utils.js'

describe('eUI - <eui-textarea> Component Object ', async () => {
  let page = new EuiTextareaPage();
  let testFolder = './/eui-textarea//';
  let imageToCompare;
  it('should navigate to <eui-textarea> demo page', async () => {
    browser.addTestMeta({uuid:"4f90c788-cc51-11ec-9d64-0242ac120002", zephyrKey: "CCQA-2054", zephyrStep: "484622", reportGroups: ["eUI.10x.eui"]});
    await page.open(Routing.getEUIDirectivesUrl('eui-textarea'));
    await CookieBannerActions.cookieBanner(true);
    let pgTitle = await $(page.pageTitle);
    await pgTitle.scrollIntoView();
    await expect(await $(pgTitle)).toBeDisplayedInViewport({wait:1000, message:'<euiTextArea> page title is not displayed on page'});
    await expect(await $(page.appDocPageById('euiTextArea'))).toExist({wait:1000, message: 'Page Id is not existing on page'});
    await chaiExpect(await pgTitle.getText()).equal('euiTextArea', 'Page title text is not as expected');
    await expect(await $(page.navigationList)).toBeDisplayedInViewport({wait:1000, message:'Navigation List -> is not displayed on page'});
  });
 it('should verify the presence and number of component objects <eui-textarea> on page ', async () => {
    browser.addTestMeta({zephyrStep: "484623"});
    let sectionId= 'Default';
    let sectionElement = await $(page.sectionById(sectionId));
    await expect(sectionElement).toExist('<' + sectionId + '> section -> does not exist on page');
    await sectionElement.scrollIntoView();
    await expect(sectionElement).toBeDisplayed({message:'<' + sectionId + '> section -> is not displayed on page'});
    let index = 1;
    let text = 'It was a dark and stormy night...';
    let textareaByIndex1 = await (await page.textareaByIndex(index).element());
    let textareaByText = await (await page.textareaByTextContains(text).element());
    let textareaByTextAndIndex = await (await page.textareaByTextEqualsAndIndex(text, 1).element());
    await expect(textareaByIndex1).toBeDisplayed({wait:1000, message:'textarea by index '+ index +' is not displayed on page'});
    await expect(textareaByText).toBeDisplayed({wait:1000, message:'textarea by text  '+ text +' is not displayed on page'});
    await expect(textareaByTextAndIndex).toBeDisplayed({wait:1000, message:'textarea by index '+ index +' and text  '+ text +' is not displayed on page'});
    if (browser.config.env.endsWith('DEV')){
      await chaiExpect(await page.textareaByIndex(index).defaultComponentsCount()).equal(18, 'There should be 18 <eui-textarea> components on the page.');
    }
    else{
      await chaiExpect(await page.textareaByIndex(index).defaultComponentsCount()).equal(18, 'There should be 18 <eui-textarea> components on the page.');
    }
    if(browser.config.useWdioImageComparison) {
      await textareaByIndex1.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_textareaByIndex1Dev' : 'img_textareaByIndex1');
      await chaiExpect(await browser.checkElement(textareaByIndex1, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for textareaByIndex1');
      await textareaByText.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_textareaByTextDev' : 'img_textareaByText');
      await chaiExpect(await browser.checkElement(textareaByText, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for textareaByText');
      await textareaByTextAndIndex.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_textareaByTextAndIndexDev' : 'img_textareaByTextAndIndex');
      await chaiExpect(await browser.checkElement(textareaByTextAndIndex, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for textareaByTextAndIndex');
    }
  });
  it('should verify the <eui-textarea> read-only ', async () => {
    browser.addTestMeta({zephyrStep: "484624"});
    let sectionId = 'readonly';
    let sectionElement = await $(page.sectionById(sectionId));
    await expect(sectionElement).toExist('<' + sectionId + '> section -> does not exist on page');
    await sectionElement.scrollIntoView();
    await expect(sectionElement).toBeDisplayed({wait:1000, message:'<' + sectionId + '> section -> is not displayed on page'});
    let textareaReadOnly = page.textareaFromSectionWithIdByIndex(sectionId, 2);
    await expect(await (await textareaReadOnly.element())).toExist({wait:1000,message:'textareaReadOnly is not existing on page'});
    await chaiExpect(await elementAttributeContains(textareaReadOnly.currentPath, 'readonly', 'false')).equal(false, 'textareaReadOnly is read-only');
  });

   it('should verify the <eui-textarea> disabled ', async () => {
    browser.addTestMeta({zephyrStep: "484625"});
    let sectionId = 'disabled';
    let sectionElement = await $(page.sectionById(sectionId));
    await expect(sectionElement).toExist('<' + sectionId + '> section -> does not exist on page');
    await sectionElement.scrollIntoView();
    await expect(sectionElement).toBeDisplayed({wait:1000, message:'<' + sectionId + '> section -> is not displayed on page'});
    let textareaDisabled = await (await page.textareaFromSectionWithIdByIndex(sectionId, 1).element());

    await expect(textareaDisabled).toBeDisplayed({wait:1000, message:'textareaDisabled is not displayed on page'});
    await expect(textareaDisabled).toHaveAttr('disabled');

    if(browser.config.useWdioImageComparison) {
      await textareaDisabled.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_textareaDisabledDev' : 'img_textareaDisabled');
      await chaiExpect(await browser.checkElement(textareaDisabled, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for textareaDisabled');
    }
  });
  it('should verify the <eui-textarea> invalid', async () => {
    browser.addTestMeta({zephyrStep: "484626"});
    let sectionId = 'invalid';
    let sectionElement = await $(page.sectionById(sectionId));
    await expect(sectionElement).toExist('<' + sectionId + '> section -> does not exist on page');
    await sectionElement.scrollIntoView();

    let textareaInvalid = await (await page.textareaFromSectionWithIdByIndex(sectionId, 2).element());
    await expect(textareaInvalid).toBeDisplayed({wait:1000, message:'textareaInvalid is not displayed on page'});
    await chaiExpect(await elementAttributeContains(page.textareaFromSectionWithIdByIndex(sectionId, 2).currentPath, 'class', 'invalid')).equal(true, 'textareaInvalid is not invalid');
    if(browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(textareaInvalid, testFolder+'img_textareaInvalid', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for textareaInvalid');
    }
     let toggleInvalidStateButton = await $(page.buttonComponentByTextContains('Toggle invalid state').currentPath);
     await toggleInvalidStateButton.click();
     let textareaValid = await (await page.textareaFromSectionWithIdByIndex(sectionId, 2).element());
    await expect(textareaValid).toBeDisplayed({wait:1000, message:'textareaValid is not displayed on page'});
    await chaiExpect(await elementAttributeContains(page.textareaFromSectionWithIdByIndex(sectionId, 2).currentPath, 'class', 'invalid')).equal(false, 'textareaRtextareaValideadOnly is invalid');
    if(browser.config.useWdioImageComparison) {
      await textareaValid.scrollIntoView();
      await browser.deductedWait(2000);
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_textareaValidDev' : 'img_textareaValid');
      await chaiExpect(await browser.checkElement(textareaValid, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for textareaValid');
    }
  });
  it('should verify the <eui-textarea> placeholder', async () => {
    browser.addTestMeta({zephyrStep: "484627"});
    let sectionId = 'placeholder';
    let sectionElement = await $(page.sectionById(sectionId));
    await expect(sectionElement).toExist('<' + sectionId + '> section -> does not exist on page');
    await sectionElement.scrollIntoView();

    let textareaPlaceholder = await (await page.textareaFromSectionWithIdByIndex(sectionId, 1).element());
    await expect(textareaPlaceholder).toBeDisplayed({wait:1000, message:'textareaPlaceholder is not displayed on page'});
    await expect(textareaPlaceholder).toHaveAttr('placeholder');
    if(browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(textareaPlaceholder, testFolder+'img_textareaPlaceholder', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for textareaPlaceholder');
    }
  });
  it('should verify the <eui-textarea> auto-resize ', async () => {
    browser.addTestMeta({zephyrStep: "484628"});
    let sectionId = 'auto-resize';
    let sectionElement = await $(page.sectionById(sectionId));
    await expect(sectionElement).toExist('<' + sectionId + '> section -> does not exist on page');
    await sectionElement.scrollIntoView();

    let textareaAutoresize = await (await page.textareaFromSectionWithIdByIndex(sectionId, 1).element());
    await expect(textareaAutoresize).toBeDisplayed({wait:1000, message:'textareaAutoresize is not displayed on page'});
    await expect(textareaAutoresize).toHaveAttr('autoresize');
    if(browser.config.useWdioImageComparison) {
      await chaiExpect(await browser.checkElement(textareaAutoresize, testFolder+'img_textareaAutoresize', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for textareaAutoresize');
    }
  });
});
