/**
 * These functions require the module {@link module dateformat}.
 * @requires dateformat
 */
import dateFormat from "dateformat"
/**
   * This function click year in calendar
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} year - year value eg '2023'
   */
export async function selectYear(datepickerCo, year) {
    // Check datepicker input visibility and click it
    let datepickerInput = await $(datepickerCo.input.currentPath);
    await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });
    await datepickerInput.click();
    // Check calendar visibility
    let matCalendarCo = datepickerCo.matDatepicker.matCalendarCO;
    let calendarBodyCo = matCalendarCo.calendarMultiYearViewCO.calendarBodyCO;
    await expect(await $(calendarBodyCo.currentPath)).toBeDisplayed({ message: 'Datepicker year view - calendarBody is not visible on page' });
    // Check year cell visibility and click it
    let yearCell = await $(calendarBodyCo.getCalendarCellByText(year));
    await expect(yearCell).toBeDisplayed({ message: 'Datepicker year view - cell of year '+ year +' is not visible on page' });
    await yearCell.click();
    await expect(await $(calendarBodyCo.currentPath)).not.toBeDisplayed({ message: 'Datepicker - calendarBody is still visible on page' });
}
/**
   * This function verify selected year
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} year - year value eg '2023'
   */
export async function verifySelectedYear(datepickerCo, year) {
  // Check datepicker input visibility and click it
  let datepickerInput = await $(datepickerCo.input.currentPath);
  await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });
  await datepickerInput.click();
  // Check calendar visibility
  let matCalendarCo = datepickerCo.matDatepicker.matCalendarCO;
  let calendarBodyCo = matCalendarCo.calendarMultiYearViewCO.calendarBodyCO;
  await expect(await $(calendarBodyCo.currentPath)).toBeDisplayed({ message: 'Datepicker year view - calendarBody is not visible on page' });
  // Verify selected year
  await expect(await $(calendarBodyCo.selectedCellValue)).toHaveTextContaining(year, 'Datepicker year view - selected year is not ' + year);
  let currentYear = dateFormat(new Date(), 'yyyy');
  await expect(await $(calendarBodyCo.currentCellValue)).toHaveTextContaining(currentYear,
    'Datepicker year view - selected year is not ' + currentYear);
  let yearCell = await $(calendarBodyCo.getCalendarCellByText(year));
  await expect(yearCell).toBeDisplayed({ message: 'Datepicker year view - cell of year '+ year +' is not visible on page' });
  await yearCell.click();
  await expect(await $(calendarBodyCo.currentPath)).not.toBeDisplayed({ message: 'Datepicker - calendarBody is still visible on page' });
}
/**
   * This function clear date picker
   * @async
   * @param {Component} datepickerCo - datepicker component
   */
export async function clearDatePicker(datepickerCo) {
  // Check if datepicker is visible and clearable
  await expect(await $(datepickerCo.input.currentPath)).toBeDisplayed({ message: 'Datepicker - input is not visible on page' });
  await expect(await datepickerCo.isClearable()).toBe(true, 'Datepicker - input is clearable');

  let inputValue = await (await (await $(datepickerCo.input.currentPath)).getValue());
  await expect(inputValue.length!=0).toBe(true, 'Datepicker - input doesnt contain current date');
  await (await $(datepickerCo.spanClearable)).click();
  let valueAfterClear = await (await (await $(datepickerCo.input.currentPath)).getValue());
  // Check that datepicker input value is empty
  await expect(await valueAfterClear.length===0).toBe(true, 'Datepicker - input is not empty');
}
/**
   * This function verify selected date
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} day - day value eg '12'
   */
