import { HotelCheckout } from '@hotel-lib/checkout';
import { Random } from '@utility-lib/random';
import { Utility, utilitySelector } from '@utility-lib/utility';
import { HotelSearchResults } from '@hotel-lib/search_results';
import { Hotels } from '@hotel-lib/search';
import { LocationUtility } from '@utility-lib/location';
import { Rewards } from '../../rewards/lib/rewards';
import { Itinerary } from './itinerary';
import { FlightBookingUtility } from '@flight-modal/flight_booking_utility';
import { FlightTripType } from '@flight-lib/search';
import { Timeouts } from '@utility-lib/constants';
import { FlightUtility } from '@flight-modal/flight_utility';
import { FlightCheckout } from '@flight-lib/checkout';
export { PersonalInterceptApis } from './api-intercept';

const CheckoutUpsell = {
  checkoutUpsell: '.hotel-checkout-extend-stay',
  extendMyTripCta:
    '[class="hotel-checkout-extend-stay__button ta-button ta-button--medium-size ta-button--secondary-theme"]',
  extendTripModal: '.hotel-checkout-extend-stay-modal__header__title',
  tripToggle: '[class="hotel-extend-stay-form__button-toggle hotel-extend-stay-form__button-toggle--active"]',
  sameHotelButton: '[class="hotel-extend-stay-form__button-path hotel-extend-stay-form__button-path--active"]',
  changeHotelButton: '.hotel-extend-stay-form__button-path',
  findAvailabilityButton: '.ta-button__content',
  acceptAcknowledgement:
    '[class="hotel-checkout-summary__headless__info ng-star-inserted"] .mat-checkbox-inner-container',
  soldOutInfoShown: '[class="hotel-checkout-extend-stay-results__info-soldout ng-star-inserted"]',
  sameRoomAvailable: '[class="hotel-checkout-extend-stay-results__info-success ng-star-inserted"]',
  bleisureStatusModal: '[class="hotel-checkout-bleisure-status-modal"]',
  holdFlightButton: '[qaid="flightContractSoftHoldButton"]',
  flightHoldModal: '[class="soft-hold-details-modal"]',
  purchseHoldFlightButton: '[class*="soft-hold-details-modal__col1__purchase"]',
  releaseHoldFlightButton: '[class*="soft-hold-details-modal__col1__release"]',
  releaseHoldButtonModal: '[class*="soft-hold-release-modal__release"]',
  homePage: '.navan-home',
  paymentOwner: '[class*="ta-payment-card--owner-type"]',
  bookHoldFlightButton: '[qaid="flightContractBook"]',
  skipSeatSelection: '[qaid="checkoutTravelerSkipAncillaries"]'
};
const selectRoomButton = CheckoutUpsell.findAvailabilityButton;

const flightBookingParams = FlightBookingUtility.initFlightBookingParams({
  locations: ['SFO', 'LAX'],
  flightTripType: FlightTripType.ONEWAY,
  tripTileType: 'Personal'
});

export class Checkout {
  static selectHotelAndNavigateToCheckout(): void {
    Hotels.selectHotelLocation(LocationUtility.getCityUS());
    Hotels.setCheckInCheckOutDate();
    Hotels.searchHotel();
    HotelSearchResults.selectHotel(0);
    HotelSearchResults.selectRoomOption();
    HotelCheckout.waitToBeLoaded();
    HotelCheckout.createNewTripInfo();
  }
  static verifyCheckoutUpsell(): void {
    cy.allure().logStep('check the extend my trip banner is present and cta is clickable');
    cy.get(CheckoutUpsell.checkoutUpsell).should('exist');
    cy.get(CheckoutUpsell.extendMyTripCta).then((ele) => {
      cy.wrap(ele).click();
      cy.wrap(ele).should('exist');
    });

    cy.get(CheckoutUpsell.extendTripModal).should('have.text', 'Extend with personal nights');
    cy.get(CheckoutUpsell.findAvailabilityButton).contains('Check availability').click();
  }

  static verifyNoCheckoutUpsellShown(): void {
    cy.allure().logStep('verify checkout does not show upsell banner');
    cy.get(CheckoutUpsell.checkoutUpsell).should('not.exist');
  }

  static verifyCheckoutUpsellShown(): void {
    cy.allure().logStep('verify checkout does not show upsell banner');
    cy.get(CheckoutUpsell.checkoutUpsell).should('exist');
  }

  static extendRoom(): void {
    cy.allure().logStep('search for a room in same hotel');
    if (cy.get(CheckoutUpsell.sameRoomAvailable).should('be.visible')) {
      Checkout.extendSameRoom();
    } else if (cy.get(CheckoutUpsell.soldOutInfoShown).should('be.visible')) {
      Checkout.extendDifferentRoom();
    }
  }

