import { Timeouts } from '@utility-lib/index';

const selectors = {
  languageDropdown: '.ta-select__chevron',
  languageUpdate: 'button.ta-profile-language__button[type="submit"]',
  languageSelect: 'div.ta-select-option',
  languagePopover: 'div.popover-content',
  languageDisplay: 'button.ta-select',
  passwordCurrent: 'input[formcontrolname="currentPassword"]',
  passwordNew: 'input[formcontrolname="newPassword"]',
  passwordNewConfirm: 'input[formcontrolname="confirmNewPassword"]',
  passwordUpdate: '.ta-password__button',
  successMessage: '.success'
};

export class OtherSettings {
  static currentLocale: string;
  static displayedLocale: string;

  static visitPage(): void {
    cy.allure().logStep('Visit profile page on otherSettings tab');
    cy.visit('app/user2/profile?tab=settings');
  }

  static checkLanguage(): void {
    // Gets the user's current locale settings
    cy.reload();
    cy.wait('@waitForUserInfo').then((interception) => {
      const response = interception.response;
      if (response && response.body.locale !== 'en-US') {
        // New users do not have a locale set, so this assigns 'en-US' as the locale
        cy.log('Locale not yet defined for user, setting locale as en-US');
        cy.get(selectors.languageDropdown).click();
        cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
        cy.get(selectors.languageSelect).then(($divs) => {
          cy.wrap($divs[0]).click();
        });
        cy.get(selectors.languageUpdate).click();
        cy.reload();
        // Refreshes the page to get a new API request and waits for the page to load
        this.checkLanguage();
      } else {
        // If the locale is set to 'en-US' then no further actions is required
        OtherSettings.currentLocale = response?.body.locale;
        cy.get(selectors.languageDisplay)
          .invoke('text')
          .then((text) => {
            OtherSettings.displayedLocale = text.trim();
            cy.log('Displayed locale is ' + OtherSettings.displayedLocale);
          });
      }
    });
  }

  static changeLanguage(): void {
    // Changes the language to go through languages 2 through 5, and checks that the changes are saved each time
    for (let languageIndex = 1; languageIndex <= 4; languageIndex++) {
      cy.get(selectors.languageDropdown).click();
      cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
      cy.get(selectors.languageSelect).then(($divs) => {
        cy.wrap($divs[languageIndex]).click();
        cy.get(selectors.languageUpdate).click();

        // Gets the user's updated locale settings
        cy.reload();
        // Refreshes the page to get a new API request and waits for the page to load
        cy.wait('@waitForUserInfo').then((interception) => {
          // Verifies that the locale is no longer the last locale
          const response = interception.response;
          if (response && response.body.locale == OtherSettings.currentLocale) {
            let count = 0;
            do {
              // Refreshes the page to get a new API request and waits for the page to load
              cy.reload();
              // Increment the count
              count++;
              // Check the condition
            } while (count < 2 && response.body.locale != OtherSettings.currentLocale);
          }
          OtherSettings.currentLocale = response?.body.locale;
          cy.log('The locale is ' + OtherSettings.currentLocale);
        });
      });
      cy.get(selectors.languageDisplay).should('be.visible');
      cy.get(selectors.languageDisplay)
        .invoke('text')
        .then((text) => {
          const trimmedText = text.trim();
          cy.wrap(OtherSettings.displayedLocale).should('not.include', trimmedText);
        });
      cy.get(selectors.languageDisplay)
        .invoke('text')
        .then((text) => {
          OtherSettings.displayedLocale = text.trim();
        });
    }
  }

  static changePassword(email: string, oldPassword: string, newPassword: string): void {
    cy.allure().logStep('Change user password through profile page on otherSettings tab');
    cy.get(selectors.passwordCurrent).type(oldPassword, { log: false });
    cy.get(selectors.passwordNew).type(newPassword), { log: false };
    cy.get(selectors.passwordNewConfirm).type(newPassword), { log: false };
    cy.get(selectors.passwordUpdate).click();
    cy.get(selectors.successMessage).should('be.visible');
  }
}
