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

test.beforeEach(async ({ page }) => {
  await page.goto('/');
  await page.evaluate(async () => {
    indexedDB.deleteDatabase('ttrpg-manager');
    localStorage.clear();
  });
  await page.reload();
});

async function makeCampaign(page: import('@playwright/test').Page, name: string) {
  await page.getByRole('button', { name: '+ New campaign' }).first().click();
  await page.locator('input[data-autofocus]').fill(name);
  await page.getByRole('button', { name: 'Create' }).click();
}

test('combat HP damage button reduces hit points', async ({ page }) => {
  await makeCampaign(page, 'Combat Test');
  await page.getByRole('link', { name: 'Combat' }).click();
  await page.getByRole('button', { name: '+ New encounter' }).first().click();
  await page.locator('input[data-autofocus]').fill('Fight');
  await page.getByRole('button', { name: 'Create' }).click();

  await page.getByLabel('Name').fill('Goblin');
  await page.getByRole('button', { name: 'Add', exact: true }).click();
  await expect(page.getByText('10/10')).toBeVisible();

  // Enter 4 damage and apply
  await page.getByLabel('Goblin HP amount').fill('4');
  await page.getByRole('button', { name: 'Dmg' }).click();
  await expect(page.getByText('6/10')).toBeVisible();

  // Heal 2 back
  await page.getByLabel('Goblin HP amount').fill('2');
  await page.getByRole('button', { name: 'Heal' }).click();
  await expect(page.getByText('8/10')).toBeVisible();
});

test('combat condition picker adds preset and valued conditions as tags', async ({ page }) => {
  await makeCampaign(page, 'Conditions');
  await page.getByRole('link', { name: 'Combat' }).click();
  await page.getByRole('button', { name: '+ New encounter' }).first().click();
  await page.locator('input[data-autofocus]').fill('Fight');
  await page.getByRole('button', { name: 'Create' }).click();
  await page.getByLabel('Name').fill('Goblin');
  await page.getByRole('button', { name: 'Add', exact: true }).click();

  const row = page.locator('li', { hasText: 'Goblin' });

  // Non-valued condition adds immediately on selection
  await row.getByLabel('Add condition').selectOption('Prone');
  await expect(row.getByRole('button', { name: /Prone/ })).toBeVisible();

  // Valued condition (5e Exhaustion) shows a value field, then adds a tag
  await row.getByLabel('Add condition').selectOption('Exhaustion');
  await row.getByLabel('Condition value').fill('3');
  await row.getByRole('button', { name: 'Add', exact: true }).click();
  await expect(row.getByRole('button', { name: /Exhaustion 3/ })).toBeVisible();

  // Clicking a tag removes it
  await row.getByRole('button', { name: /Prone/ }).click();
  await expect(row.getByRole('button', { name: /Prone/ })).toHaveCount(0);
});

test('character can be deleted', async ({ page }) => {
  await makeCampaign(page, 'Delete Test');
  await page.getByRole('link', { name: 'Characters' }).click();
  await createCharacter(page, 'Doomed'); // lands on the sheet
  await page.getByLabel('Primary').getByRole('link', { name: 'Characters' }).click();
  await expect(page.getByRole('heading', { name: 'Doomed' })).toBeVisible();

  await page.getByRole('button', { name: 'Delete' }).click();
  await page.getByRole('button', { name: 'Delete', exact: true }).last().click();
  await expect(page.getByRole('heading', { name: 'Doomed' })).toHaveCount(0);
});

test('compendium CR filter narrows the bestiary', async ({ page }) => {
  await makeCampaign(page, 'Filter Test');
  await page.getByRole('link', { name: 'Compendium' }).click();
  await expect(page.getByText(/results/)).toBeVisible();

  const countText = async () => (await page.getByText(/results/).textContent()) ?? '';
  const before = parseInt((await countText()).replace(/\D/g, ''), 10);

  await page.getByLabel('Min CR').selectOption({ label: 'CR ≥ 10' });
  // wait for the count to update
  await expect.poll(async () => parseInt((await countText()).replace(/\D/g, ''), 10)).toBeLessThan(before);
});
