import AppBasePage from '../pages/v10/app-base.js';
import WaitActions from './waiters.js';
import * as ecasActions from '@eui/automation/actions/ecas.js';
import EcasLoginPage from '@eui/automation/pages/ecas/ecas-login.js';
import BaseActions from './base-actions.js';
import Routing from '../pages/routing.js';
import ElementsActions from './element-actions.js';

export default class AppUserActions {
  static async appLogIn(user, url=null) {
    let appBasePage = new AppBasePage();
    let ecasLoginPage = new EcasLoginPage();
    if (browser.config.loggedInUser){
      await WaitActions.waitForPageLoaders(40000);
      let browserUrl = await browser.getUrl();
      if (!browserUrl.startsWith(browser.config.baseUrl)) {
        if (url) {
          await appBasePage.open(url);
        } else {
          await appBasePage.open(browser.config.baseUrl);
        }
        await WaitActions.waitForAllLoaders(40000);
      }
      if (await (await $(appBasePage.userProfileWelcomeName)).isExisting()) {
        if (browser.config.loggedInUser !== user) {
          await this.appLogOut();
          await ecasActions.ecasLogIn(user, url);
        }
      } else if ((await (await $(ecasLoginPage.mainHeaderBrandingTitle)).isExisting()) && (await (await $(ecasLoginPage.userInput)).isExisting())) {
        await ecasActions.ecasLogIn(user, url, true);
      } else if(await (await $(ecasLoginPage.passwordInput)).isExisting()) {
        await ecasActions.ecasLogIn(user, url);
      }
    } else {
      await ecasActions.ecasLogIn(user, url);
    }
    await BaseActions.verifyLoggedInUser(user);
  }

  /**
  * Logs out of MWP and Ecas
  * CAREFULL, also stops watcher so watcher needs to be restarted after
  */
  static async appLogOut() {
    let userPage = new AppBasePage();
    let navigateUrl = Routing.homeUrl;
    let userAvatar = await $(userPage.userProfileAvatar);
    if (!await userAvatar.isExisting()){
      await userPage.open(navigateUrl);
      await expect(browser).toHaveUrlContaining(navigateUrl, { wait: 10000, message: 'Could not navigate to page ' + navigateUrl + ' in 10000ms.' })
      await expect(userAvatar).toBeExisting({wait: 40000, message:'User avatar is not existing on the page.'});
    }
    await BaseActions.openAvatarMenu();

    let signOutBtn = await userPage.logoutIconCO.element();
    await ElementsActions.checkElementExistsAndIsDisplayed(signOutBtn, 'Header Toolbar', 'Sign Out button');
    await ElementsActions.clickElement(signOutBtn, 'Header Toolbar', 'Sign Out button');
    await expect(signOutBtn).not.toBeDisplayed({wait: 5000, message:'SignOut button is still displayed on the page.'});
    await expect(userAvatar).not.toBeDisplayed({wait: 5000, message:'User avatar is still displayed on the page.'});
    await expect(browser).not.toHaveUrlContaining(navigateUrl, { wait: 10000, message: 'System is still on dashboard: ' + navigateUrl + ' instead of been in ecas logout' });

    await browser.stopWatching();
    await ecasActions.ecasLogOut();
  }

  static async clearSession() {
   await ecasActions.clearLocalStorage();
   browser.config.loggedInUser = null;
  }
}
