import { expect, Page } from '@playwright/test';
import BasePage from '../Base';
import { ProjectsPage } from '../ProjectsPage';
import { CloudSSOLoginPage } from './SSOLoginPage';
import { WorkspacePage } from '../WorkspacePage';

export class CloudSAMLLoginPage extends BasePage {
  readonly projectsPage: ProjectsPage;
  readonly ssoLoginPage: CloudSSOLoginPage;

  constructor(rootPage: Page) {
    super(rootPage);
    this.projectsPage = new ProjectsPage(rootPage);
    this.ssoLoginPage = new CloudSSOLoginPage(rootPage);
  }

  async goto(_title = 'test', email: string) {
    // logout if already logged in

    await this.ssoLoginPage.goto(email);
    await this.ssoLoginPage.signIn({ email, waitForUserInfoMenu: false });
    // // reload page to get latest app info
    // await this.rootPage.reload({ waitUntil: 'networkidle' });
    // // click sign in with SAML
    // await this.rootPage.locator(`button:has-text("Sign in with ${title}")`).click();
  }

  get() {
    return this.rootPage.locator('html');
  }

  async signIn({ email }: { email: string }) {
    const signIn = this.get();
    await signIn.locator('#userName').waitFor({ state: 'visible' });

    await signIn.locator(`#userName`).fill(email);
    await signIn.locator(`#email`).fill(email);

    await Promise.all([
      this.rootPage.waitForNavigation({ url: /localhost:3000/, waitUntil: 'networkidle' }),
      signIn.locator(`#btn-sign-in`).click(),
    ]);

    const userInfoMenu = this.rootPage.locator(`[data-testid="nc-sidebar-userinfo"]`);
    await userInfoMenu.waitFor();

    await expect(userInfoMenu).toHaveAttribute('data-email', email);
  }
}
