import { test, expect } from '@playwright/test';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));

async function loadCube(page: import('@playwright/test').Page) {
  await page.locator('#file-input').setInputFiles(resolve(__dirname, '../fixtures/cube.obj'));
  await page.waitForTimeout(3000);
}

test.describe('Menu Bar', () => {
  test('menu bar is visible with all menus', async ({ page }) => {
    await page.goto('/');
    await expect(page.locator('[data-testid="menu-bar"]')).toBeVisible();
    await expect(page.locator('[data-testid="menu-File"]')).toBeVisible();
    await expect(page.locator('[data-testid="menu-Edit"]')).toBeVisible();
    await expect(page.locator('[data-testid="menu-Filters"]')).toBeVisible();
    await expect(page.locator('[data-testid="menu-Render"]')).toBeVisible();
    await expect(page.locator('[data-testid="menu-View"]')).toBeVisible();
    await expect(page.locator('[data-testid="menu-Tools"]')).toBeVisible();
    await expect(page.locator('[data-testid="menu-Help"]')).toBeVisible();
  });

  test('File menu opens on click', async ({ page }) => {
    await page.goto('/');
    await page.click('[data-testid="menu-File"]');
    await expect(page.locator('text=Import Mesh...')).toBeVisible();
  });
});

test.describe('Filter Dialog', () => {
  test('filter with params opens dialog from menu', async ({ page }) => {
    await page.goto('/');
    await page.locator('canvas').first().waitFor({ state: 'visible', timeout: 10000 });
    await loadCube(page);

    await page.click('[data-testid="menu-Filters"]');
    await page.click('text=Smoothing');
    await page.click('text=Laplacian Smooth...');

    await expect(page.locator('[data-testid="filter-dialog"]')).toBeVisible();
    await expect(page.locator('[data-testid="filter-dialog-title"]')).toContainText('Laplacian Smooth');

    await page.click('[data-testid="filter-apply"]');
    await expect(page.locator('[data-testid="filter-dialog"]')).toBeVisible();

    await page.click('[data-testid="filter-close"]');
    await expect(page.locator('[data-testid="filter-dialog"]')).not.toBeVisible();
  });

  test('filter without params executes directly', async ({ page }) => {
    await page.goto('/');
    await page.locator('canvas').first().waitFor({ state: 'visible', timeout: 10000 });
    await loadCube(page);

    await page.click('[data-testid="menu-Filters"]');
    await page.locator('[data-testid="menu-bar"]').locator('text=Normals').click();
    await page.click('text=Recompute Face Normals');

    await expect(page.locator('[data-testid="filter-dialog"]')).not.toBeVisible();
    await page.waitForTimeout(1000);
    await expect(page.locator('[data-testid="status-message"]')).toContainText('compute_normals');
  });
});

test.describe('About Dialog', () => {
  test('About dialog shows from Help menu', async ({ page }) => {
    await page.goto('/');
    await page.click('[data-testid="menu-Help"]');
    await page.click('text=About MeshLab Web');
    await expect(page.locator('[data-testid="about-dialog"]')).toBeVisible();
    await expect(page.locator('[data-testid="about-dialog"]')).toContainText('147 Filters');
  });
});

test.describe('Project Panel', () => {
  test('project panel always visible', async ({ page }) => {
    await page.goto('/');
    await expect(page.locator('[data-testid="project-panel"]')).toBeVisible({ timeout: 10000 });
  });
});
