<?php
session_start();
?>
<!DOCTYPE html>
<html data-bs-theme="dark">
<head>
<?php

require_once 'core/classes.php';
require_once 'core/classes/user.php';
if(isset($_SESSION['user'])) {
	$user = new User($con, $_SESSION['user']);
} else {
	echo ''; // fuck you
}
$getitstarted = new PartyStarter;
$getitstarted->header();

/* !!! IMPORTANT !!!
To whoever reading this, if your going to make the games page functional, please use array slice https://www.php.net/manual/en/function.array-slice.php,
since there can only be 8 in one carousel, make sure to make the offset work.
Signing off, qzip.
*/

?>
</head>
<title><?php echo $pagename; ?> | <?php echo $sitename; ?></title>
<body>
  <main class="container" style="width: 100%;  margin-left: 20px">
    <br>
    <br>
	<div class="d-flex flex-row justify-content-between mb-3">
     <h4 class="text-white card-title">Popular</h4>
	 <a href="/all_games?filter=popular" class="btn btn-link text-decoration-none align-self-end">See all <i class="bi bi-caret-right"></i></a>
   </div>
    <div id="games-popular" class="carousel carousel-dark slide">
      <div class="carousel-inner">
        <div class="carousel-item active">
		<div class="card-group" style=" margin-top: 15px; width: 100%;">
    <?php
    $sql = "SELECT allStar FROM games";
    $q = $con->prepare(str_replace('allStar', 'count(*)', $sql));
    $q->execute();
    $numberOfGames = $q->fetchColumn();
    $q = $con->prepare(str_replace('allStar', '*', $sql));
    $q->execute();
    if ($numberOfGames >= 1) {
      while ($game = $q->fetch()) {
        $playingCount = 69;
        ?>
            <div class="card bg-dark border-start rounded shadow-sm" style="max-width: 170px;  margin-right: 9px;">
              <a href="game?id=<?php echo (int)htmlspecialchars($game["id"]); ?>" style="text-decoration: none; text-align: left; margin-bottom: 9px;">
                <img src="<?php if (!empty($game["thumbnail"])) {echo htmlspecialchars($game["thumbnail"]);} else {echo "assets/placeholder.png";} ?>" class="card-img-top rounded-top" alt="<?php echo htmlspecialchars($game["name"]); ?>">
                <div class="card-body">
                  <small class="card-title text-white" style="font-size: 1rem;"><?php echo htmlspecialchars($game["name"]); ?></small>
                  <br>
                  <small class="card-text text-muted"><?php echo (int)$playingCount; ?> playing</small>
                  <div class="progress rounded-4 border border-secondary" style="margin-left: 20px; margin-top: 10px; margin-bottom: -10px; height: 8px; background: none;">
                    <div class="progress-bar bg-secondary" role="progressbar" aria-label="Segment one" style="width: 21.5%" aria-valuenow="15" aria-valuemin="0" aria-valuemax="100"></div>
                  </div>
                  <i class="bi bi-hand-thumbs-up-fill text-secondary" style="font-size: 1rem; float: left; margin-top: -8px;"></i>
                </div>
              </a>
            </div>
		<?php
      }
	  echo "</div>";
    } else {
      echo "</div> <h4 class='text-muted text-center'><i class='bi bi-egg-fried' style='font-size: 6rem;'></i><br> Looks like there are no games, sad.</h4>";} ?>
        </div>
        <div class="carousel-item"><div class="card-group" style=" margin-top: 15px; width: 100%;">
		  </div></div>
        <div class="carousel-item"></div>
      </div>
      <button class="carousel-control-prev my-auto" type="button" data-bs-target="#games-popular" data-bs-slide="prev" style="width: 35px; height: 10%;">
        <span class="bi bi-caret-left-fill" aria-hidden="true" data-bs-theme="dark" style="margin-left: 10px; font-size: 2.5rem"></span>
      </button>
      <button class="carousel-control-next my-auto" type="button" data-bs-target="#games-popular" data-bs-slide="next" style="width: 35px; height: 10%;">
        <span class="bi bi-caret-right-fill" aria-hidden="true" data-bs-theme="dark" style="margin-right: 30px; font-size: 2.5rem"></span>
      </button>
    </div>
	
    <br>
	<div class="d-flex flex-row justify-content-between mb-3">
     <h4 class="text-white card-title">Most played</h4>
	 <a href="/all_games?filter=mostplayed" class="btn btn-link text-decoration-none align-self-end">See all <i class="bi bi-caret-right"></i></a>
   </div>
    <div id="games-most-played" class="carousel carousel-dark slide">
      <div class="carousel-inner">
        <div class="carousel-item active">
		<div class="card-group" style=" margin-top: 15px; width: 100%;">
	<?php
    $sql = "SELECT allStar FROM games";
    $q = $con->prepare(str_replace('allStar', 'count(*)', $sql));
    $q->execute();
    $numberOfGames = $q->fetchColumn();
    $q = $con->prepare(str_replace('allStar', '*', $sql));
    $q->execute();
    if ($numberOfGames >= 1) {
      while ($game = $q->fetch()) {
        $playingCount = 69;
        ?>
            <div class="card bg-dark border-start rounded shadow-sm" style="max-width: 170px;  margin-right: 9px;">
              <a href="game?id=<?php echo (int)htmlspecialchars($game["id"]); ?>" style="text-decoration: none; text-align: left; margin-bottom: 9px;">
                <img src="<?php if (!empty($game["thumbnail"])) {echo htmlspecialchars($game["thumbnail"]);} else {echo "assets/placeholder.png";} ?>" class="card-img-top rounded-top" alt="<?php echo htmlspecialchars($game["name"]); ?>">
                <div class="card-body">
                  <small class="card-title text-white" style="font-size: 1rem;"><?php echo htmlspecialchars($game["name"]); ?></small>
                  <br>
                  <small class="card-text text-muted"><?php echo (int)$playingCount; ?> playing</small>
                  <div class="progress rounded-4 border border-secondary" style="margin-left: 20px; margin-top: 10px; margin-bottom: -10px; height: 8px; background: none;">
                    <div class="progress-bar bg-secondary" role="progressbar" aria-label="Segment one" style="width: 21.5%" aria-valuenow="15" aria-valuemin="0" aria-valuemax="100"></div>
                  </div>
                  <i class="bi bi-hand-thumbs-up-fill text-secondary" style="font-size: 1rem; float: left; margin-top: -8px;"></i>
                </div>
              </a>
            </div>
		<?php
      }
	  echo "</div>";
    } else {
      echo "</div> <h4 class='text-muted text-center'><i class='bi bi-egg-fried' style='font-size: 6rem;'></i><br> Looks like there are no games, sad.</h4>";} ?>
        </div>
        <div class="carousel-item"><div class="card-group" style=" margin-top: 15px; width: 100%;">
		  </div></div>
        <div class="carousel-item"></div>
      </div>
      <button class="carousel-control-prev my-auto" type="button" data-bs-target="#games-most-played" data-bs-slide="prev" style="width: 35px; height: 10%;">
        <span class="bi bi-caret-left-fill" aria-hidden="true" data-bs-theme="dark" style="margin-left: 10px; font-size: 2.5rem"></span>
      </button>
      <button class="carousel-control-next my-auto" type="button" data-bs-target="#games-most-played" data-bs-slide="next" style="width: 35px; height: 10%;">
        <span class="bi bi-caret-right-fill" aria-hidden="true" data-bs-theme="dark" style="margin-right: 30px; font-size: 2.5rem"></span>
      </button>
    </div>
  </main>
</body>
</html>
