const token = localStorage.getItem('token');

function md5(str) {
    function rotateLeft(n, s) { return (n << s) | (n >>> (32 - s)); }
    function toHex(n) { let s = '', v; for (let i = 0; i < 4; i++) { v = (n >>> (i * 8)) & 255; s += ('0' + v.toString(16)).slice(-2); } return s; }
    function utf8Encode(str) { return unescape(encodeURIComponent(str)); }
    str = utf8Encode(str);
    const msgLen = str.length;
    const numBlocks = (((msgLen + 8) >>> 6) + 1) * 16;
    const M = new Array(numBlocks).fill(0);
    for (let i = 0; i < msgLen; i++) M[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
    M[msgLen >> 2] |= 0x80 << ((msgLen % 4) * 8);
    M[numBlocks - 2] = msgLen * 8;
    let a = 1732584193, b = -271733879, c = -1732584194, d = 271733878;
    const S = [7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21];
    const K = [];
    for (let i = 0; i < 64; i++) K[i] = (Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296)) | 0;
    for (let i = 0; i < numBlocks; i += 16) {
        let A = a, B = b, C = c, D = d;
        for (let j = 0; j < 64; j++) {
            let F, g;
            if (j < 16) { F = (B & C) | (~B & D); g = j; }
            else if (j < 32) { F = (D & B) | (~D & C); g = (5 * j + 1) % 16; }
            else if (j < 48) { F = B ^ C ^ D; g = (3 * j + 5) % 16; }
            else { F = C ^ (B | ~D); g = (7 * j) % 16; }
            F = (F + A + K[j] + (M[i + g] | 0)) | 0;
            A = D; D = C; C = B;
            B = (B + rotateLeft(F, S[j])) | 0;
        }
        a = (a + A) | 0; b = (b + B) | 0; c = (c + C) | 0; d = (d + D) | 0;
    }
    return toHex(a) + toHex(b) + toHex(c) + toHex(d);
}

async function loadBalance() {
    // ✅ নতুন যোগ — Login না থাকলে, Login/Register Button দেখানো,
    // Balance-Badge লুকানো
    if (!token) {
        document.getElementById('loggedOutButtons').classList.remove('hidden');
        document.getElementById('loggedOutButtons').classList.add('flex');
        document.getElementById('loggedInBadge').classList.add('hidden');
        return;
    }
    document.getElementById('loggedInBadge').classList.remove('hidden');
    document.getElementById('loggedInBadge').classList.add('flex');

    const random = Math.random().toString(36).substring(2, 15);
    const shonustr = `{"language":1,"random":"${random}"}`;
    const signature = md5(shonustr).toUpperCase();
    try {
        const res = await fetch('/deepanshu/api/webapi/GetBalance.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ language: 1, random, signature, timestamp: Date.now() })
        });
        const data = await res.json();
        if (data.code === 0 && data.data) {
            document.getElementById('balanceBadge').textContent = '৳' + Number(data.data.amount).toLocaleString('en-IN', {maximumFractionDigits: 0});
        }
    } catch (e) {
        document.getElementById('balanceBadge').textContent = '৳ --';
    }
}

let jackpotValue = 8345621;
function renderJackpot() {
    const el = document.getElementById('jackpotDigits');
    const str = Math.floor(jackpotValue).toLocaleString('en-US').replace(/,/g, '');
    el.innerHTML = str.split('').map(d => `<span class="digit-box w-7 h-9 flex items-center justify-center rounded text-lg font-black">${d}</span>`).join('');
}
renderJackpot();
setInterval(() => { jackpotValue += Math.floor(Math.random() * 50) + 5; renderJackpot(); }, 2000);

// ✅ Real Banner Slider
let bannerImages = [];
let bannerIndex = 0;
async function loadBanners() {
    try {
        const res = await fetch('/deepanshu/api/webapi/GetBannerList.php');
        const data = await res.json();
        bannerImages = (data.data || []).map(b => b.bannerUrl).filter(Boolean);
        const container = document.getElementById('bannerContainer');
        const dotsContainer = document.getElementById('bannerDots');
        if (bannerImages.length === 0) { container.innerHTML = ''; return; }
        container.innerHTML = bannerImages.map((url, i) => `<img src="${url}" class="banner-img absolute inset-0 w-full h-full object-cover transition-opacity duration-500" style="opacity: ${i === 0 ? 1 : 0};">`).join('');
        dotsContainer.innerHTML = bannerImages.map((_, i) => `<span class="banner-dot w-2 h-2 rounded-full ${i === 0 ? 'gold-gradient' : 'bg-gray-600'}"></span>`).join('');
        if (bannerImages.length > 1) {
            setInterval(() => {
                bannerIndex = (bannerIndex + 1) % bannerImages.length;
                document.querySelectorAll('.banner-img').forEach((img, i) => img.style.opacity = i === bannerIndex ? '1' : '0');
                document.querySelectorAll('.banner-dot').forEach((dot, i) => dot.className = 'banner-dot w-2 h-2 rounded-full ' + (i === bannerIndex ? 'gold-gradient' : 'bg-gray-600'));
            }, 3500);
        }
    } catch (e) {}
}

