import { test, expect } from '@playwright/test';
import { BASE_URL, USER, loginAs, createYoutubeVideoEvent } from '../helpers';

// The WFBO Live Racing monitor plays the race video in the same player as the
// players' page (templates/play/live-racing/_video.html.twig): no YouTube
// controls, LIVE badge, our sound button, the site logo, and the BGM as set on
// the monitor - so the floor sees and hears what the players do.

test.use({ launchOptions: { args: ['--autoplay-policy=no-user-gesture-required'] } });

const VIDEO_2 = 'https://www.youtube.com/watch?v=M7lc1UVf-VE';

test.describe('WFBO Live Racing monitor — same video player as the players', () => {
    test.setTimeout(180_000);

    test('monitor and player page show the same player, and both follow Main / BGM', async ({ page, browser }, testInfo) => {
        const eventId = await createYoutubeVideoEvent(page, 'LRMonPlayer');
        const monitorUrl = `${BASE_URL}/wfbo/live-racing/monitor?event=${eventId}`;
        await page.setViewportSize({ width: 1600, height: 900 });
        await page.goto(monitorUrl);

        const mon = page.locator('[data-testid="lr-monitor-video"]');
        const monContainer = mon.locator('#yt-player-container');
        await expect(monContainer).toHaveAttribute('data-player-ready', 'true', { timeout: 30_000 });
        const monIframe = mon.locator('iframe#yt-player');
        expect(await monIframe.getAttribute('src')).toContain('ZGylNaQtilE');
        expect(await monIframe.getAttribute('src')).toContain('controls=0');
        await expect(monIframe).toHaveCSS('pointer-events', 'none');
        await expect(mon.locator('[data-testid="lr-video-live"]')).toBeVisible();
        await expect(mon.locator('[data-testid="lr-video-logo"]')).toBeVisible();
        const monSound = mon.locator('[data-testid="lr-video-sound-toggle"]');
        await expect(monSound).toBeVisible();

        // The monitor's controls sit top right, clear of the LIVE badge, and stay clickable.
        const strip = (await page.locator('[data-testid="lr-monitor-video-controls"] .lr-mon__vctl-group').first().boundingBox())!;
        const live = (await mon.locator('[data-testid="lr-video-live"]').boundingBox())!;
        expect(strip.x).toBeGreaterThan(live.x + live.width);
        await page.waitForTimeout(2000);
        await page.screenshot({ path: testInfo.outputPath('monitor-same-player.png') });

        // Its sound button works on the monitor too.
        const before = await monContainer.getAttribute('data-muted');
        await monSound.click();
        await expect(monContainer).toHaveAttribute('data-muted', before === 'true' ? 'false' : 'true');
        await monSound.click();
        await expect(monContainer).toHaveAttribute('data-muted', before ?? 'false');

        // Video 2 as the BGM: the monitor plays it hidden, main muted - like the players.
        await page.click('[data-testid="lr-monitor-video-links-toggle"]');
        await page.fill('[data-testid="lr-monitor-video-url-2"]', VIDEO_2);
        await page.click('[data-testid="lr-monitor-video-links-save"]');
        await page.click('[data-testid="lr-monitor-sound-bgm"]');
        await expect(page.locator('[data-testid="lr-monitor-sound-bgm"]')).toHaveClass(/is-on/);
        await expect(monContainer).toHaveAttribute('data-bgm', 'youtube', { timeout: 20_000 });
        const monBgm = mon.locator('[data-testid="lr-video-bgm-player"]');
        await expect(monBgm).toBeAttached({ timeout: 30_000 });
        expect(await monBgm.getAttribute('src')).toContain('M7lc1UVf-VE');
        expect(await monIframe.getAttribute('src')).toContain('mute=1');
        await page.waitForTimeout(2000);
        await page.screenshot({ path: testInfo.outputPath('monitor-same-player-bgm.png') });

        // The player page shows the very same setup.
        const ctx = await browser.newContext({ viewport: { width: 1280, height: 900 } });
        const player = await ctx.newPage();
        await loginAs(player, USER.mobile, USER.password);
        await player.goto(`${BASE_URL}/play/live-racing/${eventId}`);
        const pContainer = player.locator('#yt-player-container');
        await expect(pContainer).toHaveAttribute('data-bgm', 'youtube', { timeout: 30_000 });
        expect(await player.locator('iframe#yt-player').getAttribute('src')).toContain('ZGylNaQtilE');
        await expect(player.locator('[data-testid="lr-video-bgm-player"]')).toBeAttached({ timeout: 30_000 });
        await player.waitForTimeout(1500);
        await player.screenshot({ path: testInfo.outputPath('player-same-setup.png') });
        await ctx.close();

        // Back to the main video's own sound.
        await page.click('[data-testid="lr-monitor-sound-main"]');
        await expect(mon.locator('[data-testid="lr-video-bgm-player"]')).toHaveCount(0, { timeout: 20_000 });
    });
});
