import { utilitySelector, Timeouts } from '@utility-lib/index';
import { hotelSearchResultSelector } from './search_results';

const tripSelector = {
  tripCalendar: '.date-range__date-trigger .ta-date-trigger__text',
  generalButton: '.ta-button__content',
  selectRoom: '[class^="ta-hotel-room__button"] .ta-button__content',
  acceptAcknowledgement: '.info-banner__checkbox [type="checkbox"]',
  bookNewHotel: '[qaid="infoBannerPrimaryButton"]',
  seenNewBooking: '.ta-modal-footer__main-block .ta-button__content',
  closeFeedBack: '[data-testid="modal-header-close-button"]',
  otherComfortableOptionsButton: '.change-results__more-rooms button',
  changeOtherOptionsButton: '.hotel-change-search__other-button',
  changeResultsWrapper: '.change-results__wrapper',
  headsUpInformationModal: 'ta-modal.information',
  headsUpInformationModalContinue: '.ta-button--primary-theme'
};

export class HotelTrip {
  static getCalendar() {
    cy.allure().logStep('Get calendar');
    cy.get(tripSelector.tripCalendar).eq(0).click({ force: true });
  }
  static changeDates() {
    cy.allure().logStep('Change dates');
    cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
    this.getCalendar();

    this.getCalendar();
    Cypress._.times(1, () => {
      cy.get(hotelSearchResultSelector.nextMonthCalendarButton).click();
    });

    cy.get(hotelSearchResultSelector.selectDate).contains('15').click();
    cy.get(hotelSearchResultSelector.selectDate).contains('17').click();
  }

  static acceptHeadsUp() {
    cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
    cy.get(utilitySelector.bodySelector).then((headsUp) => {
      if (headsUp.find(tripSelector.headsUpInformationModal).length > 0) {
        cy.allure().logStep('accept heads up');
        cy.get(tripSelector.headsUpInformationModalContinue).last().click();
      } else {
        cy.log('heads up model not found');
        cy.allure().logStep('heads up model not found');
      }
    });
  }

  static checkHotelAvailability() {
    cy.allure().logStep('Check hotel availability');
    cy.get(tripSelector.generalButton).contains(' Check availability ').click();
    this.acceptHeadsUp();
    cy.wait('@waitForChangeHotel');
  }

  static selectRoom() {
    cy.clickIfExist(tripSelector.changeOtherOptionsButton, Timeouts.SHORT_TIMEOUT_3_SEC);
    cy.allure().logStep('Select room');
    cy.get(tripSelector.changeResultsWrapper).should('be.visible');
    cy.get(utilitySelector.bodySelector).then((body) => {
      if (!body.find(tripSelector.selectRoom).length) {
        cy.allure().logStep('Choose other comfortable options');
        cy.get(tripSelector.otherComfortableOptionsButton)
          .contains('View more rooms')
          .click();
      }
      cy.get(tripSelector.selectRoom).eq(0).contains('Select').click();
      cy.wait('@waitForCheckout');
    });
  }

  static acceptAcknowledgement() {
    cy.allure().logStep('Accept acknowledgement');
    cy.get(tripSelector.acceptAcknowledgement).click({ force: true });
  }

  static bookNewHotel() {
    cy.allure().logStep('Book new hotel');

    cy.get(tripSelector.bookNewHotel).click();
    cy.wait('@waitForCheckout');
  }
  static seeNewBooking() {
    cy.allure().logStep('See new booking');
    cy.get(tripSelector.seenNewBooking, Timeouts.MEDIUM_TIMEOUT_60_SEC)
      .contains(' See new booking ')
      .click();
  }
  static closeFeedbackModal() {
    cy.allure().logStep('Close feedback modal');
    cy.get(tripSelector.closeFeedBack).click();
  }
}
