<?php
ob_start();
session_start();

require_once 'core/classes.php';
require_once 'core/classes/user.php';

$uri = "grublox-player://";
$uri .= base64_encode("token");

if(!isset($_GET["id"])) {
  header("Location: ?id=".$_SESSION['user']);
} else {
  $id = (int)$_GET["id"];
}

if(isset($_SESSION['user'])) {
$user = new User($con, $_SESSION['user']);
$loggedIn = true;
} else {
$loggedIn = false; 
}

function getMoney($money) {
		if ($money < 1000000) {
         // Anything less than a million
         return number_format($money);
        } else if ($money < 1000000000) {
         // Anything less than a billion
         return number_format($money / 1000000, 1) . 'M';
        } else {
         // At least a billion
         return number_format($money / 1000000000, 1) . 'B';
        }
}

$q = $con->prepare("SELECT * FROM games WHERE id = :id");
$q->bindParam(':id',$id,PDO::PARAM_INT);
$q->execute();
$item = $q->fetch();
$itemUser = new User($con, $item['creator']);
if(!$item) {
  header('location: /error?err=404');
  exit;
}

?>
<!DOCTYPE html>
<html data-bs-theme="dark">
<head>
<?php
$getitstarted = new PartyStarter;
$getitstarted->header();
?>
</head>
<title><?php echo htmlspecialchars($item["name"]); ?> | <?php echo $sitename; ?></title>
<body>
<main class="container">
<div class="mx-auto w-75">
<br><br>

<div class="card shadow-sm w-100">
  <h3 class="card-header text-start"> <?php echo htmlspecialchars($item["name"]); ?> </h3>
  <div class="card-body mx-md-auto mx-xl-0">
   <div class="row g-2">
    <div class="col-md-6 h-100 align-self-center d-block text-center">
    <img class="img-thumbnail border-0" height=300 width=500 src="
			<?php if (!empty($item["thumbnail"])) {echo htmlspecialchars($item["thumbnail"]);} else {echo "assets/placeholder2.png";} ?>">
	</div>
	
    <div class="col p-4 mt-md-2 ms-md-0 ms-lg-4 mt-lg-0 border-0 rounded-2 align-self-center">
      <a href="user?id=<?php echo $item['creator']; ?>" class="text-decoration-none d-flex">
        <img class="float-start border border-1 rounded-1 me-2" width=64 height=64 alt="<?php echo htmlspecialchars($item["name"]); ?>" src="<?php if (!empty($itemUser->getAvatar())) {echo htmlspecialchars($itemUser->getAvatar()); } ?>">
        <p class="text-white align-self-center fs-5 mt-2"> <?php echo $itemUser->getUsername(); ?> </p>
      </a>
      <br>
      <p>Description: <span class="text-white"> <?php echo htmlspecialchars($item["description"]); ?> </span>
      </p>
      <p>Created: <span class="text-white"> <?php echo $item["date"]; ?> </span>
      </p>
	  <p>Updated: <span class="text-white"> <?php echo $item["updateddate"]; ?> </span>
      </p>
      <p>Players: <span class="text-white"> <?php echo 0; ?> </span>
      </p>
      <button class="btn btn-success fs-4 w-100 <?php if($loggedIn == false) { echo "disabled"; } ?>" onclick="window.location = '<?php echo $uri; ?>';" data-bs-toggle="modal" data-bs-target="#loadBootstrapper"><i class="bi bi-play-fill fs-2"></i>
      </button>
	 </div>
    </div>
  </div>
</div>

<div class="clearfix my-4"></div>

<div class="card"> 
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs nav-fill">
    <li class="nav-item"> <button class="nav-link active" id="comments-tab" data-bs-toggle="pill" data-bs-target="#comments" type="button" role="tab" aria-controls="comments" aria-selected="true">Comments</button> </li>
    <li class="nav-item"> <button class="nav-link" id="trades-tab" data-bs-toggle="pill" data-bs-target="#trades" type="button" role="tab" aria-controls="trades" aria-selected="false">Gamepasses</button> </li>
    <li class="nav-item"> <button class="nav-link" id="servers-tab" data-bs-toggle="pill" data-bs-target="#servers" type="button" role="tab" aria-controls="servers" aria-selected="false">Servers</button> </li>
</ul>
</div>
 <div class="card-body text-center">
  <div class="tab-content" id="v-pills-tabContent">
    <div class="tab-pane fade show active" id="comments" role="tabpanel" aria-labelledby="comments-tab" tabindex="0">soon commentz</div>
    <div class="tab-pane fade" id="trades" role="tabpanel" aria-labelledby="trades-tab" tabindex="0">gampeas</div>
	<div class="tab-pane fade" id="servers" role="tabpanel" aria-labelledby="servers-tab" tabindex="0">servdrsdad</div>
  </div>
 </div>
</div>
</div>



</div>
<?php if($loggedIn == true) { 
echo '
<div class="modal fade" id="loadBootstrapper" tabindex="1" aria-labelledby="loadBootstrapperLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content" style="transform: translateY(20vh);">
      <button type="button" class="btn-close p-2" data-bs-dismiss="modal" aria-label="Close"></button>
      <div class="modal-body text-center mb-5">
	   <div class="p-4"> 
	   <h1 class="grufont loading-bootstrapper" style="font-size: 8rem;">G</h1>
	   </div>
       Getting ready to launch <span class="gotham">'.strtoupper($sitename).'</span>
      </div>
    </div>
  </div>
</div>
'; 
}
?>


</main>
</body>
</html>
