<?php
require_once __DIR__ . '/../../../../../function/connect.php';
require_once __DIR__ . '/../../../../../play/game/functions.php';
require_once __DIR__ . '/../../../../../play/game/config.php';

if (!isset($koneksi)) {
    exit('DB NOT CONNECTED');
}

/***********************
 * USER
 ***********************/
$user      = $_SESSION['username'] ?? '';
$extplayer = '';

if ($user != '') {
    $safeUser = mysqli_real_escape_string($koneksi, $user);
    $getUser  = mysqli_query($koneksi, "SELECT extplayer FROM tb_user WHERE username='$safeUser'");
    $infouser = mysqli_fetch_assoc($getUser);
    $extplayer = $infouser['extplayer'] ?? '';
}

/***********************
 * GET GAME FROM API
 ***********************/
$response = nexusGetGameList($id_provider);

$games = [];
if (
    isset($response['status']) &&
    $response['status'] == 1 &&
    !empty($response['games']) &&
    is_array($response['games'])
) {
    $games = $response['games'];
}


function getActivePlayers($gameName, $playersSetting) {
    $name = strtolower($gameName);

    foreach ($playersSetting as $key => $range) {
        if (strpos($name, $key) !== false) {
            return rand($range[0], $range[1]);
        }
    }

    // default game sepi / bisa kosong
    return rand(0, 20);
}


usort($games, function ($a, $b) use ($priorityGames) {

    $nameA = strtolower($a['game_name']);
    $nameB = strtolower($b['game_name']);

    $posA = $posB = 999;

    foreach ($priorityGames as $index => $keyword) {
        if ($posA === 999 && strpos($nameA, $keyword) !== false) {
            $posA = $index;
        }
        if ($posB === 999 && strpos($nameB, $keyword) !== false) {
            $posB = $index;
        }
    }

    // Game prioritas selalu naik
    if ($posA != $posB) {
        return $posA - $posB;
    }

    return 0;
});
?>
<!-- ================= GAME LIST ================= -->
<?php
// Fungsi untuk generate RTP acak berdasarkan interval waktu 2 menit dan game_code
function generateRandomRtp($gameCode) {
    // Ambil timestamp saat ini, bulatkan ke bawah ke interval 120 detik (2 menit)
    $interval = floor(time() / 120);
    
    // Gunakan interval + hash game_code sebagai seed untuk variasi per game
    $seed = $interval + crc32($gameCode);
    mt_srand($seed);
    
    // Generate RTP acak 60.0 - 98.0
    $rtp = mt_rand(600, 980) / 10;
    
    // Reset mt_rand agar tidak mempengaruhi random lain
    mt_srand();
    return $rtp;
}
?>

<?php foreach ($games as $game): ?>
    
    <?php
        if ($user == '') {
            $link_url = 'index.php?pesan=28';
        } else {
            $link_url = "../play/game/playid.php"
                      . "?provider=" . strtoupper($id_provider)
                      . "&game_code=" . $game['game_code']
                      . "&p=" . $extplayer
                      . "&ln=id";
        }

        // Generate RTP untuk game ini (acak per game, konsisten per interval waktu)
        $rtpValue = generateRandomRtp($game['game_code']);
        $rtpWidth = $rtpValue . '%';
        $rtpColor = ($rtpValue >= 85) ? '#00ff88' : (($rtpValue >= 70) ? '#ffcc00' : '#ff4444');
    ?>

           <div class="sgx-game-card" data-game="<?= $game['game_code']; ?>">
                    
            <div class="sgx-player-badge">
                    <i class="fa fa-user"></i><?= getActivePlayers($game['game_name'], $playersSetting); ?>
                </div>

            <div class="sgx-game-thumb">
                <img src="<?= $game['banner']; ?>">
            </div>
        
            <div class="sgx-rtp-badge <?= 
                ($rtpValue >= 85 ? 'high' : ($rtpValue >= 70 ? 'med' : 'low')); 
            ?>">
                <span class="rtp-status">
                    <?= ($rtpValue >= 85 ? '🔥 HIGH' : ($rtpValue >= 70 ? '⚡ MED' : '❌ LOW')); ?>
                </span>
                <span class="rtp-value"><?= $rtpValue; ?>%</span>
                <span class="rtp-text">RTP</span>
            </div>

                <a href="<?= $link_url; ?>" class="sgx-game-hover">
                    <div class="game-thumb"></div>
                    <div class="game-hover-overlay">
                        <div class="play-content">
                            <i class="fa fa-play"></i>
                        </div>
                    </div>
                </a>
            
            </div>
            
<?php endforeach; ?>


<style>
.sgx-rtp-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px;
    border-radius: 0px;
    font-size: 10px;
    font-weight: 700;
    margin: 6px auto 0;
    width: fit-content;
    box-shadow: 0 4px 10px rgba(0, 0, 0, .35);
}

/* STATUS WARNA */
.sgx-rtp-badge.high {
    background: linear-gradient(135deg, #2ecc71, #27ae60);
    color: #fff;
}

.sgx-rtp-badge.med {
    background: linear-gradient(135deg, #f1c40f, #e67e22);
    color: #000;
}

.sgx-rtp-badge.low {
    background: linear-gradient(135deg, #e74c3c, #c0392b);
    color: #fff;
}

.sgx-rtp-badge .rtp-text {
    opacity: .85;
    font-weight: 600;
}
</style>