<?php
/**
 * Snippet: menu
 * Params:
 *   $currentPage  Page  The currently active page
 */

// Gather all published chapter pages sorted by chapter_number
$chapters = $site->index()->filterBy('template', 'chapter')->sortBy('chapter_number', 'asc');

// Group chapters by their `part` field value
$parts = [];
foreach ($chapters as $ch) {
    $partNum = $ch->part()->toInt() ?: 1;
    if (!isset($parts[$partNum])) {
        $parts[$partNum] = [
            'label'    => 'Part ' . ['', 'I', 'II', 'III', 'IV', 'V'][$partNum] ?? 'Part ' . $partNum,
            'sub'      => $ch->part_name()->value(),
            'chapters' => [],
        ];
    } else if (empty($parts[$partNum]['sub'])) {
        $parts[$partNum]['sub'] = $ch->part_name()->value();
    }
    $parts[$partNum]['chapters'][] = $ch;
}
ksort($parts);

// Calculate reading progress (chapter number / total chapters)
$totalChapters = $chapters->count();
$currentNum    = 0;
if ($currentPage->template()->name() === 'chapter') {
    $currentNum = $currentPage->chapter_number()->toInt();
}
$progressPct = $totalChapters > 0 ? round(($currentNum / $totalChapters) * 100) : 0;

// Label for the progress meta
$currentLabel = $currentPage->template()->name() === 'home'
    ? 'Cover'
    : ($currentPage->template()->name() === 'chapter'
        ? 'Chapter ' . str_pad($currentPage->chapter_number()->value(), 2, '0', STR_PAD_LEFT)
        : $currentPage->title()->value());
?>

<div class="menu-overlay" id="menu">
  <button class="menu-close" aria-label="Close menu">[ Close — Esc ]</button>
  <div class="menu-inner">

    <header class="menu-head">
      <div class="menu-section-label">Chapters · <?= $totalChapters ?></div>
      <input class="menu-filter" type="search" placeholder="Filter chapters…" aria-label="Filter chapters">
    </header>

    <div class="menu-list">

      <?php foreach ($parts as $partData): ?>
      <div class="menu-part">
        <div class="menu-part-head">
          <span class="menu-part-label"><?= html($partData['label']) ?></span>
          <span class="menu-part-sub"><?= html($partData['sub']) ?></span>
        </div>
        <ul class="menu-chapters">
          <?php foreach ($partData['chapters'] as $ch): ?>
          <li<?= $ch->is($currentPage) ? ' class="current"' : '' ?>>
            <a href="<?= $ch->url() ?>">
              <span class="num"><?= str_pad($ch->chapter_number()->value(), 2, '0', STR_PAD_LEFT) ?></span>
              <span class="title"><?= $ch->title()->html() ?></span>
            </a>
          </li>
          <?php endforeach ?>
        </ul>
      </div>
      <?php endforeach ?>

      <div class="menu-part">
        <div class="menu-part-head">
          <span class="menu-part-label">Front matter</span>
          <span class="menu-part-sub">&nbsp;</span>
        </div>
        <ul class="menu-chapters">
          <li<?= $currentPage->isHomePage() ? ' class="current"' : '' ?>>
            <a href="<?= $site->url() ?>">
              <span class="num">—</span>
              <span class="title">Cover / Home</span>
            </a>
          </li>
          <?php foreach ($site->index()->filterBy('template', 'default') as $defPage): ?>
          <li<?= $defPage->is($currentPage) ? ' class="current"' : '' ?>>
            <a href="<?= $defPage->url() ?>">
              <span class="num">—</span>
              <span class="title"><?= $defPage->title()->html() ?></span>
            </a>
          </li>
          <?php endforeach ?>
        </ul>
      </div>

      <div class="menu-empty">No chapters match.</div>
    </div>

    <aside class="menu-side">
      <div class="menu-section-label">About</div>
      <p class="menu-about"><?= $site->about_text()->html() ?></p>
      <div class="menu-progress">
        <div class="menu-progress-bar">
          <span style="width: <?= $progressPct ?>%;"></span>
        </div>
        <div class="menu-progress-meta">
          <span>Currently reading</span>
          <span class="accent"><?= html($currentLabel) ?></span>
        </div>
      </div>
    </aside>

  </div>
</div>