export async function verifySelectedDay(datepickerCo, day) {
  var today = new Date();
  let datepickerInput = await $(datepickerCo.input.currentPath);
  // Check datepicker input visibility
  await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });

  if(day.length < 2) {
    day = '0'+day;
  }
  let anotherDayOfMonth = dateFormat(today, day+'/mm/yyyy');
  let inputString = await datepickerInput.getValue();
  // Verify selected day
  await expect(anotherDayOfMonth.includes(inputString)).toBe(true,'Datepicker - input has not the selected value: ' + anotherDayOfMonth);
}
/**
   * This function select a day in current month
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} day - day value eg '12'
   */
export async function selectDayInCurrentMonth(datepickerCo, day) {
  // Check datepicker input visibility and click it
  let datepickerInput = await $(datepickerCo.input.currentPath);
  await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });
  await datepickerInput.click();
  // Check calendar visibility
  let matCalendarCo = datepickerCo.matDatepicker.matCalendarCO;
  let calendarBodyCo = matCalendarCo.calendarMonthViewCO.calendarBodyCO;
  await expect(await $(matCalendarCo.currentPath)).toBeDisplayed({ message: 'Datepicker - matCalendar is not visible on page' });
  await expect(await $(calendarBodyCo.currentPath)).toBeDisplayed({ message: 'Datepicker - calendarBody is not visible on page' });
  // Check day cell visibility and click it
  let dayCell = await $(calendarBodyCo.getCalendarCellByText(day));
  await dayCell.click();
  await expect(await $(matCalendarCo.currentPath)).not.toBeDisplayed({ message: 'Datepicker - matCalendar should not be visible on page' });
}
/**
   * This function select a month in current year
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} month - month value eg 'JAN'
   */
export async function selectMonthInCurrentYear(datepickerCo, month) {
  // Check datepicker input visibility and click it
  let datepickerInput = await $(datepickerCo.input.currentPath);
  await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });
  await datepickerInput.click();
  // Check calendar visibility
  let matCalendarCo = datepickerCo.matDatepicker.matCalendarCO;
  let calendarBodyCo = matCalendarCo.calendarYearViewCO.calendarBodyCO;
  await expect(await $(calendarBodyCo.currentPath)).toBeDisplayed({ message: 'Datepicker year view - calendarBody is not visible on page' });
  // Check month cell visibility and click it
  let monthCell = await $(calendarBodyCo.getCalendarCellByText(month));
  await expect(monthCell).toBeDisplayed({message: 'Datepicker year monthly view - cell of month '+ month +' is not visible on page' });
  await monthCell.click();
  await expect(await $(calendarBodyCo.currentPath)).not.toBeDisplayed({ message: 'Datepicker - calendarBody is still visible on page' });
}
/**
   * This function verify selected month
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} month - month value eg 'JAN'
   */
export async function verifySelectedMonth(datepickerCo, month) {
  // Check datepicker input visibility and click it
  let datepickerInput = await $(datepickerCo.input.currentPath);
  await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });
  await datepickerInput.click();
  // Check calendar visibility
  let matCalendarCo = datepickerCo.matDatepicker.matCalendarCO;
  let calendarBodyCo = matCalendarCo.calendarYearViewCO.calendarBodyCO;
  await expect(await $(calendarBodyCo.currentPath)).toBeDisplayed({ message: 'Datepicker year view - calendarBody is not visible on page' });
  // Verify selected month
  await expect(await $(calendarBodyCo.selectedCellValue)).toHaveTextContaining(month, 'Datepicker year view - selected year is not ' + month);
  let currentMonth = dateFormat(new Date(), 'mmm').toUpperCase();
  await expect(await $(calendarBodyCo.currentCellValue)).toHaveTextContaining(currentMonth,
    'Datepicker year view - selected month is not ' + currentMonth);
  let monthCell = await $(calendarBodyCo.getCalendarCellByText(month));
  await expect(monthCell).toBeDisplayed({message: 'Datepicker year monthly view - cell of month '+ month +' is not visible on page' });
  await monthCell.click();
  await expect(await $(calendarBodyCo.currentPath)).not.toBeDisplayed({ message: 'Datepicker - calendarBody is still visible on page' });
}
/**
   * This function verify selected day with format
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} day - day value eg '06/06/2023'
   * @param {string} dateFrmt - date format eg 'dd/mm/yyyy'
   */
