import moment = require('moment');
import { TripTileType, Utility } from '@utility-lib/index';

const blackCarItinerarySelector = {
  tripActionsBookingInfoSection: '.trip-black-car__booking-info',
  timelineItem: '.trip-timeline__item',
  timelineBookingEvent: '.trip-timeline__booking-event',
  tripType:
    '[qaId="tripsTypeTagPesonal"], [qaId="tripsTypeTagBusiness"], [qaId="tripsTypeTagBleisure"]',
  bookingInfoRow: '.trip-black-car__booking-info > .trip-black-car__booking-info__row',
  userTag: '.ta-user-tag__name'
};

export class BlackCarItinerary {
  static verifyBlackCarItineraryPage(): void {
    this.waitToBeLoaded();
    cy.get(blackCarItinerarySelector.tripActionsBookingInfoSection).then((ele) => {
      cy.wrap(ele).scrollIntoView();
      cy.wrap(ele).should('be.visible');
    });
  }

  static waitToBeLoaded() {
    cy.allure().logStep('wait for black car itinerary page to be successfully loaded');
    cy.url().should('include', `/app/user2/trips/`);
    cy.wait('@waitForTripTimeline');
  }

  static verifyTripTimelineItem(pickUpDateCached: string) {
    cy.allure().logStep('verify black car item added on trip timeline');
    const pickUpDate = moment(pickUpDateCached).format('dddd, MMMM D, YYYY');
    cy.get(blackCarItinerarySelector.timelineBookingEvent).should(
      'have.length.greaterThan',
      1
    );
    cy.get(blackCarItinerarySelector.timelineItem)
      .should('contain', pickUpDate)
      .and('contain', 'Black Car');
  }

  static verifyTripTile(tripTile: TripTileType) {
    cy.allure().logStep(`verify tripType should be ${tripTile}`);
    const tripTypeSelector = blackCarItinerarySelector.tripType;
    cy.get(tripTypeSelector).contains(tripTile);
  }

  static cancelBlackCar() {
    Utility.cancelTrip('BlackCar');
    Utility.confirmFlightCancellation();
    Utility.expectCancelledLabelIsPresent();
  }

  static verifyBookForDelegatedUser() {
    cy.task('getDataFromCache', 'delegatedUserName').then((fullName: any) => {
      const tagNameSelector = blackCarItinerarySelector.userTag;

      cy.get(tagNameSelector).should('contain', fullName);
    });
  }

  static getDataFromCachedEstimatedTotalAndVerify() {
    cy.allure().logStep('get estimated total from cache');
    cy.task('getDataFromCache', 'estTotal').then((estTotalCached: any) => {
      cy.log('cached estimated total: ' + estTotalCached);
      expect(estTotalCached, 'cached estimated total should not be null').to.not.be.null;
      this.verifyEstimatedTotal(estTotalCached);
    });
  }

  static verifyEstimatedTotal(estTotalCached: string) {
    cy.allure().logStep(
      'verify estimated total should be the same as on the checkout page'
    );
    cy.get(blackCarItinerarySelector.bookingInfoRow).should('contain', estTotalCached);
  }
}
