<?php
ob_start();
session_start();

require_once 'core/classes.php';
require_once 'core/classes/user.php';
$user = new User($con, $_SESSION['user'] ?? 0);
if(!$user->isLoggedIn()) {
  header('location: /login');
  exit;
}
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<?php

$himsgs = [
    "Hello",
    "What's up",
    "How you doing",
    "Ayo",
    "Hola",
    "Welcome",
    "Welcome back",
    "Hi",
    "Hey",
    "Heyyo",
    "Howdy",
    "Hi there",
    "Ahoy",
	"Ay",
	"Sup",
	"Wassup"
];
$himsg = array_rand($himsgs);
$himsg = $himsgs[$himsg];

$getitstarted = new PartyStarter;
$getitstarted->header();
?>
</head>
<title><?php echo $pagename; ?> | <?php echo $sitename; ?></title>
<body>
 <main class="container text-white" style="width: 100%;">
  <br><br>
  <img src="assets/renders/user/headshot?userId=<?php echo (int)htmlspecialchars($user->getID($con, $user->getUsername())); ?>" class="rounded-circle" width=190 height=190></img>
  <span class="text-center" style="font-size: 2rem; margin-left: 6px;"><?php echo $himsg; ?>, <?php echo htmlspecialchars($user->getUsername()); ?>!</span>
  <div style="margin-bottom: 50px;"></div>
   <div class="d-flex flex-row justify-content-between mb-3">
    <h4 class=" card-title">Friends (<?php $q=$con->prepare("SELECT count(*) FROM users");$q->execute(); $numberOfFriends = $q->fetchColumn(); echo $numberOfFriends; ?>)</h4>
	 <a href="/friends" class="btn btn-link text-decoration-none align-self-end">See all <i class="bi bi-caret-right"></i></a>
   </div>
  <div class="card  shadow-sm" style="height: 11.75rem;">
    <div class="card-group" style=" margin-left: 10px; margin-top: 10px;">
    <?php
    if ($numberOfFriends >= 1) {
      foreach ($user->getFriends() as $friend) { 
	  $frienduser = new User($con, $friend['friend']);
	  ?>
     <div class="card text-white border border-0" style="max-width: 130px;">
	 <a href="user?id=<?php echo (int)htmlspecialchars($friend['friend']); ?>" style="text-decoration: none; text-align: center; margin-bottom: 9px;">
	  <div class="card-body">
      <img src="assets/renders/user/headshot?userId=<?php echo (int)htmlspecialchars($friend['friend']); ?>" class="rounded-circle" width=100 height=100 alt="<?php echo htmlspecialchars($frienduser->getUsername()); ?>"></img>
	  </div>
	  <small class="text-center text-white"><?php echo htmlspecialchars($frienduser->getUsername()); ?></small>
	  </a>
     </div>
	 <?php }
	 } else {
      echo "u have no friend wtf noob";
     }		 ?>

	
	</div>
  </div>
  
  <br>
  <div class="d-flex flex-row justify-content-between mb-3">
    <h4 class="card-title">Recently played</h4> 
	<a href="/played" class="btn btn-link text-decoration-none align-self-end">See all <i class="bi bi-caret-right"></i></a>
	</div>
<div class="row row-cols-2 row-cols-sm-3 row-cols-lg-4 row-cols-xl-6 g-4">
    <?php
    $sql = "SELECT allStar FROM games ORDER BY date LIMIT 6 ";
    $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="col">
            <div class="card bg-dark border-start rounded shadow-sm" style="min-width: 140px; max-width: 220px; ">
              <a href="place?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>
		  </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> You haven't played any games, why not <a class='text-decoration-none text-muted' href='/games'>play</a> one.</h4>";} ?>
 </main>
</body>
</html>
