
import { test, expect } from "@playwright/test";
import { CheckoutPage } from "../../page/checkout-page";
import { CartPage } from "../../page/cart-page";
import { ProductPage } from "../../page/product-page";
import { HomePage } from "../../page/home-page";
import { allowAllCookies, closeWheel } from "../../src/core/popup";
import { attachGeoLocation } from "../../helper/attach.geo";

let checkoutPage: CheckoutPage;
let homePage: HomePage;
let productPage: ProductPage;
let cartPage: CartPage;

test.describe("SHOP PAY PAYMENT", () => {
  // test.beforeEach(async ({ page }) => {
  //   checkoutPage = new CheckoutPage(page);
  //   homePage = new HomePage(page);
  //   productPage = new ProductPage(page);
  //   cartPage = new CartPage(page);

  //   await homePage.goToHomePage("https://uk.perifit.co");
  //   await allowAllCookies(page);
  //   await closeWheel(page);
  //   await homePage.page.locator(homePage.xpathAnnouncementBar).click();
  // });

  // test('Checkout with Shoppay in Product page', {
  //   annotation: {
  //     type: 'SPE_PRD',
  //     description: 'Checkout with Shoppay in Product page'
  //   }
  // }, async ({ page }) => {
  //   await test.step("Navigate to Product page", async () => {
  //     const firstProduct = page.locator(productPage.xpathFirstProductInCollection).first();
  //     await firstProduct.click();
  //     await productPage.closePopUpSale();
  //   });

  //   await test.step("Checkout order with Shoppay", async () => {
  //     await productPage.buyWithShopPay();

  //     const isCapchaDisplayed = await checkoutPage.checkElementAttached(checkoutPage.xpathBtnChangeShopPayPayment, checkoutPage.page.frameLocator(checkoutPage.xpathIframeShopPayPayment));
  //     expect(isCapchaDisplayed).toBeTruthy();
  //   });
  // });

  // test('UK - Checkout with Shoppay in Cart page', {
  //   annotation: {
  //     type: 'SPE_CRT',
  //     description: 'Checkout with Shop pay'
  //   }
  // }, async ({ page }) => {
  //   await test.step("Add product to cart", async () => {
  //     const firstProduct = page.locator(productPage.xpathFirstProductInCollection).first();
  //     await firstProduct.click();
  //     await productPage.clickBtnAddToCart();
  //   });

  //   await test.step("Checkout order with Shoppay", async () => {
  //     await cartPage.buyWithShopPay();

  //     const isCapchaDisplayed = await checkoutPage.checkElementAttached(checkoutPage.xpathBtnChangeShopPayPayment, checkoutPage.page.frameLocator(checkoutPage.xpathIframeShopPayPayment));
  //     expect(isCapchaDisplayed).toBeTruthy();
  //   });
  // });

  // test('UK - Checkout with Shoppay in Checkout page', {
  //   annotation: {
  //     type: 'SPE_COP',
  //     description: 'Checkout with Shop pay'
  //   }
  // }, async ({ page }) => {
  //   const email = "for.test.perifit@gmail.com";
  //   await test.step("Add product to cart", async () => {
  //     const firstProduct = page.locator(productPage.xpathFirstProductInCollection).first();
  //     await firstProduct.click();
  //     await productPage.clickBtnAddToCart();
  //   });

  //   await test.step("Click checkout", async () => {
  //     await cartPage.clickBtnCheckout();
  //   });

  //   await test.step("Checkout order with ShopPay Express", async () => {
  //     await checkoutPage.checkoutWithShopPayExpress(email);

  //     const isCapchaDisplayed = await checkoutPage.checkElementAttached(checkoutPage.xpathBtnChangeShopPayPayment, checkoutPage.page.frameLocator(checkoutPage.xpathIframeShopPayPayment));
  //     expect(isCapchaDisplayed).toBeTruthy();
  //   });
  // });

  // test('UK - Checkout with Shop pay Credit Card in Checkout page', {
  //   annotation: {
  //     type: 'SPE_COP',
  //     description: 'Checkout with Shop pay'
  //   }
  // }, async ({ page }) => {
  //   await test.step("Add product to cart", async () => {
  //     const firstProduct = page.locator(productPage.xpathFirstProductInCollection).first();
  //     await firstProduct.click();
  //     await productPage.clickBtnAddToCart();
  //   });

  //   await test.step("Click checkout", async () => {
  //     await cartPage.clickBtnCheckout();
  //   });

  //   await test.step("Fill checkout info", async () => {
  //     const shippingAddress = checkoutPage.shippingAddress;
  //     await checkoutPage.fillCheckoutInfo(shippingAddress);
  //   });

  //   await test.step("Complete order with Shop pay", async () => {
  //     await checkoutPage.page.waitForSelector(checkoutPage.xpathHeadingPayment);
  //     await checkoutPage.uncheckRememberMe();
  //     await page.waitForTimeout(3_000);
  //     await checkoutPage.checkoutWithShopPay();
  //   });

  //   await test.step("Verify message after checkout with Credit Card", async () => {
  //     await checkoutPage.page.waitForSelector(checkoutPage.xpathErrorMessage);
  //     await expect(checkoutPage.page.locator(checkoutPage.xpathErrorMessage)).toBeVisible();
  //   });
  // });
});

