/**
 * game-layout.js
 * Script pendukung layout game mobile.
 *
 * CARA PAKAI di file PHP baru:
 *   <script src="assets/js/game-layout.js"></script>
 *   (letakkan sebelum </body>)
 *
 * Yang dilakukan script ini:
 *   1. Memblokir semua scroll (touch, wheel, keyboard)
 *   2. Mengupdate elemen #clock setiap menit
 */

(function () {
    'use strict';

    /* -----------------------------------------------
       1. BLOKIR SCROLL — touch, wheel, keyboard
    ----------------------------------------------- */
    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
    }, { passive: false });

    document.addEventListener('wheel', function (e) {
        e.preventDefault();
    }, { passive: false });

    document.addEventListener('keydown', function (e) {
        // Jangan blokir jika event berasal dari input atau textarea agar user bisa mengetik spasi dan navigasi arah
        if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
            return;
        }
        const blocked = [' ', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight',
                         'PageUp', 'PageDown', 'Home', 'End'];
        if (blocked.includes(e.key)) {
            e.preventDefault();
        }
    });

    /* -----------------------------------------------
       2. JAM DIGITAL — update elemen #clock
    ----------------------------------------------- */
    function updateClock() {
        var el = document.getElementById('clock');
        if (!el) return;
        var now  = new Date();
        var h    = String(now.getHours()).padStart(2, '0');
        var m    = String(now.getMinutes()).padStart(2, '0');
        el.textContent = h + ':' + m;
    }

    updateClock();
    setInterval(updateClock, 15000);

    /* -----------------------------------------------
       3. KONFIRMASI FULLSCREEN (Jika diakses langsung)
    ----------------------------------------------- */
    const isAccessedDirectly = window.self === window.top;
    const isFullscreenSupported = document.documentElement.requestFullscreen ||
                                  document.documentElement.webkitRequestFullscreen ||
                                  document.documentElement.msRequestFullscreen;

    if (isAccessedDirectly && isFullscreenSupported) {
        // Buat elemen modal secara dinamis
        const overlay = document.createElement('div');
        overlay.id = 'fullscreen-modal-overlay';
        overlay.innerHTML = `
            <div class="fullscreen-modal-card">
                <div class="fullscreen-modal-title">✦ FULLSCREEN MODE ✦</div>
                <div class="fullscreen-modal-body">
                    Mainkan game dalam mode Fullscreen untuk pengalaman bermain terbaik?
                </div>
                <div class="fullscreen-modal-buttons">
                    <button class="fullscreen-btn fullscreen-btn-yes" id="fs-btn-yes">YA</button>
                    <button class="fullscreen-btn fullscreen-btn-no" id="fs-btn-no">TIDAK</button>
                </div>
            </div>
        `;
        document.body.appendChild(overlay);

        // Paksa browser melakukan reflow untuk memicu animasi masuk
        overlay.offsetHeight;
        overlay.classList.add('show');

        const btnYes = document.getElementById('fs-btn-yes');
        const btnNo = document.getElementById('fs-btn-no');

        const requestFullscreen = () => {
            const docEl = document.documentElement;
            if (docEl.requestFullscreen) {
                docEl.requestFullscreen();
            } else if (docEl.webkitRequestFullscreen) {
                docEl.webkitRequestFullscreen();
            } else if (docEl.msRequestFullscreen) {
                docEl.msRequestFullscreen();
            }
        };

        const closeModal = () => {
            overlay.classList.remove('show');
            setTimeout(() => {
                overlay.remove();
            }, 300);
        };

        btnYes.addEventListener('click', function () {
            requestFullscreen();
            closeModal();
        });

        btnNo.addEventListener('click', function () {
            closeModal();
        });
    }

})();
