import { test, expect, type Page } from '@playwright/test';
import { BASE_URL, ADMIN, loginAs, registerAndLogin } from '../helpers';

/** WFBO ▸ Settings ▸ Seasonal Theme: pick one theme, or 'none', and the flying effects on/off. */
async function setSiteTheme(page: Page, theme: 'none' | 'halloween' | 'christmas' | 'new_year' | 'valentines', effects = true): Promise<void> {
    await loginAs(page, ADMIN.mobile, ADMIN.password);
    await page.goto(`${BASE_URL}/wfbo/settings`);
    await page.check(`[data-testid="input-site-theme-${theme}"]`);
    await page.setChecked('[data-testid="input-site-theme-effects"]', effects);
    await page.click('[data-testid="btn-save-settings"]');
    await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    await expect(page.locator(`[data-testid="input-site-theme-${theme}"]`)).toBeChecked();
}

// Serial: the theme is one site-wide setting.
test.describe.configure({ mode: 'serial' });

test.describe('Seasonal site theme', () => {
    test.setTimeout(120_000);

    test.afterAll(async ({ browser }) => {
        // Never leave a theme on for the other specs.
        const page = await browser.newPage();
        await setSiteTheme(page, 'none');
        await page.close();
    });

    test('a theme picked in WFBO settings decorates player pages, only the navbar inside a game, and None takes it off', async ({ page }, testInfo) => {
        await setSiteTheme(page, 'christmas');

        const player = await registerAndLogin(page);
        await page.goto(`${BASE_URL}/`);
        await expect(page.locator('body')).toHaveAttribute('data-season-theme', 'christmas');
        await expect(page.locator('[data-testid="season-decor"]')).toBeAttached();
        const effect = page.locator('[data-testid="season-effect"]');
        // three.js canvas (or the CSS particles where WebGL is missing).
        await expect(effect.locator('canvas, .season-particle').first()).toBeAttached();
        await expect(effect).toHaveCSS('pointer-events', 'none');
        await page.waitForTimeout(1500);
        await page.screenshot({ path: testInfo.outputPath('season-christmas-phone.png') });

        // The effect never blocks a click: the game card still opens - and inside the game only the
        // navbar is themed: no decorations, no flying effect over the game.
        await page.click('[data-testid="play-video-karera-card"]');
        await expect(page).toHaveURL(/\/play\/video-karera/);
        await expect(page.locator('body')).toHaveAttribute('data-season-theme', 'christmas');
        await expect(page.locator('[data-testid="season-effect"]')).toHaveCount(0);
        await expect(page.locator('[data-testid="season-decor"]')).toHaveCount(0);
        await page.screenshot({ path: testInfo.outputPath('season-christmas-game-phone.png') });

        // Nor in the back office.
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo`);
        await expect(page.locator('[data-testid="season-effect"]')).toHaveCount(0);

        // Each theme, on a phone and on a desktop.
        for (const theme of ['halloween', 'new_year', 'valentines', 'christmas'] as const) {
            await setSiteTheme(page, theme);
            await loginAs(page, player.mobile, player.password);
            for (const [label, size] of [['phone', { width: 390, height: 844 }], ['desktop', { width: 1366, height: 768 }]] as const) {
                await page.setViewportSize(size);
                await page.goto(`${BASE_URL}/`);
                await expect(page.locator('body')).toHaveAttribute('data-season-theme', theme);
                await expect(page.locator('[data-testid="season-effect"] canvas, [data-testid="season-effect"] .season-particle').first()).toBeAttached();
                await page.waitForTimeout(1500);
                await page.screenshot({ path: testInfo.outputPath(`season-${theme}-${label}.png`) });
            }
        }

        // None: the site is back to normal.
        await setSiteTheme(page, 'none');
        await loginAs(page, player.mobile, player.password);
        await page.goto(`${BASE_URL}/`);
        await expect(page.locator('body')).not.toHaveAttribute('data-season-theme', /.+/);
        await expect(page.locator('[data-testid="season-effect"]')).toHaveCount(0);
        await expect(page.locator('[data-testid="season-decor"]')).toHaveCount(0);
    });

    test('with Flying effects off a theme keeps its navbar and decorations but nothing flies', async ({ page }, testInfo) => {
        const player = await registerAndLogin(page);

        for (const theme of ['halloween', 'valentines'] as const) {
            await setSiteTheme(page, theme, false);
            await expect(page.locator('[data-testid="input-site-theme-effects"]')).not.toBeChecked();
            await page.screenshot({ path: testInfo.outputPath(`season-${theme}-effects-off-settings.png`), fullPage: false });

            await loginAs(page, player.mobile, player.password);
            for (const [label, size] of [['phone', { width: 390, height: 844 }], ['desktop', { width: 1366, height: 768 }]] as const) {
                await page.setViewportSize(size);
                await page.goto(`${BASE_URL}/`);
                await expect(page.locator('body')).toHaveAttribute('data-season-theme', theme);
                await expect(page.locator('[data-testid="season-decor"]')).toBeAttached();
                await expect(page.locator('[data-testid="season-effect"]')).toHaveCount(0);
                await expect(page.locator('[data-controller="season"]')).toHaveCount(0);
                await page.waitForTimeout(1000);
                await page.screenshot({ path: testInfo.outputPath(`season-${theme}-effects-off-${label}.png`) });
            }
        }

        // Ticking it again brings the effect back.
        await setSiteTheme(page, 'halloween', true);
        await loginAs(page, player.mobile, player.password);
        await page.goto(`${BASE_URL}/`);
        await expect(page.locator('[data-testid="season-effect"] canvas, [data-testid="season-effect"] .season-particle').first()).toBeAttached();
    });
});