  static extendRoomAtCheckout(): void {
    cy.get(utilitySelector.bodySelector).then((body) => {
      if (body.find(CheckoutUpsell.sameRoomAvailable).length > 0) {
        this.extendSameRoom();
      } else {
        this.extendDifferentRoom();
      }
    });
  }
  static extendSameRoom(): void {
    cy.allure().logStep('Same room available for extension');
    cy.log('Same room available for extension');
    cy.get(selectRoomButton).contains(' Select this room ').click();
    Checkout.bookExtension();
  }

  static extendDifferentRoom(): void {
    cy.allure().logStep('Same room not available for extension');
    cy.get(selectRoomButton).contains('View other options').click();
    HotelSearchResults.selectRoomOption();
    Checkout.bookExtension();
  }

  static updatePersonalEmail(): void {
    HotelCheckout.updateEmailDetails('personal-' + Random.randomString(12) + '@staging-prime.tripactions.xyz');
  }
  static acceptBleisureAcknowledgement(): void {
    cy.allure().logStep('accept acknowledgement');
    cy.get(CheckoutUpsell.acceptAcknowledgement).eq(0).click();
    cy.wait(3000);
    cy.get(CheckoutUpsell.acceptAcknowledgement).eq(1).click();
  }

  static bookNow() {
    cy.allure().logStep('book hotel');
    cy.get(CheckoutUpsell.findAvailabilityButton).contains(' Book now ').click();
    cy.get(CheckoutUpsell.bleisureStatusModal).should('be.visible');
    cy.get(CheckoutUpsell.findAvailabilityButton).contains('Not now').click();
  }

  static bookExtension(): void {
    cy.allure().logStep('book business and personal hotel extension');
    HotelCheckout.waitToBeLoaded();
    HotelCheckout.createNewTripInfo();
    HotelCheckout.hotelCancellationAndChargePolicy();
    HotelCheckout.setPhoneNumber();
    Checkout.updatePersonalEmail();
    Checkout.acceptBleisureAcknowledgement();
    Checkout.bookNow();
  }

  static bookExtensionForOthers(): void {
    Utility.selectVertical('hotel');
    Hotels.selectOneTraveller();
    Hotels.setCheckInCheckOutDate();
    Hotels.selectHotelLocation('Seattle, WA, United States');
    Hotels.searchHotel();
    Rewards.closeBookForOthersModal();
    Rewards.selectRewardsHotel(0);
    Rewards.selectRewardsRoomOption();
    HotelCheckout.waitToBeLoaded();
    HotelCheckout.createNewTripInfo();
    Checkout.verifyCheckoutUpsell();
    Checkout.extendRoomAtCheckout();
    Itinerary.verifyItinerary();
  }
  static clickHoldFlightButton(): void {
    cy.get(CheckoutUpsell.holdFlightButton).click();
    cy.get(CheckoutUpsell.flightHoldModal, Timeouts.MEDIUM_TIMEOUT_60_SEC).should('be.visible');
  }
  static holdFlight(): void {
    FlightUtility.runSearchFlow(flightBookingParams);
    FlightUtility.runChooseResultFlow(flightBookingParams);
    FlightCheckout.waitToPageBeLoaded();
    FlightCheckout.createTripInformation();
    FlightCheckout.checkCancellationPolicy();
    FlightCheckout.validateIfPassportAndVisaInfoRequired();
    FlightCheckout.updateEmailDetails('personal-' + Random.randomString(12) + '@staging-prime.tripactions.xyz');
    Checkout.skipSeatSelectionPersonal();
    FlightCheckout.updateContactDetails();
    Checkout.clickHoldFlightButton();
  }
  static purchaseHoldFlight(): void {
    cy.get(CheckoutUpsell.purchseHoldFlightButton).click();
    Checkout.verifyPaymentType();
  }
  static releaseHoldOnFlight(): void {
    cy.get(CheckoutUpsell.releaseHoldFlightButton).click();
    cy.get(CheckoutUpsell.releaseHoldButtonModal).click();
    cy.get(CheckoutUpsell.homePage, Timeouts.MEDIUM_TIMEOUT_30_SEC).should('be.visible');
  }

  static verifyPaymentType(): void {
    cy.get(CheckoutUpsell.paymentOwner).should('have.text', 'Personal');
  }
  static skipSeatSelectionPersonal() {
    cy.get(CheckoutUpsell.skipSeatSelection, Timeouts.SHORT_TIMEOUT_3_SEC).each((ele) => {
      cy.wrap(ele).click();
    });
    cy.allure().logStep('Seat maps were skipped');
  }
}
