import Routing from '../../../../pages/eui/10x/routing-10x.js'
import CookieBannerActions from '../../../../actions/cookieBanner.js'
import UxDateRangeSelectorPage from '../../../../pages/eui/10x/ux/ux-date-range-selector-10x.po.js'
import CoUxDateRangeSelector from '../../../../components/10x/ux/ux-date-range-selector.js'
import DatepickerActions from '../../../../actions/datepicker-actions.js'

describe('eUI - <ux-date-range-selector> component object', async() => {
  let dateRangePage = new UxDateRangeSelectorPage();
  Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
  }
  it('should navigate to <ux-date-range-selector> component', async() => {
    browser.addTestMeta({uuid:"5bb3f18f-e344-4f6d-b726-d8db56a7a315", zephyrKey: "CCQA-1064", zephyrStep: "399776", reportGroups: ["eUI.10x.ux"]});
    await dateRangePage.open(Routing.getUxComponentUrl('ux-date-range-selector'));
    await CookieBannerActions.cookieBanner(true);
    let pgTitle = await $(dateRangePage.pageTitle);
    let appDocPageId = await $(dateRangePage.appDocPageById('ux-date-range-selector'));
    let navigationList = await $(dateRangePage.navigationList);
    await pgTitle.scrollIntoView();
   await expect(pgTitle).toBeDisplayed({wait:1000, message:'<ux-date-range-selector> page title is not displayed on page'});
   await expect(pgTitle).toHaveText('ux-date-range-selector', {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 displayed on page'});
  });
  it('should verify the presence of the Component Object on the page', async() => {
    browser.addTestMeta({zephyrStep: "399777"});

    let currentSection = 'Default';
    await expect(await $(dateRangePage.dateRangeFromSectionById(currentSection).currentPath)).toBeExisting({timeout: 5000,
      timeoutMsg: '<Default> section -> is not existing on page', interval: 100
    });

    let dateRange1 = dateRangePage.dateRangeFromSectionByIdAndIndex(currentSection, 1);
    await expect(await $(dateRange1.dataE2E)).toBeExisting({ timeout: 1000,  timeoutMsg:
      'ux-date-range-selector from section' + currentSection + ' and by index "1" is not existing on page', interval: 100 });

    let uxDateRangeCo = new CoUxDateRangeSelector(dateRangePage.mainContent);
   await expect(await uxDateRangeCo.defaultComponentsCount()).toBe(8,
      'There should be 8 <ux-date-range-selector> components on the page.');
  });
  it('should verify component elements', async() => {
    browser.addTestMeta({zephyrStep: "399778"});

    let currentSection = 'Default';
    let section = await $(dateRangePage.sectionById(currentSection));
    await section.scrollIntoView();

    let dateRange1 = dateRangePage.dateRangeFromSectionByIdAndIndex(currentSection, 1);

    // date placeholders
   await expect(await dateRange1.firstPickerCo.inputDateFormat()).toBe('dd/mm/yyyy');
   await expect(await dateRange1.secondPickerCo.inputDateFormat()).toBe('dd/mm/yyyy');

    // labels
    let firstLabel = await $(dateRange1.firstLabelCo.labelContentLabel);
   await expect(firstLabel).toBeDisplayed({ message: 'date-range-selector 1 - start label is not displayed on page' });
   await expect(firstLabel).toHaveTextContaining('Start Date');

    let secondLabel = await $(dateRange1.secondLabelCo.labelContentLabel);
   await expect(secondLabel).toBeDisplayed({ message: 'date-range-selector 1 - end label is not displayed on page' });
   await expect(secondLabel).toHaveTextContaining('End Date');

    // set dates
    let datePickerFrom = dateRange1.firstPickerCo;
    await DatepickerActions.selectFromDayInCurrentMonth(datePickerFrom, '15');

    let datePickerTo = dateRange1.secondPickerCo;
    await DatepickerActions.selectToDayInCurrentMonth(datePickerTo, '20');
  });
  it('should verify default values', async() => {
    browser.addTestMeta({zephyrStep: "399779"});

    let currentSection = 'Default';
    let section = await $(dateRangePage.sectionById(currentSection));
    await section.scrollIntoView();

    let dateRange1 = dateRangePage.dateRangeFromSectionByIdAndIndex(currentSection, 2);

    // date placeholders
   await expect(await dateRange1.firstPickerCo.inputDateFormat()).toBe('dd/mm/yyyy');
   await expect(await dateRange1.secondPickerCo.inputDateFormat()).toBe('dd/mm/yyyy');

    // values
    let today = new Date();
    DatepickerActions.verifySelectedDayWithFormat(dateRange1.firstPickerCo, today, 'dd/mm/yyyy');
    let toDate = today.addDays(7);
    await DatepickerActions.verifySelectedDayWithFormat(dateRange1.secondPickerCo, toDate, 'dd/mm/yyyy');
  });
  it('should verify buttons icons', async() => {
    browser.addTestMeta({zephyrStep: "399780"});

    let currentSection = 'icon-button';
    let section = await $(dateRangePage.sectionById(currentSection));
    await section.scrollIntoView();

    let dateRange1 = dateRangePage.dateRangeFromSectionByIdAndIndex(currentSection, 1);
   await expect(await $(dateRange1.firstPickerCo.datepickerButton.buttonIcon)).toBeDisplayed({
      message: 'date-range-selector 1 - button icon for "from" datepicker is not displayed on page' });

   await expect(await $(dateRange1.secondPickerCo.datepickerButton.buttonIcon)).toBeDisplayed({
        message: 'date-range-selector 1 - button icon for "to" datepicker is not displayed on page' });
  });
  it('should verify the datepickers labels', async() => {
    browser.addTestMeta({zephyrStep: "399781"});

    let currentSection = 'labels';
    let section = await $(dateRangePage.sectionById(currentSection));
    await section.scrollIntoView();

    let dateRange1 = dateRangePage.dateRangeFromSectionByIdAndIndex(currentSection, 1);
    let firstLabel = await $(dateRange1.firstLabelCo.labelContentLabel);
   await expect(firstLabel).toBeDisplayed({ message: 'date-range-selector 1 - start label is not displayed on page' });
   await expect(firstLabel).toHaveTextContaining('First Label');

    let secondLabel = await $(dateRange1.secondLabelCo.labelContentLabel);
   await expect(secondLabel).toBeDisplayed({ message: 'date-range-selector 1 - end label is not displayed on page' });
   await expect(secondLabel).toHaveTextContaining('Second Label');
  });
  it('should verify the datepickers placeholders', async() => {
    browser.addTestMeta({zephyrStep: "399782"});

    let currentSection = 'placeholder';
    let section = await $(dateRangePage.sectionById(currentSection));
    await section.scrollIntoView();

    let dateRange1 = dateRangePage.dateRangeFromSectionByIdAndIndex(currentSection, 1);
   await expect(await dateRange1.firstPickerCo.inputDateFormat()).toBe('Start Date');
   await expect(await dateRange1.secondPickerCo.inputDateFormat()).toBe('End Date');
  });
  it('should verify the datepickers maxdate', async() => {
    browser.addTestMeta({zephyrStep: "399783"});

    let currentSection = 'max-date';
    let section = await $(dateRangePage.sectionById(currentSection));
    await section.scrollIntoView();

    let dateRange1 = dateRangePage.dateRangeFromSectionByIdAndIndex(currentSection, 1);
    let today = new Date();
    await DatepickerActions.verifySelectedDayIsDisabled(dateRange1.firstPickerCo, today.addDays(1));
    await DatepickerActions.verifySelectedDayIsDisabled(dateRange1.secondPickerCo, today.addDays(1));
  });
});
