<?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 foreach ($games as $game): ?>
            

            <?php
                if ($user == '') {
                    $link_url = 'index.php?pesan=28';
                } else {
                    $link_url = "?page=playgame"
                              . "&provider=" . strtoupper($id_provider)
                              . "&game_code=" . $game['game_code']
                              . "&p=" . $extplayer
                              . "&ln=id";
                }
            ?>
            
            
            <div class="sgx-game-card" data-name="<?= strtolower($game['game_name']); ?>">
            
                <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']; ?>" alt="<?= htmlspecialchars($game['game_name']); ?>">
                </div>
            
                <div class="sgx-game-provider">
                    <?= strtoupper($game['game_name']); ?>
                </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; ?>


