import { browser, $, ElementFinder } from 'protractor';

import SignInPage from './signin-page';
<%_ if (!authenticationTypeOauth2) { _%>
import RegisterPage from './register-page';
import PasswordPage from './password-page';
import SettingsPage from './settings-page';
<%_ } _%>
import {
  ConfigurationsPage,
<%_ if (applicationTypeGateway && serviceDiscoveryType) { _%>
  GatewayPage,
<%_ } _%>
  HealthPage,
  LogsPage,
  MetricsPage
<%_ if (!authenticationTypeOauth2) { _%>,
  UserManagementPage
<%_ } _%>
} from './administration-page';
import {click, isVisible, waitUntilDisplayed, waitUntilHidden} from '../util/utils';

export default class NavBarPage {
  selector: ElementFinder = $('#app-header');
  adminMenu: ElementFinder = this.selector.$('#admin-menu');
  accountMenu: ElementFinder = this.selector.$('#account-menu');
  entityMenu: ElementFinder = this.selector.$('#entity-menu');

  async clickOnTabMenu(item: string) {
    await click(this.selector.$(`.dropdown-menu ${item}`));
  }

  async clickOnAccountMenu() {
    await click(this.accountMenu);
  }

  async clickOnAccountMenuItem(item: string) {
    await this.clickOnAccountMenu();
    await click(this.selector.$(`.dropdown-item[href="/account/${item}"]`));
  }

<%_ if (!skipUserManagement) { _%>
  async getPasswordPage() {
    await this.clickOnAccountMenuItem('password');

    const passwordPage = new PasswordPage();
    await waitUntilDisplayed(passwordPage.title);
    return passwordPage;
  }

  async getSettingsPage() {
    await this.clickOnAccountMenuItem('settings');

    const settingsPage = new SettingsPage();
    await waitUntilDisplayed(settingsPage.title);
    return settingsPage;
  }

async getRegisterPage() {
    await this.clickOnAccountMenu();
    await this.clickOnTabMenu('#register');
    const registerPage = new RegisterPage();
    await waitUntilDisplayed(registerPage.title);
    return registerPage;
  }

<%_ } _%>
  async getSignInPage() {
    await this.clickOnAccountMenu();
    await this.clickOnTabMenu('#login');
    const signInPage = new SignInPage();
<%_ if (!authenticationTypeOauth2) { _%>
    await waitUntilDisplayed(signInPage.title);
<%_ } _%>
    return signInPage;
  }

  async clickOnAdminMenuItem(item: string) {
    await click(this.adminMenu);
    await click(this.selector.$(`.dropdown-item[href="/admin/${item}"]`));
  }

  async clickOnEntityMenu() {
    await click(this.entityMenu);
  }

  async clickOnEntity(entityName: string) {
    await click(this.selector.$(`.dropdown-item[href="/${entityName}"]`));
  }

  async getEntityPage(entityName: string) {
    await this.clickOnEntityMenu();
    await this.clickOnEntity(entityName);
  }

  async login(username: string, password: string) {

    await this.clickOnAccountMenu();
    const alreadyLoggedIn = await isVisible(this.selector.$(`.dropdown-menu #logout`));

    // close account menu
    await this.clickOnAccountMenu();
    if(!alreadyLoggedIn) {
      const signInPage = await this.getSignInPage();
      await signInPage.login(username, password);
<%_ if (!authenticationTypeOauth2) { _%>
      await waitUntilHidden(signInPage.loginForm);
<%_ }_%>
      await waitUntilDisplayed(this.entityMenu);
    }
  }

  async autoSignOut() {

    // avoid interference due to dialogs
    await browser.get('/');
    await this.clickOnAccountMenu();
    await this.clickOnTabMenu('#logout');
    await waitUntilHidden(this.entityMenu);
  }

<%_ if (!authenticationTypeOauth2) { _%>
  async getUserManagementPage() {
    await this.clickOnAdminMenuItem('user-management');
    const userManagementPage = new UserManagementPage();
    await waitUntilDisplayed(userManagementPage.title);
    return userManagementPage;
  }

<%_ } _%>
  async getMetricsPage() {
    await this.clickOnAdminMenuItem('metrics');
    const metricsPage = new MetricsPage();
    await waitUntilDisplayed(metricsPage.title);
    return metricsPage;
  }

  async getHealthPage() {
    await this.clickOnAdminMenuItem('health');
    const healthPage = new HealthPage();
    await waitUntilDisplayed(healthPage.title);
    return healthPage;
  }

  async getConfigurationsPage() {
    await this.clickOnAdminMenuItem('configuration');
    const configurationsPage = new ConfigurationsPage();
    await waitUntilDisplayed(configurationsPage.title);
    return configurationsPage;
  }

  async getLogsPage() {
    await this.clickOnAdminMenuItem('logs');
    const logsPage = new LogsPage();
    await waitUntilDisplayed(logsPage.title);
    return logsPage;
  }
<%_ if (applicationTypeGateway && serviceDiscoveryType) { _%>

  async getGatewayPage() {
    await this.clickOnAdminMenuItem('gateway');
    const gatewayPage = new GatewayPage();
    await waitUntilDisplayed(gatewayPage.title);
    return gatewayPage;
  }
<%_ } _%>
}
