import { FlightTripType, Flights, flightSearchSelector } from '@flight-lib/search';
import { FlightSearchResults } from '@flight-lib/index';
import { Utility, Timeouts } from '@utility-lib/index';
import { TrainSearchResults, TofStations } from '@train-lib/index';

const tofFlowSelectors = {
  searchForTrainButton: '[class="ta-button-v2 large ta-button-v2-default"]',

  searchButton: '[class^="ta-button-v2"]',
  currency: '.ta-currency',
  preferToFly: '.ta-trains-over-flight__content',
  annoucment: '.ta-announcement__image',
  popup: '[class="cdk-overlay-pane modal-panel"]',
  Currency: '[class="ta-currency-trigger"]'
};

export class TofFlow {
  static tofSearch(location1: string, location2: string) {
    Flights.visitFlightSearchPage();
    Flights.selectFlightType(FlightTripType.ONEWAY);
    Utility.selectFutureDate();
    this.searchForTOF(location1, location2);
  }

  static searchForTOF(location1: string, location2: string) {
    Flights.setOrigin(location1);
    Flights.setDestination(location2);
    this.searchTOF();
  }

  static searchTOF(eventSearch = false) {
    cy.allure().logStep('search flight alternative');
    const selector = eventSearch ? flightSearchSelector.flightEventSearch : flightSearchSelector.flightSearchButton;
    cy.get(selector, { timeout: Timeouts.MEDIUM_TIMEOUT_60_SEC.timeout }).click({ force: true });
    cy.wait('@tofSearch');
  }

  static continueWithFlights() {
    cy.allure().logStep('click continue with flights button');
    cy.get(tofFlowSelectors.searchButton).contains('Continue with flights').click();
    cy.wait('@waitForFlightSearch');
    cy.wait('@waitForTravellers');
    FlightSearchResults.verifyNumberOfBookings();
    cy.allure().logStep('flight search page loaded successfully');
  }

  static tofTrainSearchResultPage() {
    cy.get(tofFlowSelectors.searchButton).contains('Search for a train').click();
    cy.allure().logStep('search results page loaded successfully ');

    cy.wait('@waitForSearchOrCheckout').then((interception) => {
      const response = interception.response;
      const faresCount = response?.body.searchResults.stats.allClassesPriceSummary?.faresCount;
      if (faresCount !== null && faresCount !== 0) {
        cy.get(tofFlowSelectors.currency, Timeouts.MEDIUM_TIMEOUT_60_SEC).should('be.visible');
        cy.get(tofFlowSelectors.preferToFly).should('contain.text', 'I prefer to fly');
        cy.allure().logStep('train search page loaded');
        TrainSearchResults.expectSearchResultPresent();
      } else {
        cy.get(tofFlowSelectors.currency, Timeouts.MEDIUM_TIMEOUT_60_SEC).should('be.visible');
        cy.log(`No train search result found from ${TofStations.TofOrigin} to ${TofStations.TofDestination}`);
        cy.allure().logStep('Flights search page loaded');
        cy.contains('Maybe next time');
        //cy.get(tofFlowSelectors.preferToFly).should('contain.text', 'Maybe next time');
        // Search for a flight from New york  (AMS) to Boston (BER)
        this.searchForTOF(TofStations.TofOriginSabre, TofStations.TofDestinationSabre);
        cy.get(tofFlowSelectors.searchButton).contains('Search for a train').click();
        cy.wait('@waitForSearchOrCheckout').then((interception) => {
          const res = interception.response;
          const currentLegSelected = res?.body.searchResults.currentSelection.legId;
          if (currentLegSelected !== null && currentLegSelected !== 0) {
            cy.get(tofFlowSelectors.popup, Timeouts.MAX_TIMEOUT_120_SEC).should('not.exist');
            cy.get(tofFlowSelectors.currency, Timeouts.MEDIUM_TIMEOUT_60_SEC).should('be.visible');
            TrainSearchResults.expectSearchResultPresent();
          } else {
            cy.allure().logStep(
              `no train results for both Trainline route from ${TofStations.TofOrigin} to ${TofStations.TofDestination} and Sabre route from ${TofStations.TofOriginSabre} to ${TofStations.TofDestinationSabre}`
            );
            cy.log('no train results for both Trainline and Sabre');
          }
        });
      }
    });
  }
}
