import { Timeouts } from '@utility-lib/constants';
import { hotelSearchResultSelector } from '@hotel-lib/search_results';
import { HotelSearchResults, HotelCheckout } from '@hotel-lib/index';
import { utilitySelector } from '@utility-lib/utility';
import { User_Type } from '@support-onboard/data_model/user_type';
import { LoginPage } from '@admin-lib/login';
import { hotelSelector } from '@hotel-lib/search';
import { blackCarSelector } from '@blackCar-lib/search';

const rewardsSelector = {
  rewardsButtonGlobalNav: '[data-qaid="REWARDS-button"]',
  rewardsBadge: '[data-testid="rewards-badge"]',
  rewardsSelectButton: '[class="rewards-button-loyalty"]',
  rewardsAtHotelDetails: '[class="rate-rewards"]',
  rewardsTextAtCheckout: '[data-testid="checkout-traveler-rewards"]',
  earnedRewardsAmount: '[data-testid="earnedLoyaltyRewardsAmount"]',
  earnedRewardsText: '[class="ta-guest-info__rewards-amount loyalty-rewards"]',
  infoRewardsItinerary: '.ta-trip-event-hotel-loyalty-program__rewards-info',
  closeModal: '.ta-button__content',
  bookForOthersModal: '[data-testid="rewards-delegate-modal"]',
  bookerRewardsCard: '[data-testid="checkout-booker-card"]',
  bookerInfoRewardsText:
    '.ta-booker-info__rewards-info [class="ta-booker-info__rewards-text"]',
  bookerEarnedRewardsAmount: '[data-testid="earnedLoyaltyRewardsAmount"]',
  rewardsMigrationModal: '[data-testid="rewards-migration-info-modal"]',
  rewardsPill: '[data-testid="rewardsSubtab"]',
  rewardsSideNav: '.ta-profile-side-nav__text',
  rewardsEducation: '[data-testid="rewards-education"]',
  redeemRewards: '[class="ta-payment-at-booking__rewards ng-star-inserted"]',
  profileMenuButton: '[data-qaid="USER_MENU-button"]',
  rewardsButtonMenuBar: '[data-qaid="REWARDS"]',
  rewardsBalance: '.ta-profile-rewards-overview'
};

export class Rewards {
  static verifyRewardsOnGlobalNav(): void {
    cy.allure().logStep('verify rewards on global nav');
    cy.get(rewardsSelector.rewardsButtonGlobalNav).should(`be.visible`);
  }
  static verifyRewardsBadgeShown(): void {
    cy.allure().logStep('verify rewards badge is shown at search results');
    cy.get(rewardsSelector.rewardsBadge).should('be.visible');
  }
  static selectRewardsHotel(index: number) {
    cy.allure().logStep('select rewards hotel from the list');
    HotelSearchResults.openNewHotelTab();
    cy.get(hotelSearchResultSelector.priceBlock, Timeouts.MEDIUM_TIMEOUT_60_SEC);
    HotelSearchResults.closePopup();
    cy.get(rewardsSelector.rewardsSelectButton, Timeouts.MEDIUM_TIMEOUT_60_SEC)
      .eq(index)
      .click({ force: true });
    cy.allure().logStep(`select ${index} hotel`);
  }

  static getRewardsRoom(): void {
    cy.allure().logStep('select rewards room from the options');
    cy.get(rewardsSelector.rewardsSelectButton, Timeouts.MAX_TIMEOUT_120_SEC)
      .should('be.visible')
      .eq(0)
      .scrollIntoView({ easing: 'linear' });
    cy.get(rewardsSelector.rewardsSelectButton, Timeouts.MEDIUM_TIMEOUT_60_SEC)
      .eq(0)
      .should('be.visible')
      .click({ force: true });
  }

  static selectRewardsRoomOption() {
    cy.get(rewardsSelector.rewardsAtHotelDetails).should('be.visible');
    this.getRewardsRoom();
    cy.get(utilitySelector.bodySelector).then((paymentAtCheckIn) => {
      if (paymentAtCheckIn.find(hotelSearchResultSelector.paymentAtCheckIn).length > 0) {
        cy.get(hotelSearchResultSelector.paymentAtCheckIn).contains('Continue').click();
        cy.allure().logStep('continue payment at check in');
      } else {
        cy.allure().logStep('payment not required');
      }
    });
    HotelSearchResults.acceptHeadsUp();
    HotelSearchResults.acceptRoomRate();
    HotelSearchResults.paymentRequiredWhenCheckIn();
  }

  static verifyRewardsShownAtCheckout(): void {
    cy.allure().logStep('verify earning rewards text is shown at checkout');
    cy.get(rewardsSelector.rewardsTextAtCheckout).should('be.visible');
    cy.allure().logStep('verify rewards badge is shown at checkout');
    cy.get(rewardsSelector.rewardsBadge).should('be.visible');
    cy.allure().logStep('verify rewards amount is shown at checkout');
    cy.get(rewardsSelector.earnedRewardsAmount).should('be.visible');
    cy.allure().logStep('verify rewards text is shown at checkout');
    cy.get(rewardsSelector.earnedRewardsText).contains(' in Navan rewards');
  }
  static checkoutRewardsRoom(): void {
    cy.allure().logStep('navigate to checkout page');
    HotelCheckout.createNewTripInfo();
    HotelCheckout.hotelCancellationAndChargePolicy();
    HotelCheckout.acceptHotelBookingPolicy();
    HotelCheckout.setPhoneNumber();
    HotelCheckout.checkIfProofOfVaccineRequired();
  }
  static clickBookNow(): void {
    cy.intercept('GET', /api\/user\/trips\/timeline/).as(
      'waitForBookingConfirmationModal'
    );
    HotelCheckout.acceptAcknowledgement();
    HotelCheckout.bookNow();
  }

