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('Sprint M: Filter Menu Enablement', () => {
  test('Loop Subdivision is enabled in Remeshing menu', async ({ page }) => {
    await page.goto('/');
    await page.locator('[data-testid="menu-bar"]').waitFor({ state: 'visible', timeout: 10000 });
    await page.click('[data-testid="menu-Filters"]');
    await page.click('text=Remeshing, Simplification and Reconstruction');
    const item = page.locator('text=Loop Subdivision').first();
    await expect(item).toBeVisible();
    await expect(item).not.toHaveAttribute('data-disabled', 'true');
  });

  test('Butterfly Subdivision is enabled', async ({ page }) => {
    await page.goto('/');
    await page.click('[data-testid="menu-Filters"]');
    await page.click('text=Remeshing, Simplification and Reconstruction');
    const item = page.locator('text=Butterfly Subdivision').first();
    await expect(item).toBeVisible();
    await expect(item).not.toHaveAttribute('data-disabled', 'true');
  });

  test('Montecarlo Sampling is enabled', async ({ page }) => {
    await page.goto('/');
    await page.click('[data-testid="menu-Filters"]');
    await page.click('text=Sampling');
    const item = page.locator('text=Montecarlo Sampling').first();
    await expect(item).toBeVisible();
    await expect(item).not.toHaveAttribute('data-disabled', 'true');
  });

  test('Create Sphere is enabled in Mesh Creation', async ({ page }) => {
    await page.goto('/');
    await page.click('[data-testid="menu-Filters"]');
    await page.click('text=Create New Mesh Layer');
    const item = page.locator('text=Sphere').first();
    await expect(item).toBeVisible();
    await expect(item).not.toHaveAttribute('data-disabled', 'true');
  });

  test('Flatten Visible Layers is enabled', async ({ page }) => {
    await page.goto('/');
    await page.click('[data-testid="menu-Filters"]');
    await page.click('text=Mesh Layer');
    const item = page.locator('text=Flatten Visible Layers').first();
    await expect(item).toBeVisible();
    await expect(item).not.toHaveAttribute('data-disabled', 'true');
  });

  test('Mesh Boolean is enabled', async ({ page }) => {
    await page.goto('/');
    await page.click('[data-testid="menu-Filters"]');
    await page.click('text=Other');
    const item = page.locator('text=Mesh Boolean').first();
    await expect(item).toBeVisible();
    await expect(item).not.toHaveAttribute('data-disabled', 'true');
  });
});

test.describe('Sprint M: Filter Execution', () => {
  test('Loop Subdivision dialog opens and applies', 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=Remeshing, Simplification and Reconstruction');
    await page.click('text=Loop Subdivision');

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

  test('Create Sphere generates a new layer', async ({ page }) => {
    await page.goto('/');
    await page.locator('canvas').first().waitFor({ state: 'visible', timeout: 10000 });

    await page.click('[data-testid="menu-Filters"]');
    await page.click('text=Create New Mesh Layer');
    await page.click('text=Sphere');

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

    // Should have vertex count showing non-zero
    await expect(page.locator('[data-testid="vertex-count"]')).toBeVisible();
    await expect(page.locator('[data-testid="vertex-count"]')).not.toContainText('0');
  });

  test('Isotropic Remeshing dialog opens and applies', 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=Remeshing, Simplification and Reconstruction');
    await page.click('text=Explicit Isotropic Remeshing');

    await expect(page.locator('[data-testid="filter-dialog"]')).toBeVisible();
    await page.click('[data-testid="filter-apply"]');
    await page.waitForTimeout(2000);
    await page.click('[data-testid="filter-close"]');
    await expect(page.locator('[data-testid="status-message"]')).toContainText('isotropic_remeshing');
  });
});

test.describe('Sprint M: About Dialog Filter Count', () => {
  test('About dialog shows 40 Filters', 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');
  });
});
