import { test, expect } from '@playwright/test';
import { clickWithTryCatch } from '../../src/core/utils';
import { launchBrowserWithISPProxy, launchBrowserWithProxy } from '../../helper/lauch.proxy';
import { randomString } from '../../helper/random.string';
import { attachGeoLocation } from '../../helper/attach.geo';

test('German checkout test', {
  annotation: {
    type: 'DE',
    description: 'Stripe - DE - German checkout test'
  },
  tag: "@MARIAM_DE"
}, async ({ }, testInfo) => {
  test.setTimeout(180_000);
  // Navigate to the German store URL
  const { page } = await launchBrowserWithISPProxy(randomString(10), 'de');
  await page.goto('https://de.perifit.co/collections/our-products', { waitUntil: 'domcontentloaded' });
  await expect(page).toHaveURL(/de\.perifit\.co/);

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

  const popupLocator = page.locator("//button[contains(text(), 'Allow all')]");
  clickWithTryCatch(popupLocator, 10_000);

  // Navigate through the checkout process
  // await page.locator("//div[contains(@class, 'announcement-bar__wrapper')]").click();
  await page.waitForTimeout(3_000);
  await page.locator("//span[contains(@class, 'product-card__title')]//a").first().click({ force: true });

  // await firstProduct.click();
  const isDisplayed = await page.locator("//div[@id='row_JVD6sA4BbE']").isVisible({ timeout: 5_000 });
  if (isDisplayed) {
    await page.click("//div[@id='pge_hpy1uwpNS']//span[contains(@class, 'om-popup-close-x')]");
  }
  await page.locator("//button[contains(@class,'button button--xl button--secondary')]").waitFor({ state: 'visible' });
  await page.waitForTimeout(5_000);
  await page.locator("//button[contains(@class,'button button--xl button--secondary')]").click();
  await page.waitForTimeout(5_000);
  await page.locator("(//button[@name='checkout'])[2]").click();

  // Fill in the checkout form
  // await page.getByPlaceholder('E-Mail').fill('for.test.perifit@gmail.com');
  await page.waitForTimeout(4000);
  await page.locator("//input[@placeholder='E-Mail']").fill('for.test.perifit@gmail.com');
  await page.getByPlaceholder('Vorname').fill('Test');
  await page.getByPlaceholder('Nachname').fill('Auto');
  await page.getByPlaceholder('Adresse').type('Airport Nürnberg (NUE', { delay: 100 });
  await page.waitForTimeout(2_000);
  await expect(page.getByText("Vorschläge")).toBeVisible({ timeout: 10_000 });
  await page.getByText('Airport Nürnberg').first().click();
  await page.getByPlaceholder('Telefon').fill('49 30 12345678');

  // Ensure iframes are available
  await page.waitForSelector('iframe[name^="card-fields-number"]', { timeout: 60000 });
  await page.waitForSelector('iframe[name^="card-fields-expiry"]', { timeout: 60000 });
  await page.waitForSelector('iframe[name^="card-fields-verification_value"]', { timeout: 60000 });

  // Interact with the card number iframe
  const iframeCardNumber = page.frameLocator('iframe[name^="card-fields-number"]');
  await iframeCardNumber.locator('input[placeholder="Kartennummer"]').fill('4578 8900 8186 8844');
  // await iframeCardNumber.locator('input[placeholder="Kartennummer"]').fill('4396 5501 0322 2509');
  // await iframeCardNumber.locator('input[placeholder="Kartennummer"]').fill('4396 5501 0569 7773');

  // Interact with the expiry date iframe
  const iframeExpiryDate = page.frameLocator('iframe[name^="card-fields-expiry"]');
  await iframeExpiryDate.locator('input[placeholder="Gültig bis (MM/JJ)"]').fill('02 / 28');
  // await iframeExpiryDate.locator('input[placeholder="Gültig bis (MM/JJ)"]').fill('11 / 29');
  // await iframeExpiryDate.locator('input[placeholder="Gültig bis (MM/JJ)"]').fill('11 / 30');

  // Interact with the CVV iframe
  const iframeCvv = page.frameLocator('iframe[name^="card-fields-verification_value"]');
  await iframeCvv.locator('input[placeholder="Sicherheitscode"]').fill('732');
  // await iframeCvv.locator('input[placeholder="Sicherheitscode"]').fill('346');
  // await iframeCvv.locator('input[placeholder="Sicherheitscode"]').fill('634');

  // Click the 'Bestellung überprüfen' button to review the order
  await page.getByRole('button', { name: 'Bestellung überprüfen' }).click();

  // Wait to ensure that the review page has loaded
  await page.waitForTimeout(5000);

  // Print the current URL to check redirection
  console.log(`Current URL after 'Bestellung überprüfen': ${page.url()}`);

  // Click the 'Jetzt bezahlen' button to complete the payment
  await page.locator("//button[@id='checkout-pay-button']").click();

  // Wait to ensure that the payment page has loaded
  await page.waitForTimeout(3000);

  // Get the final URL and assert that it contains the expected part
  const finalUrl = page.url();
  console.log(`Final URL after payment: ${finalUrl}`);
  expect(finalUrl).toMatch(/(payment|review)/);;

  // await page.waitForTimeout(3000);
  // let isErrorDisplayed = await page.locator("//div[@id='PaymentErrorBanner']").isVisible();
  // if (isErrorDisplayed) {
  //   expect(isErrorDisplayed).toBeTruthy();
  // } else {
  //   await page.locator("//button[@id='checkout-pay-button']").click();;
  //   await expect(page.locator("//div[@id='PaymentErrorBanner']")).toBeVisible({ timeout: 15_000 });
  // }
});