test.describe("@COF_UK PAYPAL PAYMENT", {
  tag: ["@COF_UK", "@COF_UK_PP"]
}, () => {
  test.beforeEach(async ({ page }, testInfo) => {
    checkoutPage = new CheckoutPage(page);
    homePage = new HomePage(page);
    productPage = new ProductPage(page);
    cartPage = new CartPage(page);

    await homePage.goToHomePage("https://de.perifit.co");
    await expect(page).toHaveURL(/uk\.perifit\.co/, { timeout: 15_000 });

    await allowAllCookies(page);
    await closeWheel(page);
    await homePage.page.locator(homePage.xpathAnnouncementBar).click();

    //Attach geo location to the report
    await attachGeoLocation(page, testInfo);
  });

  // test('Checkout with PayPal express in Product page', {
  //   annotation: {
  //     type: 'PPE_PRD',
  //     description: 'Checkout with PayPal express in Product page'
  //   },
  //   tag: ["@COF_UK_PPE_PRD"]
  // }, async ({ page }) => {
  //   await test.step("Navigate to Product page", async () => {
  //     const firstProduct = page.locator(productPage.xpathFirstProductInCollection).first();
  //     await firstProduct.click();
  //     await productPage.closePopUpSale();
  //   });

  //   await test.step("Checkout order with PayPal express", async () => {
  //     const logoDisplayed = await productPage.buyWithPaypal();
  //     expect(logoDisplayed).toBeTruthy();
  //   });
  // });

  test('UK - PayPal express - Checkout with PayPal express in Cart page', {
    annotation: {
      type: 'PPE_CRT',
      description: 'Checkout with PayPal express in Cart page'
    },
    tag: ["@COF_UK_PPE_CRT"]
  }, async ({ page }) => {
    await test.step("Add product to cart", async () => {
      const firstProduct = page.locator(productPage.xpathFirstProductInCollection).first();
      await firstProduct.click();
      await productPage.clickBtnAddToCart();
    });

    await test.step("Checkout order with PayPal express", async () => {
      const logoDisplayed = await cartPage.buyWithPaypalInCheckout();
      expect(logoDisplayed).toBeTruthy();
    });
  });

  test('UK - PayPal express - Checkout with PayPal express in Checkout page', {
    annotation: {
      type: 'PPE_COP',
      description: 'Checkout with PayPal express in Checkout page'
    },
    tag: ["@COF_UK_PPE_COP"]
  }, async ({ page }) => {
    await test.step("Add product to cart", async () => {
      const firstProduct = page.locator(productPage.xpathFirstProductInCollection).first();
      await firstProduct.click();
      await productPage.clickBtnAddToCart();
    });

    await test.step("Click checkout", async () => {
      await cartPage.clickBtnCheckout();
    });

    await test.step("Checkout order with PayPal Express", async () => {
      const logoDisplayed = await checkoutPage.checkoutWithPayPalExpress();
      expect(logoDisplayed).toBeTruthy();
    });
  });

  test('UK - PayPal Standard - Checkout with PayPal Standard in Checkout page', {
    annotation: {
      type: 'PP_COP',
      description: 'Checkout with PayPal'
    },
    tag: ["@COF_UK_PP_COP"]
  }, async ({ page }) => {
    await test.step("Add product to cart", async () => {
      const firstProduct = page.locator(productPage.xpathFirstProductInCollection).first();
      await firstProduct.click();
      await productPage.clickBtnAddToCart();
    });

    await test.step("Click checkout", async () => {
      await cartPage.clickBtnCheckout();
    });

    await test.step("Fill checkout info", async () => {
      const shippingAddress = checkoutPage.shippingAddress;
      await checkoutPage.fillCheckoutInfo(shippingAddress);
    });

    await test.step("Complete order with PayPal", async () => {
      await checkoutPage.page.waitForSelector(checkoutPage.xpathHeadingPayment);
      const logoDisplayed = await checkoutPage.checkoutWithPaypal();
      expect(logoDisplayed).toBeTruthy();
    });
  });
});