import { AdminInterceptsApis } from '@admin-lib/index';
import { SplitUtility } from '@split-lib/index';
import { utilitySelector } from '@utility-lib/index';

const guestInvitesPageSelectors = {
  inviteGuestPageButton: '[data-testid="inviteGuestButton"] button',
  eventSearchField: '[data-testid="searchInvite"]',
  searchResultsEventName: '[data-testid="eventRowData01"]',
  searchResultsEventStatus: '[data-testid="eventRowData08"]',
  searchResultsEmail: '[data-testid="eventRowData03"]',
  helpPopup: '[class="contextual-help__content animate-help-panel"] span'
};

export class GuestInvitePage {
  static visitGuestInvitePage() {
    cy.allure().logStep('visit guest invite page');

    cy.updateSplits();
    SplitUtility.isSplitOn('GROUP_TRAVEL_REFACTOR').then((refactored) => {
      if (refactored) {
        cy.intercept('GET', /api\/v1\/user\/guest-travel-events?/).as(
          'waitForInviteeStatus'
        );
        cy.visit('app/admin2/guest-travel/list');
        cy.url().should('include', '/guest-travel/list');
      } else {
        cy.intercept('GET', /api\/admin\/tripInvitations?/).as('waitForInviteeStatus');
        cy.visit('app/admin2/guest-invites');
        cy.url().should('include', '/guest-invites');
      }
      cy.wait('@waitForInviteeStatus');
    });
  }

  static selectGuestInviteOption() {
    cy.allure().logStep('Select invite guest option');
    AdminInterceptsApis.guestInvitePopupModel();
    cy.get(guestInvitesPageSelectors.inviteGuestPageButton)
      .contains('Invite guest')
      .click({ force: true });
    this.waitForGuestInvitePopupModel();
    cy.get(utilitySelector.bodySelector).then((newTask) => {
      if (newTask.find(guestInvitesPageSelectors.helpPopup).length > 0) {
        cy.allure().logStep('close help popup');
        cy.get(guestInvitesPageSelectors.helpPopup).eq(0).click();
      }
    });
  }
  static waitForGuestInvitePopupModel() {
    cy.wait('@waitForPopUpModel');
    cy.wait('@waitForGuestInviteModal');
  }

  static searchEventByEventName(eventName: string) {
    cy.allure().logStep(`Searching for previously created event : ${eventName}`);

    cy.updateSplits();
    SplitUtility.isSplitOn('GROUP_TRAVEL_REFACTOR').then((refactored) => {
      if (refactored) {
        cy.intercept('GET', /api\/v1\/user\/guest-travel-events?/).as(
          'teamEventInvitation'
        );
      } else {
        cy.intercept('GET', /api\/admin\/tripInvitations?/).as('teamEventInvitation');
      }
    });

    cy.get(guestInvitesPageSelectors.eventSearchField).type(eventName);
    cy.wait('@teamEventInvitation');
  }

  static getDataFromCachedUserEmailAndVerifyEventData(eventName: string) {
    cy.task('getDataFromCache', 'userInfo').then((userInfo: any) => {
      this.verifyCreatedEventData(eventName, userInfo.email);
    });
  }

  static verifyCreatedEventData(eventName: string, inviteeEmail: string) {
    cy.get(guestInvitesPageSelectors.searchResultsEventName).contains(eventName);
    cy.get(guestInvitesPageSelectors.searchResultsEventStatus).contains('Invited');
    cy.get(guestInvitesPageSelectors.searchResultsEmail).contains(inviteeEmail);
  }
}
