<?php

/**
 * Selects a hero image filename based on a weighted random algorithm.
 *
 * The function gives a higher probability to more recent images by using a power law
 * distribution with an adjustable baseline. Returns an image filename
 * (from `assets/img/hero/`) to be used as the hero image.
 *
 * @return string Image filename to be used as the hero image.
 */
function getHero()
{
  // 01.jpg is not most recent and most probable.
  // 14.jpg is the oldest baby picture, may never show up.
  $nImg = 14;
  // Increase the baseline to give older photos higher probability
  $baseLine = 5;
  // $power=1 => linear, increase it to change the dist to give more probability
  // to more recent photos
  $power = 3;
  $ranges = [0];
  for ($i = $nImg; $i > 0; $i--) {
    $wt = round($i ** $power) + $baseLine;
    $iImg = $nImg - $i + 1;
    $ranges[$iImg] = $ranges[$iImg - 1] + $wt;
  }
  // Generate a random number between 1 and $ranges[$nImg]
  $randomNumber = mt_rand(1, $ranges[$nImg]);
  // Do a range-lookup (similar to Excel's XLOOKUP())
  $imgIdx = getRangeIndex($randomNumber, $ranges);
  // Format the number to always be 2 digits (e.g., 01, 02, ..., 99)
  $formattedNumber = str_pad($imgIdx, 2, '0', STR_PAD_LEFT);
  // Create the filename
  $heroImg = "assets/img/hero/$formattedNumber.jpg";
  return $heroImg;
}

/**
 * Helper function to find the range index that corresponds to the given random number.
 *
 * @param int $randomNumber Random number generated.
 * @param array $ranges Array of cumulative weights.
 * @return int Index of the selected range.
 */
function getRangeIndex($randomNumber, $ranges)
{
  $nImg = count($ranges);
  // Loop through the ranges and find the appropriate index
  for ($i = 1; $i < $nImg; $i++) {
    if ($randomNumber <= $ranges[$i]) {
      return $i;
    }
  }
  // If the value is greater than the last range, return the last index
  return $nImg - 1;
}

/**
 * Unit test for `getHero()` function to simulate hero image selection.
 * 
 * Generates a histogram of image selection frequency over 10,000 trials.
 * 
 * @return void
 */
function unitTest()
{
  $hist = [];
  for ($i = 0; $i < 10000; $i++) {
    $img = getHero();
    if (isset($hist[$img])) {
      $hist[$img]++;
    } else {
      $hist[$img] = 1;
    }
  }
  ksort($hist);
  var_dump($hist);
  die;
}

/**
 * Inserts a link that triggers a video modal window to play a YouTube video.
 *
 * This function uses the modal and scripts defined in `videoModal.php`.
 * 
 * @param string $id YouTube video ID.
 * @param string $title The title for the video modal window (optional).
 * @param string $linkText The text shown as the clickable link (optional).
 * @return void
 */
function insertVideo($id, $title, $linkText = "")
{
  if (empty($id)) {
    die("Cannot insert YouTube video: ID not provided");
  }
  $title = $title ?: "YouTube Video";
  $linkText = $linkText ?: $title;
  echo <<<RENDER
<a href="#" class="video-link" data-bs-toggle="modal" data-bs-target="#videoModal" data-video-url="https://www.youtube.com/embed/$id" data-title="$title">$linkText</a>
RENDER;
}
?>

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <title>Manoj Thulasidas<?php if (isset($_GET['title'])) {echo ": " . $_GET['title'];} ?></title>
  <meta
    content="Explore the personal website of Assoc. Prof. Manoj Thulasidas, an educator in computer science, writer, philosopher, and thinker. Discover his life, work, and insights from a career spanning education, research, and thought leadership."
    name="description">
  <meta content="" name="keywords">

  <!-- Favicons -->
  <link href="assets/img/favicon.png" rel="icon">
  <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Fonts -->
  <link href="https://fonts.googleapis.com" rel="preconnect">
  <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
  <link
    href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Raleway:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap"
    rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css"
    rel="stylesheet">
  <link href="assets/vendor/aos/aos.css" rel="stylesheet">
  <link href="assets/vendor/glightbox/css/glightbox.min.css" rel="stylesheet">
  <link href="assets/vendor/swiper/swiper-bundle.min.css" rel="stylesheet">

  <!-- Main CSS File -->
  <link href="assets/css/main.css" rel="stylesheet">
  <style>
    /* Custom CSS for dotted underline */
    .dotted-underline {
      text-decoration: underline;
      text-decoration-style: dotted;
      text-decoration-color: #149ddd;
      color: #149ddd;
    }
  </style>
  <!-- =======================================================
  * Template Name: iPortfolio
  * Template URL: https://bootstrapmade.com/iportfolio-bootstrap-portfolio-websites-template/
  * Updated: Jun 29 2024 with Bootstrap v5.3.3
  * Author: BootstrapMade.com
  * License: https://bootstrapmade.com/license/
  ======================================================== -->
</head>