import { Page } from "@playwright/test";
export class ProductPage {
    page: Page;

    constructor(page: Page) {
        this.page = page;
    }

    xpathFirstProductInCollection = "//span[contains(@class, 'product-card__title')]//a";
    xpathPopUpSale = "//div[@id='row_JVD6sA4BbE']";
    xpathBtnBuyWithShopPay = "//shop-pay-wallet-button";
    xpathBtnBuyWithPayPal = "//div[contains(@class,'paypal-button-container')]";

    defaultPaypalAccount = {
        username: "for.test.perifit@gmail.com",
        password: "Testforperifit2024"
    };

    defaultShopPay = {
        account: "for.test.perifit@gmail.com",
        password: "Testforperifit2024"
    };

    // PayPal
    xpathPayPalExpress = "//div[@aria-label='PayPal' and @id='buttons-container']";
    xpathHeaderTextPayPal = "//h1[@id='headerText']";
    xpathUsernamePayPal = "//input[@id='email']";
    xpathPasswordPayPal = "//input[@id='password']";
    xpathBtnLogIn = "//button[@id='btnLogin']";
    xpathChooseAWayToPay = "//div[text()='Choose a way to pay']";
    xpathAddACreditOrPrepaidCard = "//div[text()='Add a credit or prepaid card']";
    xpathCardNumberPayPal = "//input[@id='cardNumber']";
    xpathExpirationDatePayPal = "//input[@id='cardExpiry']";
    xpathCardCVVPayPal = "//input[@id='cardCvv']";
    xpathLogoPayPal = "(//p[text()='PayPal'])[1]";

    // ShopPay
    xpathIframeShopPayPayment = "//iframe[@id='shop-pay-login-iframe']";
    xpathBtnChangeShopPayPayment = "//button[text()='Change']";

    // Click add to cart
    async clickBtnAddToCart() {
        await (this.page.locator("//button[contains(@class,'careplus-ld__hero-buy-button') or contains(@class,'button button--xl button--secondary')]").first()).waitFor({ state: 'visible' });
        await this.page.waitForTimeout(3_000);
        await this.page.locator("//button[contains(@class,'careplus-ld__hero-buy-button') or contains(@class,'button button--xl button--secondary')]").first().click({ force: true });
        await this.page.waitForTimeout(5_000);
    }

    async closePopUpSale() {
        const isDisplayed = await this.page.locator(this.xpathPopUpSale).isVisible({ timeout: 15_000 });
        if (isDisplayed) {
            await this.page.click("//div[@id='pge_hpy1uwpNS']//span[contains(@class, 'om-popup-close-x')]");
        }
    }

    async buyWithShopPay() {
        // await this.page.waitForSelector(this.xpathBtnBuyWithShopPay);
        // await this.page.click(this.xpathBtnBuyWithShopPay);

        await this.page.waitForSelector(this.xpathBtnBuyWithShopPay);
        await this.page.click(this.xpathBtnBuyWithShopPay);

        const iframeShopPayPayment = this.page.frameLocator(this.xpathIframeShopPayPayment);
        await iframeShopPayPayment.locator("//input[@id='IdentityEmailForm-input']").fill(this.defaultShopPay.account);
        await iframeShopPayPayment.locator("//button[contains(@class,'button')]").click();
        await this.page.waitForTimeout(7000);
    }

    async buyWithPaypal() {
        await this.page.waitForTimeout(5000);
        const iframePayPal = this.page.frameLocator("//iframe[contains(@title,'PayPal')]");
        const [popup] = await Promise.all([this.page.waitForEvent("popup", { timeout: 5000 }), await iframePayPal.locator(this.xpathBtnBuyWithPayPal).click()]);
        await popup.waitForLoadState();
        return true;

        // login
        // await popup.waitForSelector(this.xpathHeaderTextPayPal);
        // await popup.locator("//input[@id='email']").fill(this.defaultPaypalAccount.username);
        // await popup.locator("//button[contains(@class,'login-click-next')]").click();
        // await popup.waitForTimeout(3000);

        // if (await popup.locator(this.xpathLogoPayPal).isVisible()) {
        //     return true;
        // } else if (await popup.locator(this.xpathHeaderTextPayPal).isVisible()) {
        //     return true;
        // } else if (await popup.locator(this.xpathPasswordPayPal).isVisible()) {
        //     return true;
        // } else {
        //     return false;
        // }

        // await popup.waitForSelector(this.xpathHeaderTextPayPal);
        // await popup.locator(this.xpathPasswordPayPal).fill(this.defaultPaypalAccount.password);
        // await popup.locator(this.xpathBtnLogIn).click();
        // await popup.waitForSelector("//button[@id='change-shipping']");
    }
}