export async function verifySelectedDayWithFormat(datepickerCo, day, dateFrmt) {
  // Check datepicker input visibility
  let datepickerInput = await $(datepickerCo.input.currentPath);
  await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });
  // Verify date format
  let formattedDate = dateFormat(day, dateFrmt);
  let inputString = await datepickerInput.getValue();
  await expect(formattedDate.includes(inputString)).toBe(true,'Datepicker - input has not the selected value: ' + formattedDate);
}
/**
   * This function verify if selected day is disabled
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} day - day value eg '06'
   */
export async function verifySelectedDayIsDisabled(datepickerCo, date) {
  // Check datepicker input visibility and click it
  let datepickerInput = await $(datepickerCo.input.currentPath);
  await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });
  await datepickerInput.click();
  // Check calendar visibility
  let matCalendarCo = datepickerCo.matDatepicker.matCalendarCO;
  let calendarBodyCo = matCalendarCo.calendarMonthViewCO.calendarBodyCO;
  await expect(await $(matCalendarCo.currentPath)).toBeDisplayed({ message: 'Datepicker - matCalendar is not visible on page' });
  await expect(await $(calendarBodyCo.currentPath)).toBeDisplayed({ message: 'Datepicker - calendarBody is not visible on page' });
  // Take the day from date and check if selected day cell is disabled
  let day = dateFormat(date, 'dd');
  var intDay = parseInt(day);
  await expect(await calendarBodyCo.isCellDisabled(intDay)).toBe(true,'Day ' + intDay + 'is not disabled');

  // Dismiss the calendar by pressing Esc key
  browser.keys('\uE00C');
  await expect(await $(matCalendarCo.currentPath)).not.toBeDisplayed({ message: 'Datepicker - matCalendar should not be visible on page' });
}
/**
   * This function select from day in current month
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} day - day value eg '12'
   */
export async function selectFromDayInCurrentMonth(datepickerCo, day) {
  // Check datepicker input visibility and click it
  let datepickerInput = await $(datepickerCo.input.currentPath);
  await expect(datepickerInput).toBeDisplayed({ message: 'Datepicker - button is not visible on page' });
  await datepickerInput.click();
  // Check calendar visibility
  let matCalendarCo = datepickerCo.matDatepicker.matCalendarCO;
  let calendarBodyCo = matCalendarCo.calendarMonthViewCO.calendarBodyCO;
  await expect(await $(matCalendarCo.currentPath)).toBeDisplayed({ message: 'Datepicker - matCalendar is not visible on page' });
  await expect(await $(calendarBodyCo.currentPath)).toBeDisplayed({ message: 'Datepicker - calendarBody is not visible on page' });
  // Click day cell
  let dayCell = await $(calendarBodyCo.getCalendarCellByText(day));
  await dayCell.click();
}
/**
   * This function select to day in current month
   * @async
   * @param {Component} datepickerCo - datepicker component
   * @param {string} day - day value eg '12'
   */
export async function selectToDayInCurrentMonth(datepickerCo, day) {
  // Check calendar visibility
  let matCalendarCo = datepickerCo.matDatepicker.matCalendarCO;
  let calendarBodyCo = matCalendarCo.calendarMonthViewCO.calendarBodyCO;
  await expect(await $(matCalendarCo.currentPath)).toBeDisplayed({ message: 'Datepicker - matCalendar is not visible on page' });
  await expect(await $(calendarBodyCo.currentPath)).toBeDisplayed({ message: 'Datepicker - calendarBody is not visible on page' });
  // Click day cell
  let dayCell = await $(calendarBodyCo.getCalendarCellByText(day));
  await dayCell.click();
  await expect(await $(matCalendarCo.currentPath)).not.toBeDisplayed({ message: 'Datepicker - matCalendar should not be visible on page' });
}
