import {
  Logs,
  Utility,
  utilitySelector,
  TripTileType,
  Timeouts
} from '@utility-lib/index';

export const rentalCarSelector = {
  location: '[formcontrolname="pickUpLocation"] input',
  calendarButton: '[class^="ta-date-trigger-v2 ta-date"]',
  nextMonthCalendarButton:
    '[class="popover popover-calendar enter"] [aria-label="next month"]',
  searchRentalCar: '.home-search-form__actions .ta-button__content',
  searchRentalCarGuest: '.search-form-car-invitation button.ta-button--primary-color',
  tripCheckoutAddToTrip: '[qaid="tripCheckoutAddToTrip"]',
  dropOffSelect: '[qaid="carDropOffTypeSelect"]',
  dropDownButton: '.ta-dropdown button',
  eventSearchButton: '.search-form-car-invitation__bottom-row button',
  userGroupPassengers: '[class^="user-group-v2__passengers"]',
  popoverContentLocation: (location: string) =>
    `.popover-content [aria-label^="${location}"]`,
  dropOffTypeInput: (dropOffType: string) => `[formcontrolname="${dropOffType}"] input`,
  dropOffTypeLabel: (dropOffType: string) => `[aria-label="${dropOffType}"]`,
  dAndCInfo: '[class^="car-search-form__delivery-and-collect-info"]',
  locationInput: '[class="popover-content"] [aria-label]'
};

export class RentalCar {
  static visitRentalCarSearch(tripTileType: TripTileType) {
    Utility.visitSearchPage();
    if (tripTileType === 'Personal') {
      Utility.selectTripTileType('Personal');
    }
    Utility.selectVertical('car');
  }

  static getTravelerName() {
    cy.get(rentalCarSelector.userGroupPassengers)
      .invoke('text')
      .then((text) => {
        cy.log(text);
        cy.task('putDataInCache', {
          key: 'travelerName',
          data: text
        });
      });
  }

  static selectPickUpLocation(pickUpLocation: string) {
    cy.allure().logStep('Select pick up location');
    this.setLocation(pickUpLocation, 'pickUpLocation');
  }

  static selectDropOffLocation(dropOffLocation: string) {
    cy.allure().logStep('Select drop off location');
    this.setLocation(dropOffLocation, 'dropOffLocation');
  }

  static setLocation(location: string, dropOffType: string) {
    cy.allure().logStep(`set rental car pickup location: ${location}`);
    cy.clearInputAndType(rentalCarSelector.dropOffTypeInput(dropOffType), `${location}`);
    cy.wait('@waitForLocation');
    cy.get(rentalCarSelector.popoverContentLocation(location)).eq(0).trigger('click', {
      force: true
    });
  }

  static setPickUpAndDropOffDate() {
    const numberOfMonthsAfterStartDate = 6;
    const newDayOfMonth = 1;
    const numberOfDaysAfterDeparture = 5;
    cy.allure().logStep(
      `set pick up and drop off date after ${numberOfMonthsAfterStartDate} months and ${numberOfDaysAfterDeparture} days`
    );
    Utility.selectFutureDate(
      numberOfMonthsAfterStartDate,
      newDayOfMonth,
      numberOfDaysAfterDeparture
    );
  }

  static searchRentalCar() {
    cy.allure().logStep(`Search rental car `);
    cy.get(rentalCarSelector.searchRentalCar).click();
    cy.wait('@waitForCarSearchResults');
    cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
    cy.get('body').then(($body) => {
      if ($body.find('[class="ta-modal-body"]').length > 0) {
        cy.get('[class="ta-modal-body"]').should('not.exist');
      }
    });
  }

  static amadeusAirportLocationSearch() {
    cy.allure().logStep(`Search rental car `);
    cy.intercept('GET', /api\/v1\/trip\/car\/searches/).as(
      'waitForAmadeusCarSearchResults'
    );
    cy.get(rentalCarSelector.searchRentalCar).click();
    cy.wait('@waitForAmadeusCarSearchResults').then((Interception) => {
      expect(Interception.response?.statusCode).to.eq(400);
    });
  }

  static eventSearchRentalCar() {
    cy.allure().logStep('search rental car');
    cy.get(utilitySelector.dataTestID('car')).click();
    cy.get(rentalCarSelector.eventSearchButton).should('be.visible').click();
    cy.wait('@waitForCarSearchResults').then((xhr) => {
      Logs.cypressResponseLog('EVENT SEARCH RENTAL CAR RESPONSE', xhr);
    });
  }

  static searchRentalCarGuest() {
    cy.allure().logStep('search rental car');
    cy.get(rentalCarSelector.searchRentalCarGuest).click();
    cy.wait('@waitForCarSearchResults');
  }

  static selectDropOffType(dropOffType: string) {
    cy.allure().logStep(`select drop off type: ${dropOffType}`);

    cy.get(rentalCarSelector.dropDownButton).click();

    cy.get(rentalCarSelector.dropOffTypeLabel(dropOffType)).click();
  }

  static createRentalCarPolicy(maxCarPrice: number) {
    cy.allure().logStep('create rental car policy');
    cy.createRentalCarPolicyWithHardApproval(maxCarPrice);
  }
}
