// ✅ Game-Click-Logic — আসল launchGame Function, বদলানো হয়নি,
// শুধু Recent-History-তে Save করার একটা ছোট Wrapper যোগ করা হয়েছে
function handleGameClick(code, name, img, vendor) {
    addToRecent(code, name, img, vendor);
    launchGame(code, name);
}

// ✅ এটাই আসল Game-Launch Logic — শুধু "Premium Loading" UX যোগ
// হয়েছে (Backend/API-Call অপরিবর্তিত)
async function launchGame(gameCode, gameName) {
    if (!gameCode) { alert('এই Game-এর Code পাওয়া যায়নি।'); return; }
    if (!token) { window.location.href = '/newlogin.php'; return; }

    // ✅ নতুন যোগ — Click-Gesture থাকা অবস্থাতেই নতুন Tab খোলা
    // (Popup-Blocker এড়াতে), সাথে সাথেই Premium Loading-Screen
    const gameWindow = window.open('about:blank', '_blank');
    if (gameWindow) {
        gameWindow.document.write(`
            <!DOCTYPE html><html><head><meta charset="UTF-8"><title>CV111 - Loading</title>
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover">
            <style>
                *{margin:0;padding:0;box-sizing:border-box;}
                html,body{width:100%;height:100%;margin:0;padding:0;touch-action:pan-x pan-y;}
                body{min-height:100vh;min-width:100vw;background:radial-gradient(circle at 15% 0%, rgba(16,185,129,.10), transparent 40%),
                    radial-gradient(circle at 85% 15%, rgba(251,191,36,.08), transparent 45%), linear-gradient(180deg, #01201d 0%, #011412 60%, #010c0b 100%);
                    display:flex;align-items:center;justify-content:center;flex-direction:column;
                    font-family:'Hind Siliguri',sans-serif;overflow:hidden;position:relative;}
                .brand{
                    font-size:24px;font-weight:900;font-style:italic;letter-spacing:1.5px;margin-bottom:36px;
                    background:linear-gradient(90deg,#a5f3fc 0%,#22d3ee 22%,#f0fdff 42%,#2dd4bf 62%,#0e7490 85%,#22d3ee 100%);
                    background-size:250% auto; -webkit-background-clip:text; background-clip:text;
                    -webkit-text-fill-color:transparent; color:transparent;
                    filter:drop-shadow(0 1px 1px rgba(0,0,0,.5)) drop-shadow(0 0 6px rgba(34,211,238,.4));
                    animation:brandShimmer 4s linear infinite;position:relative;z-index:1;
                }
                @keyframes brandShimmer{0%{background-position:0% center;}100%{background-position:-250% center;}}
                .spin-box{width:76px;height:76px;display:flex;align-items:center;justify-content:center;
                    margin-bottom:22px;position:relative;z-index:1;}
                .spin-box img{width:100%;height:100%;object-fit:contain;animation:spin 1s linear infinite;}
                @keyframes spin{to{transform:rotate(360deg);}}
                .txt{color:#fff;font-weight:800;font-size:17px;margin-bottom:8px;position:relative;z-index:1;}
                .sub{color:#9fb8b5;font-size:13.5px;position:relative;z-index:1;margin-bottom:26px;}
                .bar-wrap{width:170px;height:5px;border-radius:10px;background:rgba(255,255,255,.08);overflow:hidden;position:relative;z-index:1;}
                .bar-fill{position:absolute;top:0;left:-40%;height:100%;width:40%;border-radius:10px;
                    background:linear-gradient(90deg,#0e7490,#22d3ee);animation:slide 1.1s ease-in-out infinite;}
                @keyframes slide{0%{left:-40%;}100%{left:100%;}}
            </style></head>
            <body>
                <div class="brand">CV111</div>
                <div class="spin-box"><img src="/spin.png" alt="Loading" onerror="this.outerHTML='<span style=font-size:30px>🎰</span>';"></div>
                <div class="txt">${gameName || 'Game'} খোলা হচ্ছে...</div>
                <div class="sub">একটু অপেক্ষা করো, প্রায় হয়ে এসেছে</div>
                <div class="bar-wrap"><div class="bar-fill"></div></div>
                <script>
                    document.addEventListener('gesturestart', function(e){ e.preventDefault(); });
                    document.addEventListener('touchmove', function(e){ if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
                    let __lte = 0;
                    document.addEventListener('touchend', function(e){
                        const now = Date.now();
                        if (now - __lte <= 300) e.preventDefault();
                        __lte = now;
                    }, { passive: false });
                </script>
            </body></html>
        `);
        gameWindow.document.close();
    }

    try {
        const res = await fetch('/deepanshu/api/webapi/GetGameUrl.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ gameCode: gameCode })
        });
        const data = await res.json();
        if (data.code === 0 && data.data && data.data.url) {
            if (gameWindow) gameWindow.location.href = data.data.url;
            else window.location.href = data.data.url;
        } else if (data.code === 4) {
            if (gameWindow) gameWindow.close();
            localStorage.removeItem('token');
            window.location.href = '/newlogin.php';
        } else {
            if (gameWindow) gameWindow.close();
            alert('Game চালু করা যায়নি: ' + (data.msg || 'অজানা সমস্যা'));
        }
    } catch (e) {
        if (gameWindow) gameWindow.close();
        alert('একটা সমস্যা হয়েছে, আবার চেষ্টা করো।');
    }
}
