import Routing from '../../../../pages/eui/10x/routing-10x.js'
import FileUploadPage from '../../../../pages/eui/10x/eui/eui-file-upload-10x.po.js'
import CookieBannerActions from '../../../../actions/cookieBanner.js'
import {join} from 'path'

describe('eUI - <eui-file-upload> Component Object',async () => {
  let page = new FileUploadPage();
  let testFolder = './/eui-file-upload//';
  it('should navigate to <eui-file-upload> demo page',async () => {
    browser.addTestMeta({uuid:"35362fbe-4221-11ec-81d3-0242ac130003", zephyrKey: "CCQA-1792", zephyrStep: "443897", reportGroups: ["eUI.10x.eui"]});
    await page.open(Routing.getEUIComponentUrl('eui-file-upload'));
    await CookieBannerActions.cookieBanner(true);
    let pgTitle = await $(page.pageTitle);
    await pgTitle.scrollIntoView();
    await expect(await $(pgTitle)).toBeDisplayedInViewport({wait:3000, message:'<eui-file-upload> page title is not displayed on page'});
    await expect(await $(page.appDocPageById('eui-file-upload'))).toExist({message:'Page Id is not existing on page'});
    await expect(pgTitle).toHaveText('eui-file-upload', {wait:1000,message:'Page title text is not as expected'});
    await expect(await $(page.navigationList)).toBeDisplayedInViewport({message:'Navigation List -> is not displayed on page'});
  });
  it('should verify the presence and number of component objects <eui-file-upload> on page ',async () => {
    browser.addTestMeta({zephyrStep: "443898"});
    let sectionId = 'Default';
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    let index = 1;
    let text = 'Choose file';
    let imageToCompare;
    let fileUploadByIndex = await (await page.fileUploadByIndex(index).element());
    let fileUploadByText = await (await page.fileUploadByTextContains(text).element());
    let fileUploadByTextAndIndex = await (await page.fileUploadByTextAndIndex(text, index).element());
    await expect(await $(page.sectionById(sectionId))).toBeDisplayed({message:'<'+sectionId+'> section -> is not displayed on page'});
    await expect(fileUploadByIndex).toBeDisplayed({message:'FileUpload by index "'+index+'" is not displayed on page'});
    await expect(fileUploadByText).toBeDisplayed({message:'FileUpload by text "'+text+'" is not displayed on page'});
    await expect(fileUploadByTextAndIndex).toBeDisplayed({message:'FileUpload by text "'+text+'" and by index "'+index+'" is not displayed on page'});
    await chaiExpect(await page.fileUploadByIndex(index).defaultComponentsCount()).equal(12, 'There should be 12 <eui-file-upload> components on the page.');
    if(browser.config.useWdioImageComparison) {
      await fileUploadByIndex.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_fileUploadByIndex1Dev' : 'img_fileUploadByIndex1');
      await chaiExpect(await browser.checkElement(fileUploadByIndex, testFolder + imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for fileUploadByIndex1');
      await fileUploadByText.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_fileUploadByText1Dev' : 'img_fileUploadByText1');
      await chaiExpect(await browser.checkElement(fileUploadByText, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for fileUploadByText1');
      await fileUploadByTextAndIndex.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_fileUploadByTextAndIndex1Dev' : 'img_fileUploadByTextAndIndex1');
      await chaiExpect(await browser.checkElement(fileUploadByTextAndIndex, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for fileUploadByTextAndIndex1');
    }
  });
  it('should verify drag and drop area',async () => {
    browser.addTestMeta({zephyrStep: "443899"});
    let sectionId= 'enable-drag-area';
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    let fileUploadFromSection = page.fileUploadFromSectionById(sectionId);
    let dropArea = await (await page.fileUploadFromSectionById(sectionId).element());
    let chooseFile = await $(fileUploadFromSection.chooseFile);
    let dragAndDropMsg = await $(fileUploadFromSection.dragAndDropMsg);
    let dropAreaFileInput = await $(fileUploadFromSection.dropAreaFileInput);
    await expect(dropArea).toBeDisplayed({message:'dropArea is not displayed on page'});
    await chaiExpect(await fileUploadFromSection.hasDragArea()).equal(true, 'hasDragArea    should be true');
    await expect(chooseFile).toBeDisplayed({message:'chooseFile is not displayed on page'});
    await expect(dragAndDropMsg).toBeDisplayed({message:'dragAndDropMsg is not displayed on page'});
    await expect(dropAreaFileInput).toExist({message:'dropAreaFileInput is not existing on page'});
    if(browser.config.useWdioImageComparison) {
      await dropArea.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_dropAreaDev' : 'img_dropArea');
      await chaiExpect(await browser.checkElement(dropArea, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dropArea');
      await chooseFile.scrollIntoView();
      await chaiExpect(await browser.checkElement(chooseFile, testFolder+'img_chooseFile', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for chooseFile');
      await dragAndDropMsg.scrollIntoView();
      await chaiExpect(await browser.checkElement(dragAndDropMsg, testFolder+'img_dragAndDropMsg', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dragAndDropMsg');
      await dropAreaFileInput.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_dropAreaFileInputDev' : 'img_dropAreaFileInput');
      await chaiExpect(await browser.checkElement(dropAreaFileInput, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for dropAreaFileInput');
    }
  });
  it('should verify simple input file',async () => {
    browser.addTestMeta({zephyrStep: "443901"});
    let sectionId= 'Default';
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    let fileUploadFromSection = page.fileUploadFromSectionById(sectionId);
    let simpleInputFile = await $(fileUploadFromSection.simpleInputFile);
    let chooseSimpleFile = await $(fileUploadFromSection.chooseFileForSimpleInputFileLabel);
    let buttonChooseSimpleFile = await $(fileUploadFromSection.euiButtonChooseFileCO.currentPath);
    let multipleFilesInput = await $(fileUploadFromSection.multipleFilesInput);
    await expect(simpleInputFile).toBeDisplayed({message:'simpleInputFile is not displayed on page'});
    await expect(chooseSimpleFile).toBeDisplayed({message:'chooseSimpleFile is not displayed on page'});
    await expect(buttonChooseSimpleFile).toBeDisplayed({message:'buttonChooseSimpleFile is not displayed on page'});
    await expect(multipleFilesInput).toExist({message:'multipleFilesInput is not existing on page'});
    if(browser.config.useWdioImageComparison) {
      await simpleInputFile.scrollIntoView();
      await chaiExpect(await browser.checkElement(simpleInputFile, testFolder+'img_simpleInputFile', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for simpleInputFile');
      await chooseSimpleFile.scrollIntoView();
      await chaiExpect(await browser.checkElement(chooseSimpleFile, testFolder+'img_chooseSimpleFile', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for chooseSimpleFile');
      await buttonChooseSimpleFile.scrollIntoView();
      await chaiExpect(await browser.checkElement(buttonChooseSimpleFile, testFolder+'img_buttonChooseSimpleFile', {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for buttonChooseSimpleFile');
    }
    await chaiExpect(await fileUploadFromSection.isMultiple()).equal(true, 'isMultiple should be true');
  });
  it('should verify reset button',async () => {
    browser.addTestMeta({zephyrStep: "443932"});
    let sectionId= 'enable-drag-area';
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    let input = await $(page.fileUploadFromSectionById(sectionId).dropAreaFileInput);
    await expect(input).toExist({message:'input is not existing on page'});
    const filePath = './packages/eui-automation/tests/test files/eui-file-upload.PNG'
    const remoteFilePath = await browser.uploadFile(join(process.cwd(), filePath));
    await (await $(input)).setValue(remoteFilePath);
    await browser.pause(4000);
    let fileUploadFromSection = page.fileUploadFromSectionById(sectionId);
    let filePreview = await $(page.filePreviewFromSection(sectionId).currentPath);
    let resetButton = await $(fileUploadFromSection.fileUploadResetButtonCO.currentPath);
    await expect(filePreview).toBeDisplayed({wait:3000,message:'filePreview is not displayed on page'});
    await expect(resetButton).toBeDisplayed({wait:3000,message:'resetButton is not displayed on page'});
    if(browser.config.useWdioImageComparison) {
      await filePreview.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_filePreview1Dev' : 'img_filePreview1');
      await chaiExpect(await browser.checkElement(filePreview, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for filePreview1');
      await resetButton.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_resetButtonDev' : 'img_resetButton');
      await chaiExpect(await browser.checkElement(resetButton, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for resetButton');
    }
    await chaiExpect(await fileUploadFromSection.hasResetButton()).equal(true, 'hasResetButton should be true');
    await chaiExpect(await fileUploadFromSection.hasProgressBar()).equal(true, 'hasProgressBar should be true');
  });
  it('should verify file preview',async () => {
    browser.addTestMeta({zephyrStep: "443964"});
    let sectionId= 'preview-icon';
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    let fileUploadFromSection = page.fileUploadFromSectionById(sectionId);
    await chaiExpect(await fileUploadFromSection.hasResetButton()).equal(false, 'hasResetButton should be false');
    await chaiExpect(await fileUploadFromSection.hasTotalSize()).equal(false, 'hasTotalSize should be false');
    let input = await $(page.fileUploadFromSectionById(sectionId).dropAreaFileInput);
    await expect(input).toExist({message:'input is not existing on page'});
    const filePath = './packages/eui-automation/tests/test files/eui-file-upload.PNG'
    const remoteFilePath = await browser.uploadFile(join(process.cwd(), filePath))
    await (await $(input)).setValue(remoteFilePath);
    await browser.pause(3000);
    let filePreview = await $(page.filePreviewFromSection(sectionId).currentPath);
    let resetButton = await $(fileUploadFromSection.fileUploadResetButtonCO.currentPath);
    let fileUploadTotalSize = await $(fileUploadFromSection.fileUploadTotalSize);
    await expect(filePreview).toBeDisplayed({wait:3000,message:'filePreview is not displayed on page'});
    await expect(resetButton).toBeDisplayed({wait:3000,message:'resetButton is not displayed on page'});
    await expect(fileUploadTotalSize).toBeDisplayed({wait:3000,message:'fileUploadTotalSize is not displayed on page'});
    if(browser.config.useWdioImageComparison) {
      await resetButton.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_resetButtonDev' : 'img_resetButton');
      await chaiExpect(await browser.checkElement(resetButton, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for resetButton');
      await fileUploadTotalSize.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_fileUploadTotalSizeDev' : 'img_fileUploadTotalSize');
      await chaiExpect(await browser.checkElement(fileUploadTotalSize, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for fileUploadTotalSize');
    }
    await chaiExpect(await fileUploadFromSection.hasResetButton()).equal(true, 'hasResetButton should be true');
    await chaiExpect(await fileUploadFromSection.hasProgressBar()).equal(true, 'hasProgressBar should be true');
    await chaiExpect(await fileUploadFromSection.hasTotalSize()).equal(true, 'hasTotalSize should be true');
  });
  it('should verify file uploaded with no preview',async () => {
    browser.addTestMeta({zephyrStep: "443965"});
    let sectionId= 'no-preview';
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    let fileUploadFromSection = page.fileUploadFromSectionById(sectionId);
    let input = await $(page.fileUploadFromSectionById(sectionId).dropAreaFileInput);
   await expect(input).toExist({message:'input is not existing on page'});
    const filePath = './packages/eui-automation/tests/test files/eui-file-upload.PNG'
    const remoteFilePath = await browser.uploadFile(join(process.cwd(), filePath))
    await (await $(input)).setValue(remoteFilePath);
    await browser.pause(3000);
    let filePreview = await $(page.filePreviewFromSection(sectionId).currentPath);
    let filePreviewFile = await $(fileUploadFromSection.euiFilePreviewFileCO.currentPath);
    let previewUploadedFile = await $(fileUploadFromSection.euiFilePreviewUploadedFileCO.currentPath);
   await expect(filePreview).toBeDisplayed({wait:3000,message:'filePreview is not displayed on page'});
   await expect(filePreviewFile).not.toBeDisplayed({wait:3000,message:'filePreviewFile is displayed on page'});
   await expect(previewUploadedFile).not.toBeDisplayed({wait:3000,message:'previewUploadedFile is not displayed on page'});
  });
  it('should verify no reset button ',async () => {
    browser.addTestMeta({zephyrStep: "443966"});
    let sectionId= 'no-reset-button';
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    let fileUploadFromSection = page.fileUploadFromSectionById(sectionId);
    let input = await $(page.fileUploadFromSectionById(sectionId).dropAreaFileInput);
    await expect(input).toExist({message:'input is not existing on page'});
    const filePath = './packages/eui-automation/tests/test files/eui-file-upload.PNG'
    const remoteFilePath = await browser.uploadFile(join(process.cwd(), filePath))
    await (await $(input)).setValue(remoteFilePath);
    await browser.pause(3000);
    let filePreview = await $(page.filePreviewFromSection(sectionId).currentPath);
    await expect(filePreview).toBeDisplayed({wait:3000,message:'filePreview is not displayed on page'});
    await chaiExpect(await fileUploadFromSection.hasResetButton()).equal(false, 'hasResetButton should be false');
    if(browser.config.useWdioImageComparison) {
      await filePreview.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_filePreview2Dev' : 'img_filePreview2');
      await chaiExpect(await browser.checkElement(filePreview, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for filePreview2');
      }
  });
  it('should verify predefined-values ',async () => {
    browser.addTestMeta({zephyrStep: "443967"});
    let sectionId= 'predefined-values';
    let scrollIntoSection = await $(page.sectionById(sectionId));
    await scrollIntoSection.scrollIntoView();
    let filePreview = await $(page.filePreviewFromSection(sectionId).currentPath);
    let fileUploadFromSection = page.fileUploadFromSectionById(sectionId);
    let simpleInputFile = await $(fileUploadFromSection.simpleInputFile);
    let chooseSimpleFile = await $(fileUploadFromSection.chooseFileForSimpleInputFileLabel);
    let multipleFilesInput = await $(fileUploadFromSection.multipleFilesInput);
    await expect(filePreview).toBeDisplayed({wait:3000,message:'filePreview is not displayed on page'});
    await expect(simpleInputFile).toBeDisplayed({message:'simpleInputFile is not displayed on page'});
    await expect(chooseSimpleFile).toBeDisplayed({message:'chooseSimpleFile is not displayed on page'});
    await expect(multipleFilesInput).toExist({message:'multipleFilesInput is not existing on page'});
    if(browser.config.useWdioImageComparison) {
      await filePreview.scrollIntoView();
      imageToCompare =(browser.config.env.endsWith('DEV') ? 'img_filePreview3Dev' : 'img_filePreview3');
      await chaiExpect(await browser.checkElement(filePreview, testFolder+imageToCompare, {hideElements: [await $('//eui-app-toolbar')]})).equal(0, 'Image comparison failed for filePreview3');
      }
  });
});
