import { FlightUtility } from '@flight-modal/index';
import { User_Type } from '@support-onboard/data_model/user_type';
import { HelpCenter } from '@helpcenter-lib/helpcenter';
import { ChatBox, ChatApiIntercept, ChatBooking, TravelType } from '@chat-lib/index';
import { Directions, FlightBookingData } from './booking';
import { LoginPage } from '@admin-lib/login';
import { Timeouts } from '@utility-lib/constants';
import { DaysToMove } from '@utility-lib/utility';
const credentials = Cypress.env('newUser').NEW_USER_PASSWORD;

export class BookingUtility {
  static bookFlight(
    flightBookingData: FlightBookingData,
    daysToMove: DaysToMove,
    numberOfFlights = 2
  ) {
    ChatApiIntercept.interceptChatApis();
    FlightUtility.prepareCompanyAndUserCreation(User_Type.ADMIN);
    HelpCenter.visitPage();
    ChatBox.openChatWithAva();
    ChatBox.selectBookTravelFlow(TravelType.FLIGHT);
    ChatBooking.bookTripFlight(
      {
        flightBookingParams: flightBookingData.flightBookingParams,
        departingFrom: Directions.FROM,
        arrivingAt: Directions.TO,
        isChangeFlight: false
      },
      daysToMove,
      numberOfFlights
    );
    ChatBooking.checkIsChatMinimized();
  }

  static createFlightBookingAndClearSession(
    flightBookingData: FlightBookingData,
    daysToMove: DaysToMove,
    numberOfFlights = 2
  ) {
    BookingUtility.bookFlight(flightBookingData, daysToMove, numberOfFlights);
    cy.reload(true);
    cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
    cy.clearCookies();
    cy.clearAllLocalStorage();
    cy.clearAllSessionStorage();
    Cypress.session.clearAllSavedSessions();
    cy.task('getDataFromCache', 'userEmail').then((userEmail: any) => {
      LoginPage.visitTALoginPage();
      LoginPage.loginWithCredentials(userEmail, credentials);
    });
    cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
    HelpCenter.visitPage();
    cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
  }
}
