<?php
session_start();
$conn = new mysqli("localhost","root","","breakout_db");
if($conn->connect_error){ die("DB Error"); }

$isLoggedIn = isset($_SESSION['user']);
$username = $isLoggedIn ? htmlspecialchars($_SESSION['user']) : null;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Breakout Game</title>

    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    
    <link rel="stylesheet" href="style.css">

</head>
<body>

    <h1><i class="fas fa-gamepad"></i> Breakout Game</h1>

    <div class="menu">
        <?php if($isLoggedIn): ?>
            <span class="user-info">Hello, <b><?= $username ?></b> 👋</span>
            <a href="leaderboard.php"><i class="fas fa-trophy"></i> Leaderboard</a>
            <a href="logout.php"><i class="fas fa-right-from-bracket"></i> Logout</a>
        <?php else: ?>
            <a href="login.php"><i class="fas fa-sign-in-alt"></i> Login</a>
            <a href="leaderboard.php"><i class="fas fa-trophy"></i> Leaderboard</a>
        <?php endif; ?>
    </div>

    <div class="game-container">
        <canvas id="game" width="480" height="320"></canvas>

        <p>Score: <span id="score">0</span></p>

        <div class="controls">
            <select id="diff">
                <option value="easy">Easy</option>
                <option value="medium">Medium</option>
                <option value="hard">Hard</option>
            </select>

            <button onclick="startGame()">Start Game</button>
        </div>
    </div>
    
    <div id="gameModal" class="game-modal">
        <div class="modal-content">
            <h2 id="modalTitle"></h2>
            <p id="modalScore"></p>
            <div class="modal-buttons">
                <?php if($isLoggedIn): ?>
                    <button id="saveScoreBtn">Save Score</button>
                <?php endif; ?>
                <button id="playAgainBtn">Play Again</button>
            </div>
            <a href="leaderboard.php" class="leaderboard-link"><i class="fas fa-trophy"></i> Leaderboard</a>
            <a href="<?php echo $isLoggedIn ? 'index.php' : 'login.php'; ?>" class="back-link"><i class="fas fa-home"></i> Home</a>
        </div>
    </div>
    <script src="script.js"></script>

</body>
</html>