// Shared site components: Nav, Footer, Logo, HelixRibbon, ThemeBadge.
// Every page imports this file and uses these directly.

const { useEffect, useRef } = React;

// Theme colors for animations
const THEME_COLOR_LIST = ['#2ea043', '#1f6feb', '#f59e0b', '#db2777', '#8250df'];

// ────────────────────────────────────────────────────────────
// Logo — Binary helix leaf (Logo B from exploration)
// ────────────────────────────────────────────────────────────
function Logo({ size = 44 }) {
  return (
    <svg className="brand__svg" width={size} height={size * 1.09} viewBox="0 0 86 94" fill="none">
      <defs>
        <linearGradient id="navLeafBig" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%"  stopColor="#a9e0b3" />
          <stop offset="100%" stopColor="#2ea043" />
        </linearGradient>
        <linearGradient id="navLeafSmall" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%"  stopColor="#7dd28d" />
          <stop offset="100%" stopColor="#1b6e30" />
        </linearGradient>
      </defs>
      <path d="M40 70 C 42 78, 48 86, 58 90" stroke="#0a1408" strokeWidth="1.8" strokeLinecap="round" fill="none" />
      <path d="M34 4 C 14 12, 10 34, 14 52 C 18 66, 28 72, 34 70 C 40 72, 50 66, 54 52 C 58 34, 54 12, 34 4 Z"
            fill="url(#navLeafBig)" stroke="#0a1408" strokeWidth="2.4" strokeLinejoin="round" />
      <text x="34" y="26" textAnchor="middle" fontFamily="JetBrains Mono, monospace" fontSize="12" fontWeight="600" fill="#0a1408" letterSpacing="1">01</text>
      <text x="34" y="42" textAnchor="middle" fontFamily="JetBrains Mono, monospace" fontSize="12" fontWeight="600" fill="#0a1408" letterSpacing="1">10</text>
      <text x="34" y="58" textAnchor="middle" fontFamily="JetBrains Mono, monospace" fontSize="12" fontWeight="600" fill="#0a1408" letterSpacing="1">01</text>
      <g transform="translate(46 80) rotate(-20)">
        <path d="M0 0 C 2 -8, 10 -14, 18 -12 C 22 -6, 18 4, 10 4 C 4 4, 0 2, 0 0 Z"
              fill="url(#navLeafSmall)" stroke="#0a1408" strokeWidth="1.6" strokeLinejoin="round" />
        <path d="M1 0 C 6 -4, 12 -8, 17 -11" stroke="#0a1408" strokeWidth="0.9" fill="none" opacity="0.65" />
        <path d="M5 -2 L 8 -7"  stroke="#0a1408" strokeWidth="0.7" opacity="0.55" />
        <path d="M9 -5 L 13 -9" stroke="#0a1408" strokeWidth="0.7" opacity="0.55" />
        <path d="M4 1 L 7 -1"   stroke="#0a1408" strokeWidth="0.7" opacity="0.55" />
      </g>
    </svg>
  );
}

// ────────────────────────────────────────────────────────────
// Helix ribbon — animated canvas for hero
// ────────────────────────────────────────────────────────────
function HelixRibbon() {
  const ref = useRef(null);
  useEffect(() => {
    const c = ref.current; if (!c) return;
    const ctx = c.getContext('2d');
    let raf, t = 0, alive = true;
    const resize = () => {
      const r = c.getBoundingClientRect();
      c.width = r.width * devicePixelRatio;
      c.height = r.height * devicePixelRatio;
      ctx.setTransform(1, 0, 0, 1, 0, 0);
      ctx.scale(devicePixelRatio, devicePixelRatio);
    };
    resize();
    const draw = () => {
      if (!alive) return;
      const { width: W, height: H } = c.getBoundingClientRect();
      ctx.clearRect(0, 0, W, H);
      const cy = H * 0.55, amp = H * 0.22;
      THEME_COLOR_LIST.forEach((color, i) => {
        ctx.beginPath();
        ctx.lineWidth = 1.5;
        ctx.strokeStyle = color + '55';
        for (let x = 0; x <= W; x += 4) {
          const phase = t * 0.0006 + i * 0.9;
          const y = cy + Math.sin(x * 0.005 + phase) * amp * (1 - i * 0.12);
          if (x === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y);
        }
        ctx.stroke();
      });
      t += 16;
      raf = requestAnimationFrame(draw);
    };
    draw();
    window.addEventListener('resize', resize);
    return () => { alive = false; cancelAnimationFrame(raf); window.removeEventListener('resize', resize); };
  }, []);
  return <canvas ref={ref} className="hero__canvas" />;
}

// ────────────────────────────────────────────────────────────
// Site nav (sticky)
// ────────────────────────────────────────────────────────────
function SiteNav({ active }) {
  const link = (id, label, href) => (
    <a key={id} href={href} className={active === id ? 'is-active' : ''}>{label}</a>
  );
  return (
    <header className="nav">
      <a className="brand" href="index.html">
        <Logo size={44} />
        <span className="brand__name">BioAI Lab</span>
      </a>
      <nav className="navlinks">
        {link('home', 'Home', 'index.html')}
        {link('research', 'Research', 'research.html')}
        {link('team', 'Team', 'team.html')}
        {link('publications', 'Publications', 'publications.html')}
        {link('teaching', 'Teaching', 'teaching.html')}
        <a href="contact.html" className="cta">Contact →</a>
      </nav>
    </header>
  );
}

// ────────────────────────────────────────────────────────────
// Site footer
// ────────────────────────────────────────────────────────────
function SiteFooter() {
  return (
    <footer className="footer">
      <div className="footer__inner">
        <div className="footer__brand">
          <h4>BioAI Lab</h4>
          <p>Computational tools for the biology of tomorrow — at the School of Computer Science, University of Guelph.</p>
        </div>
        <div>
          <h4>Explore</h4>
          <ul>
            <li><a href="research.html">Research</a></li>
            <li><a href="team.html">Team</a></li>
            <li><a href="publications.html">Publications</a></li>
            <li><a href="teaching.html">Teaching</a></li>
          </ul>
        </div>
        <div>
          <h4>Connect</h4>
          <ul>
            <li><a href="mailto:yyan15@uoguelph.ca">Email</a></li>
            <li><a href="https://github.com/orgs/LabBioAI">GitHub</a></li>
            <li><a href="https://orcid.org/0000-0002-8380-2863">ORCID</a></li>
            <li><a href="contact.html">Contact</a></li>
          </ul>
        </div>
        <div>
          <h4>Where</h4>
          <ul>
            <li>School of Computer Science</li>
            <li>University of Guelph</li>
          </ul>
        </div>
      </div>
      <div className="footer__bottom">
        <span>BioAI Lab · 2026</span>
        <span>Bioinformatics & AI</span>
      </div>
    </footer>
  );
}

// ────────────────────────────────────────────────────────────
// Theme badge — small inline label using a theme's color
// ────────────────────────────────────────────────────────────
function ThemeBadge({ theme }) {
  const t = window.LAB.THEMES[theme];
  if (!t) return null;
  return (
    <span className="tag tag--theme" style={{ '--theme-c': t.color }}>{t.short}</span>
  );
}

window.Logo = Logo;
window.HelixRibbon = HelixRibbon;
window.SiteNav = SiteNav;
window.SiteFooter = SiteFooter;
window.ThemeBadge = ThemeBadge;
