import { homePageSelectors } from '@travelxen-lib/selectors/home';

export class TravelXenHomePage {
  static retrieveCreatedUserAndSearchByEmail() {
    cy.allure().logStep('retrieve user information to search');
    cy.task('getDataFromCache', 'userResponse').then((response: any) => {
      this.searchUser(response.email.replace(/\.[a-zA-Z]+$/, ''));
    });
  }

  static retrieveCreatedBookingAndSearchById() {
    cy.allure().logStep('retrieve booking id to search');
    cy.task('getDataFromCache', 'bookingConfirmation').then((createdBookingId: any) => {
      this.searchBookingById(createdBookingId);
    });
  }

  static switchToSearchByBookingIdOption() {
    cy.allure().logStep('switch to search by booking id option');
    cy.get(homePageSelectors.bookingSearchButton).first().click();
  }

  static searchBookingById(bookingId: string) {
    cy.allure().logStep('search booking by booking id');

    this.switchToSearchByBookingIdOption();
    cy.clearInputAndType(homePageSelectors.bookingSearchField, bookingId);
    cy.get(homePageSelectors.bookingSearchItem).contains(bookingId);
    cy.wait('@searchBookings');
  }

  static searchUser(query: string) {
    cy.allure().logStep('search user by name');
    cy.clearInputAndType(homePageSelectors.userSearchField, query);
    cy.wait('@searchUsers');
    cy.get(homePageSelectors.userSearchItem).contains(query);
  }

  static selectBookingFromSearchResult(index = 0) {
    cy.allure().logStep('click on booking from search results');
    cy.get(homePageSelectors.bookingSearchItem).eq(index).click();
  }

  static selectUserFromSearchResult(index = 0) {
    cy.allure().logStep('click on user from search results');
    cy.get(homePageSelectors.userSearchItem).eq(index).click();
  }
}
