import { helpCenterSelectors } from './selectors/support-entries';
import { HelpCenterApiIntercept } from './api-intercept';
import { AmplitudeEvents } from '@growth-lib/amplitude-events';
import { Timeouts } from '@utility-lib/constants';

export class HelpCenter {
  static visitPage() {
    cy.allure().logStep('Visit helpCenter page');
    HelpCenterApiIntercept.interceptHelpCenterApis();
    AmplitudeEvents.interceptAmplitudeAPI();
    cy.visit('app/helpcenter/home/travel/myself');
    cy.wait(['@getHelpCenterContent', '@amplitudeBrowserSDK']);
    cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
  }

  static visitAndLogin(email: string, password: string) {
    HelpCenterApiIntercept.interceptHelpCenterApis();
    cy.visit('app/helpcenter/home/travel/myself');
    cy.wait(['@getHelpCenterContent']);
    cy.setAuthToken({ email, password, localStorage: true });
    cy.reload();
  }

  static verifyMainPageContentIsVisible() {
    cy.allure().logStep('Check helpCenter content');
    cy.get(helpCenterSelectors.topCategories).should('be.visible');
    cy.get(helpCenterSelectors.topCategoriesCards)
      .should('be.visible')
      .find(helpCenterSelectors.topCategoriesCard)
      .should('have.length', 5);
    cy.get(helpCenterSelectors.topCategoriesCards).find('a').should('have.length', 6);

    cy.get(helpCenterSelectors.topArticles)
      .should('be.visible')
      .find(helpCenterSelectors.article)
      .should('have.length', 6);
  }

  static verifyFooterAndHeaderVisibility(loggedIn: boolean) {
    cy.allure().logStep('Check helpCenter header and footer');
    cy.get(helpCenterSelectors.header).should('exist');
    if (loggedIn) {
      cy.get(helpCenterSelectors.loginButton).should('not.exist');
      cy.get(helpCenterSelectors.footer).should('exist');
    } else {
      cy.get(helpCenterSelectors.loginButton).should('exist');
      cy.get(helpCenterSelectors.footer).should('not.exist');
    }
  }

  static verifyContentCouldBeReached() {
    cy.allure().logStep('Check helpCenter content accessibility');
    let categoryName: string;
    cy.get(helpCenterSelectors.topCategoriesCard).then(($el) => {
      categoryName = $el[0].innerText;
      cy.get(helpCenterSelectors.topCategoriesCard).eq(0).click();
      cy.url().should('include', '/app/helpcenter/categories/');
      cy.get(helpCenterSelectors.categoryPage).should('contain.text', categoryName);
    });

    cy.get(helpCenterSelectors.categoryPageArticle).should('be.visible');
    cy.get(helpCenterSelectors.breadcrumbs).should('be.visible');

    cy.get(helpCenterSelectors.categoryPageArticle).eq(0).click();

    cy.get(helpCenterSelectors.articleTitle).should('be.visible');
    cy.get(helpCenterSelectors.articleContent).should('be.visible');
  }

  static initiateChat() {
    cy.allure().logStep('Initiate the support chat');
    cy.get(helpCenterSelectors.chatEntry).should('be.visible');
    cy.get(helpCenterSelectors.chatBubble).should('be.visible');

    cy.get(helpCenterSelectors.chatEntry).click();

    cy.get(helpCenterSelectors.chat).should('be.visible');
    cy.get(helpCenterSelectors.chatMessage).should('be.visible');

    cy.get(helpCenterSelectors.minimizeChat).click();
  }

  static checkPhoneNumbers() {
    cy.allure().logStep('Check support phone numbers');
    cy.get(helpCenterSelectors.callEntry).should('not.be.disabled').should('be.visible');
    cy.get(helpCenterSelectors.callEntry).click();
    cy.get(helpCenterSelectors.phoneNumbersModal)
      .should('be.visible')
      .should('contain.text', 'Our phone numbers');
    cy.get(helpCenterSelectors.phoneNumberWrapper)
      .should('be.visible')
      .should('have.length', 6);
  }
}
