import { test, expect } from '@playwright/test';

test('Record something given', async ({ page }) => {
  // Generate a random string of 16 characters
  let randomString = Math.random().toString(36).substring(2, 18);

  // In case the string is shorter than 16 characters, generate more characters until it is 16 characters long
  while (randomString.length < 16) {
    randomString += Math.random().toString(36).substring(2, 18);
  }
  const finalRandomString = randomString.substring(0, 16);

  // Generate a random non-zero single-digit number
  const randomNonZeroNumber = Math.floor(Math.random() * 99) + 1;

  // Standard title prefix
  const standardTitle = "Gift ";

  // Combine title prefix with the random string
  const finalTitle = standardTitle + finalRandomString;

  // Create new ID using seed phrase "rigid shrug mobileā€¦"
  await page.goto('./start');
  await page.getByText('You have a seed').click();
  await page.getByPlaceholder('Seed Phrase').fill('rigid shrug mobile smart veteran half all pond toilet brave review universe ship congress found yard skate elite apology jar uniform subway slender luggage');
  await page.getByRole('button', { name: 'Import' }).click();

  // Set name
  await page.getByRole('link', { name: 'Set Your Name' }).click();
  await page.getByPlaceholder('Name').fill('User Zero');
  await page.getByRole('button', { name: 'Save Changes' }).click();

  // Record something given
  await page.goto('./');
  await page.getByRole('heading', { name: 'Unnamed/Unknown' }).click();
  await page.getByPlaceholder('What was given').fill(finalTitle);
  await page.getByRole('spinbutton').fill(randomNonZeroNumber.toString());
  await page.getByRole('button', { name: 'Sign & Send' }).click();
  await expect(page.getByText('That gift was recorded.')).toBeVisible();

  // Refresh home view and check gift
  await page.goto('./');
  await page.locator('li').filter({ hasText: finalTitle }).locator('a').click();
  await expect(page.getByRole('heading', { name: 'Verifiable Claim Details' })).toBeVisible();
  await expect(page.getByText(finalTitle, { exact: true })).toBeVisible();
  const page1Promise = page.waitForEvent('popup');
  await page.getByRole('link', { name: 'View on the Public Server' }).click();
  const page1 = await page1Promise;
});