const { test, expect } = require('@playwright/test');
const { openController, openDisplay, resetGame } = require('./helpers.cjs');

// Il flusso usa sempre l'opzione "Nessun video": non dipende dal contenuto
// della cartella spot/ e resta deterministico su qualsiasi macchina.
test.describe('Time Out', () => {

    test('flusso completo: avvio, overlay sul display, chiusura anticipata', async ({ context }) => {
        const displayPage = await openDisplay(context);
        const controllerPage = await openController(context);
        await resetGame(controllerPage);

        // Apri la modal time out
        await controllerPage.locator('.btn-timeout').click();
        await expect(controllerPage.locator('.dialog-timeout')).toBeVisible();

        // Avvia è disabilitato finché non si sceglie la squadra
        const avvia = controllerPage.locator('.dialog-timeout .btn-confirm');
        await expect(avvia).toBeDisabled();

        // Scegli squadra Home e nessun video
        await controllerPage.locator('.dialog-timeout .btn-set.home-bg').click();
        await controllerPage.locator('.timeout-video-option').last().locator('input').check();
        await expect(avvia).toBeEnabled();
        await avvia.click();

        // Display: overlay a tutto schermo con countdown e squadra
        await expect(displayPage.locator('.timeout-overlay')).toBeVisible();
        await expect(displayPage.locator('.timeout-placeholder')).toContainText('TIME OUT');
        await expect(displayPage.locator('.timeout-placeholder')).toContainText('Antoniana');
        await expect(displayPage.locator('.timeout-countdown')).toBeVisible();

        // Controller: banner attivo con nome squadra
        await expect(controllerPage.locator('.timeout-banner')).toContainText('TIME OUT');
        await expect(controllerPage.locator('.timeout-banner')).toContainText('Antoniana');

        // Chiusura anticipata: il bottone (che mostra il countdown) invia stopTimeout
        await controllerPage.locator('.btn-timeout').click();
        await expect(displayPage.locator('.timeout-overlay')).not.toBeVisible();
        await expect(controllerPage.locator('.timeout-banner')).not.toBeVisible();
    });

    test('Annulla chiude la modal senza avviare il time out', async ({ context }) => {
        const displayPage = await openDisplay(context);
        const controllerPage = await openController(context);
        await resetGame(controllerPage);

        await controllerPage.locator('.btn-timeout').click();
        await expect(controllerPage.locator('.dialog-timeout')).toBeVisible();

        await controllerPage.locator('.dialog-timeout .btn-cancel').click();
        await expect(controllerPage.locator('.dialog-timeout')).not.toBeVisible();
        await expect(controllerPage.locator('.timeout-banner')).not.toBeVisible();
        await expect(displayPage.locator('.timeout-overlay')).not.toBeVisible();
    });
});
