import { guidRegex, Utility, Timeouts } from '@utility-lib/index';
import { localizationData } from '@fixtures/commerce/localization_data';
import { CommerceShared } from '@commerce-shared-lib/index';

const english = localizationData.english;

const carsInvoiceValidationSelectors = {
  viewItinerary: '[class^="bookings-car-card__view"]',
  bookingCard: '.ta-booking-card',
  viewInvoice: '[qa-id="event-action-view-invoice"]',
  carBookingId: '[class="trip-event-card-car-list__content ng-star-inserted"]',
  totalBookingChargeOnItinerary: '.trip-event-card-car-list__content:nth-child(8)',
  carName: '.ta-trip-event-car-details__title',
  invoiceBookingId: '.grid-kv__grid__column__entry__value',
  invoiceCarName: '[qaid="provider-name"]',
  invoiceTotalPrice: '[class="price-column__amount is-total"]',
  carCheckoutPageSummary: '.checkout-card__content',
  carTotalBookingCharge: '.car-checkout__est-total div span:nth-child(1)',
  carTotalBookingChargeDueAtPickup:
    '.car-checkout__schedule div div:nth-child(2) .car-checkout__schedule-amount span:nth-child(1)',
  carTotalBookingChargeCurrency: '.car-checkout__est-total div span:nth-child(2)',
  tripFeeSummaryOfCharges: "//*[@title='Trip fee']",
  downloadInvoiceLink: '[qa-id="event-action-download-invoice"]'
};

export class CarInvoiceValidation {
  static visitTripsPage() {
    CommerceShared.visitTripsPage();
  }

  static checkItineraryPageIsVisited() {
    cy.allure().logStep(` Routing to Itinerary Page :`);
    cy.url().should(
      'include',
      `/app/user2/trips/${guidRegex}/itinerary?carUuid=${guidRegex}`
    );
  }

  static storeCarBookingData() {
    cy.get(carsInvoiceValidationSelectors.carTotalBookingCharge)
      .then((ele) => {
        cy.wrap(ele).scrollIntoView();
      })
      .then((element) => {
        cy.task('putDataInCache', {
          key: 'carTotalBookingCharge',
          data: element.text().trim()
        });
      });
    cy.get(carsInvoiceValidationSelectors.carTotalBookingChargeCurrency)
      .then((ele) => {
        cy.wrap(ele).scrollIntoView();
      })
      .then((element) => {
        cy.task('putDataInCache', {
          key: 'carTotalBookingChargeCurrency',
          data: element.text().trim()
        });
      });
  }

  static storeTripData() {
    cy.get(carsInvoiceValidationSelectors.carBookingId)
      .eq(0)
      .then((ele) => {
        cy.wrap(ele).scrollIntoView();
      })
      .then((element) => {
        cy.task('putDataInCache', {
          key: 'carBookingId',
          data: element.text().trim()
        });
      });

    cy.get(carsInvoiceValidationSelectors.carName).then((element) => {
      cy.task('putDataInCache', { key: 'carName', data: element.text().trim() });
    });

    cy.get(carsInvoiceValidationSelectors.carTotalBookingCharge)
      .then((ele) => {
        cy.wrap(ele).scrollIntoView();
      })
      .then((element) => {
        cy.task('putDataInCache', {
          key: 'totalPrice',
          data: element.text().trim()
        });
      });
  }

  static storeCarBookingID() {
    cy.get(carsInvoiceValidationSelectors.carBookingId)
      .eq(0)
      .then((ele) => {
        cy.wrap(ele).scrollIntoView();
      })
      .then((element) => {
        cy.task('putDataInCache', {
          key: 'bookingID',
          data: element.text().trim()
        });
      });
  }

  static checkCarsInvoice() {
    cy.allure().logStep(` Check Car name :`);
    cy.task('getDataFromCache', 'carName').then((carName) => {
      Utility.getIframeBody()
        .find(carsInvoiceValidationSelectors.invoiceCarName)
        .scrollIntoView()
        .eq(0)
        .should(($el) => {
          expect($el.text().trim()).to.equal(carName);
        });
    });
  }

  static checkInvoiceTotalPrice(index: number) {
    cy.allure().logStep(` Check total price :`);
    cy.task('getDataFromCache', 'totalPrice').then((totalPrice) => {
      Utility.getIframeBody()
        .find(carsInvoiceValidationSelectors.invoiceTotalPrice)
        .scrollIntoView()
        .eq(index)
        .should(($el) => {
          expect($el.text().trim()).to.equal(totalPrice);
        });
    });
  }

  static checkInvoiceTAFee() {
    cy.allure().logStep(` Check TripActions Fee :`);
    cy.task<string>('getDataFromCache', 'tripFee').then((tripFee) => {
      cy.allure().logStep(` Check TripActions Fee :`);
      cy.contains(tripFee);
    });
  }
  static checkInvoiceBookingId() {
    cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
    cy.allure().logStep(` Check booking Id :`);
    cy.task<string>('getDataFromCache', 'bookingID').then((carBookingId) => {
      cy.contains(carBookingId);
    });
  }

  static checkTripFeeInCheckoutPageSummary(fee: string) {
    cy.get(carsInvoiceValidationSelectors.carCheckoutPageSummary)
      .should('contain', fee)
      .should('be.exist');
  }

  static checkBookingTotalPriceOnItineraryPage() {
    cy.allure().logStep(` Check Booking Total Price :`);
    cy.task('getDataFromCache', 'carTotalBookingCharge').then((carTotalBookingCharge) => {
      cy.get(carsInvoiceValidationSelectors.totalBookingChargeOnItinerary)
        .then((ele) => {
          cy.wrap(ele).scrollIntoView();
        })
        .should(($el) => {
          expect($el.text().trim()).contains(carTotalBookingCharge);
        });
    });
    cy.task('getDataFromCache', 'carTotalBookingChargeCurrency').then(
      (carTotalBookingChargeCurrency) => {
        cy.get(carsInvoiceValidationSelectors.totalBookingChargeOnItinerary)
          .then((ele) => {
            cy.wrap(ele).scrollIntoView();
          })
          .should(($el) => {
            expect($el.text().trim()).contains(carTotalBookingChargeCurrency);
          });
      }
    );
  }
  static checkInvoiceTripeFee() {
    cy.allure().logStep(` Check trip fee invoice localization data :`);
    cy.contains(english.NavanFee);
    cy.contains(english.NavanTripFee);
    cy.contains(english.PaymentMethod);
  }
}
