import { Timeouts } from '@utility-lib/constants';
import { Utility } from '@utility-lib/utility';
import { ExploratorySearch } from './exploratory_search';
import { HotelSearchResults } from '@hotel-lib/search_results';
import { HotelCheckout } from '@hotel-lib/checkout';
import { HotelItinerary } from '@hotel-lib/itinerary';
import { createGuestDetails } from '@user-lib/factory/guest_user';
import { Hotels } from '@hotel-lib/search';

const PersonalHotelSearchResultsSelector = {
  addTravelerPill: '.navan-exploratory-search',
  payWithRewardsToggle: '.pay-with-rewards__toggle',
  fillRewardsAmount: '.cdk-text-field-autofill-monitored',
  SummaryChargeItemAmount: '[class^="charge-item gradient"] [class-label="charge-item__amount"]',
  inThisBookingBreakdown: '.hotel-in-this-booking__breakdown__rewards',
  rewardsSummary: '[qaid="hotelCheckoutLemonadeRewards"]',
  hotelLocationV2: '.ta-location-search-v2',
  itineraryRewardsPayment: '.ta-trip-event-hotel-payment__rewards',
  rewardsPaymentAmount: '.ta-trip-event-hotel-payment__rewards__amount',
  payWithRewardsToggleDetailsPage: '[class^="filter-details__rewards-toggle"]'
};
const guestDetails = createGuestDetails(40, 'FEMALE', 'Personal');

export class PersonalHotelSearchResults {
  static setHotelLocationByName(location: string) {
    cy.get(PersonalHotelSearchResultsSelector.hotelLocationV2).type(location);
    cy.wait('@waitForLocation');
    cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
    Utility.selectLocationFromDropdown(location);
  }
  static payWithRewardsToggleShown(): void {
    cy.allure().logStep('rewards toggle is visible');
    cy.get(PersonalHotelSearchResultsSelector.payWithRewardsToggle).should('be.visible');
  }
  static updateRewardsAmount(amount: number): void {
    cy.allure().logStep('update rewards amount');
    cy.clearInputAndType(PersonalHotelSearchResultsSelector.fillRewardsAmount, amount.toString());
  }
  static rewardsAppliedShownInSummary(): void {
    cy.allure().logStep('rewards applied shown in summary');
    cy.get(PersonalHotelSearchResultsSelector.rewardsSummary).should('be.visible');
  }
  static rewardsAmountShownInBookingBreakdown(): void {
    cy.allure().logStep('rewards amount shown in booking breakdown');
    cy.get(PersonalHotelSearchResultsSelector.inThisBookingBreakdown).should('be.visible');
  }

  static rewardsShownOnItinerary(): void {
    cy.allure().logStep('rewards shown in Itinerary payment section');
    cy.get(PersonalHotelSearchResultsSelector.itineraryRewardsPayment).should('be.visible');
  }

  static payWithRewardsToggleShownRoomDetails(): void {
    cy.allure().logStep('rewards toggle is visible on room details page');
    cy.get(PersonalHotelSearchResultsSelector.payWithRewardsToggleDetailsPage).should('be.visible');
  }

  static runFullRedemptionBookingFlow(location: string): void {
    Hotels.setCheckInCheckOutDate();
    PersonalHotelSearchResults.setHotelLocationByName(location);
    Hotels.searchHotel();
    PersonalHotelSearchResults.payWithRewardsToggleShown();
    ExploratorySearch.selectHotel(0);
    HotelSearchResults.selectPrepayRoomFilter();
    PersonalHotelSearchResults.payWithRewardsToggleShownRoomDetails();
    HotelSearchResults.selectRoomOption();
    HotelCheckout.waitToBeLoaded();
    HotelCheckout.setTripInfo();
    HotelCheckout.hotelCancellationAndChargePolicy();
    HotelCheckout.acceptHotelBookingPolicy();
    ExploratorySearch.clickTravelerDropdown(0);
    //ExploratorySearch.selectTravelerAtHotelCheckout(' Select Traveler for room 1 ');
    ExploratorySearch.createNewTravelerAtCheckout(guestDetails);
    cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
    PersonalHotelSearchResults.rewardsAppliedShownInSummary();
    PersonalHotelSearchResults.rewardsAmountShownInBookingBreakdown();
    HotelCheckout.acceptAcknowledgement();
    HotelCheckout.bookNow();
    HotelCheckout.closeBookingSuccessModel();
    PersonalHotelSearchResults.rewardsShownOnItinerary();
    HotelItinerary.cancelHotel();
  }
}
