
import AppBasePage from "../pages/v10/app-base.js"
import ElementsActions from "./element-actions.js"
const allowedLanguages = ['EN', 'DE', 'FR', 'NL', 'IT', 'CL'];

export default class BaseActions {
  static async checkAvatar() {
    let page = new AppBasePage();
    let avatar = await $(page.userProfileAvatar);
    await this.checkElementExistsAndIsDisplayed(avatar, 'Home Toolbar', 'User Profile Avatar', 40000);
  }

  static async openAvatarMenu() {
    let page = new AppBasePage();
    let userAvatar = await $(page.userProfileAvatar);
    await ElementsActions.checkElementExistsAndIsDisplayed(userAvatar, 'Main toolbar', 'user avatar', 10000);
    await ElementsActions.clickElement(userAvatar, 'Main toolbar', 'user avatar')
    let profileMenu = page.userProfileMenuCO;
    await expect(await profileMenu.element()).toBeDisplayed({ wait: 1000, message: 'Avatar dropdown menu is not displayed.' });
    return profileMenu;
  }
  static async checkUrlContains(expectedUrl) {
    await browser.deductedWait(2000);
    await expect(browser).toHaveUrlContaining(expectedUrl, { wait: 5000, message: 'URL is not as the expected.' });
  }

  static async verifyLoggedInUser(expectedLoggedInUser) {
    let page = new AppBasePage();
    let loggedInUser = await $(page.userProfileWelcomeName);
    await ElementsActions.checkElementExistsAndIsDisplayed(loggedInUser, 'Header Toolbar', 'User Name', 30000);
    await expect(loggedInUser).toHaveTextContaining(expectedLoggedInUser, { wait: 2000, message: 'Logged in user is not the expected one' });
  }

  static async getLanguage() {
    let appBasePage = new AppBasePage();
    let languageElement = await $(await appBasePage.languageSelectedCode());
    let language = await (await languageElement.getText()).toLowerCase();
    return language;
  }
  static async changeLang(langIndicator) {
    let langIndicatorUpperCase = langIndicator.toUpperCase();
    if (!allowedLanguages.includes(langIndicatorUpperCase)) {
      throw new Error('Language: "' + langIndicatorUpperCase + '" isn\'t defined in languages list');
    }
    let appBasePage = new AppBasePage();
    let actualLanguage = (await this.getLanguage()).toUpperCase();
    if (actualLanguage != langIndicatorUpperCase) {
      let langSelector = await $(await appBasePage.languageSelectorLink());
      await ElementsActions.checkElementExistsAndIsDisplayed(langSelector, 'Header Toolbar', 'Language Selector Link', 10000);
      await ElementsActions.clickElement(langSelector, 'Header Toolbar', 'Language Selector Link');
      let langSelectorModal = await $(appBasePage.modalSelector);
      await ElementsActions.checkElementExistsAndIsDisplayed(langSelectorModal, 'Header Toolbar', 'Language Menu');
      let selectLanguageButton = await appBasePage.selectLanguageButtonCO.byTextContains('(' + langIndicatorUpperCase.toLowerCase() + ')').element();
      await selectLanguageButton.click();
      await expect(langSelectorModal).not.toBeExisting({ wait: 5000, message: 'Language selector dropdown menu should not be existing after selecting the language' });
      await ElementsActions.checkElementExistsAndIsDisplayed(langSelector, 'Header Toolbar', 'Language Selector Link', 25000);
      await expect(await $(await appBasePage.languageSelectedCode())).toHaveText(langIndicatorUpperCase, { wait: 3000, message: 'Language code is not as expected' });
    }
  }
}