  static waitForBookingConfirmationModal(): void {
    cy.allure().logStep('wait for booking confirmation modal to show up');
    cy.wait('@waitForBookingConfirmationModal');
  }
  static validateUpsellModal(): void {
    cy.get(rewardsSelector.rewardsBadge).should('be.visible');
    cy.get(rewardsSelector.closeModal).contains('Not now').click();
  }
  static verifyRewardsItinerary(): void {
    cy.allure().logStep('verify rewards badge is shown at itinerary');
    cy.get(rewardsSelector.rewardsBadge).should('be.visible');
    cy.allure().logStep('verify rewards info text is shown at itinerary');
    cy.get(rewardsSelector.infoRewardsItinerary).contains('in Navan rewards');
  }

  static verifyBookForOthersModal(): void {
    cy.allure().logStep('verify book for others modal is shown on search results');
    cy.get(rewardsSelector.bookForOthersModal).should('be.visible');
  }

  static closeBookForOthersModal(): void {
    cy.allure().logStep('close book for others modal');
    cy.get(rewardsSelector.closeModal).contains('Got it!').click();
  }

  static verifyBookerRewardsCardShown(): void {
    cy.allure().logStep('verify booker rewards card is shown on checkout');
    cy.get(rewardsSelector.bookerRewardsCard).should('be.visible');
    cy.allure().logStep('verify booker rewards badge is shown at checkout');
    cy.get(rewardsSelector.rewardsBadge).should('be.visible');
    cy.allure().logStep('verify booker rewards info is shown on checkout');
    cy.get(rewardsSelector.bookerInfoRewardsText).contains(
      'For booking on behalf of others'
    );
  }
  static loginAsRewardsCompanyDelegate(credentials: any) {
    const options = {
      users: [User_Type.DELEGATE],
      companySuffix: 'rewards'
    };
    LoginPage.createNewCompanyAndLoginWithUserByType(options);
    LoginPage.createNewTravelerAndLogin();
    cy.task('getDataFromCache', 'companyCreationEmail').then((email: any) => {
      cy.loginWithAuthToken({ email: email, password: credentials, isSA: false });
    });
  }
  static setTraveler(userEmail: string) {
    cy.allure().logStep('set traveller');
    cy.get(hotelSelector.users).click();
    cy.get(hotelSelector.remove).click();
    cy.get(hotelSelector.travellerInput).type(userEmail);
    cy.wait('@waitForTravellers');
    cy.get(hotelSelector.travellerDropDown).eq(0).click({ force: true });
  }

  static verifyRewardsMigrationModal(): void {
    cy.allure().logStep('verify company rewards modal is shown on search results');
    cy.get(rewardsSelector.rewardsMigrationModal).should('be.visible');
    cy.get(rewardsSelector.closeModal).contains('Got it!').click();
  }
  static loginAsRewardsDelegatedUser(email: string) {
    cy.allure().logStep(
      'login as traveler from "login as" panel to make delegate booking'
    );
    cy.get(blackCarSelector.taLoginAsPanel).should('be.visible');
    cy.get(blackCarSelector.taLoginAsSearchUser).click();
    cy.get(blackCarSelector.taLoginAsSearchUserInput).type(email);
    cy.get(blackCarSelector.taLoginAsSearchUserResult).contains(email).click();
    cy.get(utilitySelector.searchLocationInput).click();
  }
  static verifyRewardsPillShown(): void {
    cy.allure().logStep('verify rewards pill is shown');
    cy.get(rewardsSelector.rewardsPill).should(`be.visible`);
  }
  static verifyRewardsRedirection(): void {
    cy.allure().logStep('verify rewards pill redirection');
    cy.get(rewardsSelector.rewardsPill).should(`be.visible`).click();
    cy.get(rewardsSelector.rewardsSideNav).contains('Rewards');
  }

  static verifyRewardsEducation(): void {
    cy.allure().logStep('verify rewards education');
    cy.get(rewardsSelector.rewardsEducation).should(`be.visible`);
  }

  static verifyRedeemRewardsShown(): void {
    cy.allure().logStep('verify redeem rewards at payment is shown on checkout');
    cy.get(rewardsSelector.redeemRewards).should(`be.visible`);
  }

  static verifyRewardsOnGlobalNavNotShown(): void {
    cy.allure().logStep('verify rewards on global nav');
    cy.get(rewardsSelector.rewardsButtonGlobalNav).should(`not.be.exist`);
  }
  static openRewardsPage(): void {
    cy.allure().logStep('Open referral page from home');
    cy.get(rewardsSelector.profileMenuButton).click();
    cy.get(rewardsSelector.rewardsButtonMenuBar).eq(0).click();
  }
  static verifyRewardsTabContent(): void {
    cy.allure().logStep('Verify referral tab exist with copy link button');
    cy.url().should('include', '/profile?tab=rewards');
    cy.get(rewardsSelector.rewardsBalance).should('exist');
  }
}
