<?php
require_once('session.php');
include('../config/class_softgaming.php');

$useridnya = $u['username'];
$gameID = isset($_GET['gamecode']) ? trim($_GET['gamecode']) : '';
$gameProvider = isset($_GET['providercode']) ? trim($_GET['providercode']) : '';
$lang = isset($_GET['lang']) ? trim($_GET['lang']) : 'en';

// Debug info untuk ditampilkan jika error
$debugInfo = [
    'username' => $useridnya,
    'game_code' => $gameID,
    'provider_code' => $gameProvider,
    'lang' => $lang,
    'timestamp' => date('Y-m-d H:i:s')
];

$WL = new fiver();
$response = $WL->opengame($useridnya, $gameID, $gameProvider, $lang);

// Cek jika status = 1 (SUCCESS) dan ada launch_url
if (isset($response['status']) && $response['status'] == 1) {
    // Prioritas: launch_url > game_url > url
    $gameUrl = null;
    
    if (isset($response['launch_url']) && !empty($response['launch_url'])) {
        $gameUrl = $response['launch_url'];
    } elseif (isset($response['game_url']) && !empty($response['game_url'])) {
        $gameUrl = $response['game_url'];
    } elseif (isset($response['url']) && !empty($response['url'])) {
        $gameUrl = $response['url'];
    }
    
    // Jika ada URL, langsung redirect
    if ($gameUrl) {
        // Pastikan URL valid
        if (filter_var($gameUrl, FILTER_VALIDATE_URL)) {
            header("Location: " . $gameUrl);
            exit();
        } else {
            // Jika URL tidak valid, tampilkan error
            $errorMessage = "URL game tidak valid: " . htmlspecialchars($gameUrl);
        }
    } else {
        // Status SUCCESS tapi tidak ada URL
        $errorMessage = "Status SUCCESS tapi tidak ada launch_url, game_url, atau url dalam response.";
    }
}

// Ambil pesan error dari API response
$errorMessage = 'Game sedang dalam perbaikan atau tidak ditemukan. Silakan coba lagi nanti.';

if (isset($response['msg']) && !empty($response['msg'])) {
    $errorMessage = $response['msg'];
} elseif (isset($response['error']) && !empty($response['error'])) {
    $errorMessage = $response['error'];
} elseif (isset($response['message']) && !empty($response['message'])) {
    $errorMessage = $response['message'];
}

// Format seluruh response API untuk ditampilkan
$fullResponse = json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$fullResponse = htmlspecialchars($fullResponse, ENT_QUOTES, 'UTF-8');

// Escape untuk JavaScript
$errorMessage = addslashes($errorMessage);
$fullResponseJs = addslashes($fullResponse);

// Tampilkan pesan error dan seluruh response API
echo "<!DOCTYPE html>
<html>
<head>
    <meta charset='UTF-8'>
    <title>Error Launch Game</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .error-container {
            max-width: 800px;
            margin: 50px auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .error-message {
            background: #fee;
            border-left: 4px solid #f00;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 4px;
        }
        .response-container {
            background: #f9f9f9;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 15px;
            margin-bottom: 20px;
        }
        .response-title {
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }
        pre {
            background: #2d2d2d;
            color: #f8f8f2;
            padding: 15px;
            border-radius: 4px;
            overflow-x: auto;
            font-size: 12px;
            line-height: 1.5;
        }
        .btn-back {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .btn-back:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>
    <div class='error-container'>
        <div class='error-message'>
            <strong>Error:</strong> " . htmlspecialchars($errorMessage, ENT_QUOTES, 'UTF-8') . "
        </div>
        <div class='response-container'>
            <div class='response-title'>Request Info:</div>
            <pre style='background: #e8f4f8; color: #333; margin-bottom: 15px;'>" . htmlspecialchars(json_encode($debugInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), ENT_QUOTES, 'UTF-8') . "</pre>
            <div class='response-title'>Seluruh Response API:</div>
            <pre>" . $fullResponse . "</pre>
        </div>
        <button class='btn-back' onclick='window.history.back()'>Kembali</button>
    </div>
</body>
</html>";
?>