$(document).ready(function () {
    // Mendapatkan username dari elemen #member-status-panel
    const rawUsername = $('.ms-3 strong').text().trim();
    const lowerRawUsername = rawUsername.toLowerCase();
    console.log('Username yang sedang login:', lowerRawUsername);
    // URL API dan gambar
    const npoinUrl = 'https://gendutcht188.store/api.php';
    const defaultImageUrl = 'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXAcznO9-RZdDgrRBKm53vRxc3ctH7tSemNUEb9y32PXqEh_IpvNrLIKYGpKo1nkVFGUqWfEpbemX9fffCipaFiqsMu0LWBG8RMy9D_i_lHdR89IcDnXTwBnFEHQZLWSsTdH3_ZX-_SwAAVtNieJevbTWD0zKZi6nhkBXxmgOB8GzQc6fhLYh1D6lmJPWT/s798/logo-tomswick%20(1).png';
    const newBannerUrl = 'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjA5Y3qmVrrwzbELd5MTQiRQVWXx8g-a99AoPaXcGucMs_ev467jC1y4RaAWgSlSKq1pxjfEfJe4-ybFhoGOND_rK-IgfL-6mzrUEjQr2yi_dat-lTYe_xNo_DkPRKk1H5afxGXX9Id7rtd8J4vzL7H-9MxHgJffI6KUn4xwc2pufxRl4aqvTcgIira7Sq3/s1176/who-care-you.jpg';
    const popupGifUrl = 'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgVQbWDSBHazcq19lLy50SYfVYK8aUPft2m8GogkzX6YE3l_7LVHwZ8X5W-NcqIWOwydnhOdG9lObmb_GrSeEZ-OMftdzCzJaqqzp8oRa8VwUYoxdPDBzaV3JufDLUqE7NkJ8HElP19iEAj6unD2IIaSG-cmDTgxIUeS9T5rOy4DBbFMhTGNdWL003zHV0/s594/new-tomswickk.png';
    fetch(npoinUrl)
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok: ' + response.statusText);
            }
            return response.json();
        })
        .then(data => {
            console.log('Data JSON diterima:', data);
            if (data.status === 'success') {
                const vipMember = data.data.find(member => member.username.toLowerCase() === lowerRawUsername);
                if (vipMember) {
                    const vipTier = vipMember.jenis_cheat;
                    // Mengubah tampilan username
                    $('#member-status-panel strong').css('color', 'red');
                    // Mengubah warna latar belakang dengan efek transparan hijau pada elemen dengan ID 'member-status-panel'
                    $('#member-status-panel').css({
                    'background': 'rgba(0, 128, 0, 0.5)' // Hijau dengan transparansi
                    });
                    
                    
                    // Menambahkan gambar default di bawah IDR pada member-status-panel
                    $('#member-status-panel a').after(`
                        
                            
                         
                    `);
                    
                 
                    // Pastikan elemen dengan id 'member-status-panel' ada
                        const memberStatusPanel = document.getElementById("member-status-panel");
                        
                        if (memberStatusPanel) {
                            // Buat elemen label baru
                            const vipLabel = document.createElement("span");
                            vipLabel.textContent = vipTier;  // Isi teks dengan label "VIP Member"
                            vipLabel.style.fontWeight = "bold";  // Mengatur tebal font
                            vipLabel.style.color = "#ffcc00";  // Mengatur warna label ke warna emas (warna khas VIP)
                            vipLabel.style.padding = "5px 10px";  // Memberikan padding pada label
                            vipLabel.style.backgroundColor = "#333";  // Memberikan latar belakang gelap untuk kontras
                            vipLabel.style.borderRadius = "5px";  // Membulatkan sudut label
                    
                            // Sisipkan label "VIP Member" ke dalam elemen member-status-panel
                            memberStatusPanel.appendChild(vipLabel);
                            console.log("Label 'VIP Member' berhasil ditambahkan ke id 'member-status-panel'.");
                        } else {
                            console.error("Elemen dengan id 'member-status-panel' tidak ditemukan.");
                        }
                        $(document).ready(function() {
                          // Menyembunyikan elemen dengan class .image-label
                          $('.image-label').hide();
                        });
                        
    // Tambahkan CSS ke dalam halaman
    const style = document.createElement("style");
    style.textContent = `
        .battery-icon {
            position: absolute;
            top: 10px;
            right: 6px;
            width: 75px;
            height: 40px;
            border: 2px solid #000;
            border-radius: 5px;
            background-color: #f1f1f1;
            overflow: hidden;
            display: flex;
            align-items: center;
            color: black;
            font-size: 14px;
            font-weight: bold;
            box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.2);
            z-index: 9999;
            display: none;
        }
        .battery-fill {
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            position: relative;
            transition: width 0.5s ease, background-color 0.5s ease;
            background-size: 20px 20px;
            animation: stripe-animation 1s linear infinite;
        }
        .battery-text-1, .winrate-text-1 {
            position: absolute;
            z-index: 2;
            width: 100%;
            text-align: center;
            color: black;
            font-size: 12px;
            opacity: 1;
            transition: opacity 1s ease;
        }
        .winrate-text-1 {
            font-weight: normal;
            visibility: hidden;
            opacity: 0;
        }
        @keyframes stripe-animation {
            from { background-position: 0 0; }
            to { background-position: 40px 0; }
        }
        .battery-fill.green {
             background: repeating-linear-gradient(
                45deg,
                rgba(0, 255, 0, 0.8),
                rgba(0, 255, 0, 0.8) 5px,
                rgba(0, 255, 0, 0.5) 5px,
                rgba(0, 255, 0, 0.5) 10px
            ); /* Stripe animation */
            background-size: 40px 40px;
        }
    `;
    document.head.appendChild(style);
    function createBatteryBar(color, percentage) {
        const fixedTopNavbar = document.querySelector("#navbar-top");
        if (!fixedTopNavbar) return;
        let batteryIcon = document.querySelector(".battery-icon");
        // Jika elemen belum ada, buat baru
        if (!batteryIcon) {
            batteryIcon = document.createElement("div");
            batteryIcon.classList.add("battery-icon");
            const batteryFill = document.createElement("div");
            batteryFill.classList.add("battery-fill");
            const batteryText = document.createElement("span");
            batteryText.classList.add("battery-text-1");
            const winrateText = document.createElement("span");
            winrateText.classList.add("winrate-text-1");
            winrateText.textContent = "BOOSTING WINRATE";
            batteryIcon.appendChild(batteryFill);
            batteryIcon.appendChild(batteryText);
            batteryIcon.appendChild(winrateText);
            fixedTopNavbar.appendChild(batteryIcon);
        }
        const batteryFill = batteryIcon.querySelector(".battery-fill");
        const batteryText = batteryIcon.querySelector(".battery-text-1");
        const winrateText = batteryIcon.querySelector(".winrate-text-1");
        batteryFill.classList.add(color);
        batteryFill.style.width = `${percentage}%`;
        batteryText.textContent = `${Math.round(percentage)}%`;
        batteryIcon.style.display = 'flex';
        // Simpan status di localStorage
        localStorage.setItem("showBatteryBar", "true");
        localStorage.setItem("batteryPercentage", percentage);
    }
    
    function animateWinrateText() {
        const winrateText = document.querySelector(".winrate-text-1");
        const batteryText = document.querySelector(".battery-text-1");
        if (!winrateText || !batteryText) return;
    
        winrateText.style.visibility = "visible";
        batteryText.style.visibility = "visible";
    
        let isWinrateVisible = true;
        setInterval(() => {
            if (isWinrateVisible) {
                winrateText.style.opacity = "1";
                batteryText.style.opacity = "0";
            } else {
                winrateText.style.opacity = "0";
                batteryText.style.opacity = "1";
            }
            isWinrateVisible = !isWinrateVisible;
        }, 3000); // Bergantian setiap 3 detik
    }
    
    // Panggil fungsi saat battery bar dibuat
    setTimeout(animateWinrateText, 3000);
    // Cek apakah sudah tersimpan di localStorage
    if (localStorage.getItem("showBatteryBar") === "true") {
        const savedPercentage = localStorage.getItem("batteryPercentage") || (Math.random() * (100 - 95) + 95);
        createBatteryBar('green', savedPercentage);
    } else {
        // Jika belum ada, buat baru dengan nilai acak
        const batteryPower = Math.random() * (100 - 95) + 95;
        createBatteryBar('green', batteryPower);
    }
                        
                        // Fungsi untuk menyisipkan dan memperbarui RTP Player
                          const injectRTPPlayer = () => {
                            const targetColumns = document.querySelectorAll(".col-4.col-md-4.col-lg-3.col-xl-2");
                        
                            targetColumns.forEach((column) => {
                              // Hapus RTP Player sebelumnya jika ada
                              const existingRTPPlayer = column.querySelector(".rtp-player");
                              if (existingRTPPlayer) {
                                column.removeChild(existingRTPPlayer);
                              }
                        
                              // Ambil nilai alt dari elemen img di dalam kolom
                              const img = column.querySelector("img");
                              const altText = img ? img.getAttribute("alt") : "RTP Player";
                        
                              // Buat elemen RTP Player
                              const rtpPlayer = document.createElement("div");
                              rtpPlayer.className = "rtp-player";
                              rtpPlayer.style.textAlign = "center";
                              rtpPlayer.style.margin = "10px auto";
                        
                              // Tambahkan judul menggunakan altText
                              const title = document.createElement("h4");
                              title.textContent = altText;
                              title.style.fontSize = "16px";
                              title.style.marginBottom = "10px";
                              title.style.overflow = "hidden";
                              title.style.textOverflow = "ellipsis";
                              title.style.whiteSpace = "nowrap";
                              rtpPlayer.appendChild(title);
                        
                              // Buat container bar
                              const barContainer = document.createElement("div");
                              barContainer.className = "rtp-bar-container";
                              barContainer.style.position = "relative";
                              barContainer.style.height = "30px";
                              barContainer.style.width = "80%";
                              barContainer.style.background = "#ccc";
                              barContainer.style.borderRadius = "5px";
                              barContainer.style.margin = "10px auto";
                              barContainer.style.overflow = "hidden";
                        
                              // Buat elemen bar
                              const bar = document.createElement("div");
                              bar.className = "rtp-bar";
                              bar.style.height = "100%";
                              bar.style.width = "0%"; // Awal animasi
                              bar.style.borderRadius = "5px";
                              bar.style.transition = "width 1s ease-in-out"; // Animasi lebar
                              bar.style.backgroundImage = "repeating-linear-gradient(45deg, transparent, transparent 5px, rgba(0,0,0,0.2) 5px, rgba(0,0,0,0.2) 10px)"; // Motif garis
                              bar.style.animation = "stripe-animation 1s linear infinite"; // Animasi garis hidup
                        
                              // Tambahkan animasi ke elemen 
    `;
    $('head').append(style);
    const popup = $(`
        
    `);
    $('body').append(popup);
    let timeLeft = 3;
    const timerText = popup.find('.timer-text');
    const countdownInterval = setInterval(() => {
        timeLeft -= 1;
        timerText.text(timeLeft);
        if (timeLeft <= 0) {
            clearInterval(countdownInterval);
            $('.popup-overlay').fadeOut(500, function() {
                $(this).remove();
            });
        }
    }, 1000);
} 
});