import { Page } from "@playwright/test";
import { BasePage } from "./base.page";
import { DashboardPage } from "./dashboard.page";
import { ForgotPage } from "./authentication/forgot.page";

export class LoginPage extends BasePage {

    constructor(page: Page) {
        super(page)
    }

    async open() {
        await this.page.goto('sign-in', { waitUntil: 'domcontentloaded' });
    }

    get loginLoc() {
        return {
            // form
            mainContainer: this.genLoc("//div[./form]"),
            inputUsername: this.genLoc("//input[@name='email']"),
            inputPassword: this.genLoc("//input[@name='password']"),
            cbRemember: this.genLoc("//input[@name='remember']"),
            btnLogin: this.genLoc("//button[@type='submit' and normalize-space()='Log in']"),
            btnNext: this.genLoc("//button[text()='Next']"),
            forgotPasswordLink: this.genLoc("//a[contains(text(), 'Forgot Password')]"),
            loginWithPasscodeLink: this.genLoc("//a[contains(text(), 'Login with passcode')]"),
            eyeIcon: (status: string) => this.genLoc(`//a[@aria-label='${status} password']`),
            emailError: this.genLoc("//span[contains(text(), 'Enter an email.')]"),
            emailFormatError: this.genLoc("//span[contains(text(), 'Please enter a valid email address.')]"),
            passwordRequiredError: this.genLoc("//span[contains(text(), 'Enter a password.')]"),
            invalidCredentialError: this.genLoc("//span[contains(text(), 'These credentials do not match our records.')]"),
            infoButton: this.genLoc("//span[@role='button']"),
            trustedBrowserDialog: this.genLoc("//div[@class='modal-content']"),
            trustedBrowserTitle: this.genLoc("//h5[@class='modal-title']"),
            gotItButton: this.genLoc("//button[@type='button' and @class='btn-close']"),

            // tos things
            tosLink: this.genLoc("//a[normalize-space()='Terms of Use']"),
            privacyPolicyLink: this.genLoc("//a[normalize-space()='Privacy Policy']"),
            baaLink: this.genLoc("//a[normalize-space()='Business Associate Agreement']"),

            // 2FA
            twoFaLoginHeading: this.genLoc("//h2[normalize-space()='Two-factor authentication']"),
            digitInput: (digitNum: number) => this.genLoc(`//input[@id="digit-${digitNum}"]`),
            confirmButton: this.genLoc(`//button[@type='submit' and normalize-space()='Confirm']`),

            // Passcode
            headingLoginWithPasscode: this.genLoc("//h3[normalize-space()='Sign In with OTP']"),
            errorEnterEmail: this.genLoc("//span[contains(text(), 'Enter an email.')]"),
            errorEnterEmailFormat: this.genLoc("//span[contains(text(), 'Please enter a valid email address.')]"),
            errorEmailNotFound: this.genLoc(`//span[contains(text(), "We're searching our records to find the account for")]`),
            messageEmailFound: this.genLoc(`//span[contains(text(), "Passcode for one-time access was sent to")]`),
            resendCode: this.genLoc(`//a[contains(text(), "Resend code, didn't get it or has expired")]`),
            errorTryAgainLater: this.genLoc("//span[contains(text(), 'Your request is processing')]"),
            inputToken: this.genLoc("//input[@name='token']"),
            errorInvalidPasscode: this.genLoc("//span[contains(text(), 'Passcode is invalid or has expired.')]"),
        }
    }

    async fillLoginInfo(username: string, password: string) {
        await this.loginLoc.inputUsername.fill(username);
        await this.loginLoc.inputPassword.fill(password);
    }

    async login(username: string, password: string, remember: boolean = false) {
        await this.fillLoginInfo(username, password);
        if (remember) {
            await this.loginLoc.cbRemember.check();
        }

        await this.loginLoc.btnLogin.click();
        return new DashboardPage(this.page);
    }

    async clickForgotPassword() {
        await this.loginLoc.forgotPasswordLink.click();
        return new ForgotPage(this.page);
    }
}