// Into the Wild: shared layout (nav, covers, testimonials, footer)

const NAV_LINKS = [
{ href: '/#next', label: 'Edition V' },
{ href: '/#editions', label: 'Past editions' },
{ href: '/#voices', label: 'Voices' }];


function Nav() {
  const [open, setOpen] = React.useState(false);
  const [solid, setSolid] = React.useState(false);
  const close = () => setOpen(false);

  React.useEffect(() => {
    const onScroll = () => setSolid(window.scrollY > 120);
    onScroll();
    window.addEventListener('scroll', onScroll);
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  React.useEffect(() => {
    if (!open) return;
    const onKey = (e) => {if (e.key === 'Escape') setOpen(false);};
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [open]);

  return (
    <nav className={`nav ${solid || open ? 'nav-solid' : ''} ${open ? 'nav-open' : ''}`}>
      <a href="/" className="nav-logo" onClick={close}>
        <img className="nav-wordmark nav-wordmark-positive" src="/assets/logo-positive.png" alt="Into the Wild" />
        <img className="nav-wordmark nav-wordmark-negative" src="/assets/logo-negative.png" alt="" aria-hidden="true" />
      </a>
      <div className="nav-links">
        {NAV_LINKS.map((l) => <a key={l.href} href={l.href} className="nav-link">{l.label}</a>)}
        <a href="/#notify" className="nav-cta" onClick={close}>Get notified</a>
        <button
          type="button"
          className="nav-burger"
          aria-label={open ? 'Close menu' : 'Open menu'}
          aria-expanded={open}
          aria-controls="nav-mobile"
          onClick={() => setOpen(!open)}>
          <span /><span /><span />
        </button>
      </div>
      <div id="nav-mobile" className="nav-mobile" hidden={!open}>
        {NAV_LINKS.map((l) => <a key={l.href} href={l.href} onClick={close}>{l.label}</a>)}
        {EDITIONS.map((e) =>
        <a key={e.n} href={editionUrl(e.n)} onClick={close} className="nav-mobile-sub">Edition {e.roman} · {e.year}</a>
        )}
      </div>
    </nav>);

}

function editionCover(e) {
  const { a, b, c } = e.colors;
  const svg = `<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 200' preserveAspectRatio='xMidYMid slice'>
    <rect width='320' height='200' fill='${a}'/>
    <g opacity='0.85'>
      <path d='M 0 140 L 60 90 L 110 130 L 180 70 L 240 120 L 320 80 L 320 200 L 0 200 Z' fill='${c}'/>
      <path d='M 0 170 L 80 135 L 160 160 L 240 130 L 320 150 L 320 200 L 0 200 Z' fill='${b}' opacity='0.8'/>
    </g>
    <circle cx='260' cy='45' r='18' fill='${b}'/>
    <g stroke='${b}' stroke-width='0.5' opacity='0.35' fill='none'>
      <path d='M 0 30 L 320 60'/><path d='M 0 50 L 320 80'/><path d='M 0 70 L 320 40'/>
    </g>
  </svg>`;
  return `url("data:image/svg+xml;utf8,${encodeURIComponent(svg)}")`;
}

function Testimonials({ quotes, title, id }) {
  if (!quotes.length) return null;
  return (
    <section className="section testimonials" id={id}>
      <div className="wrap">
        <h2 className="section-title" style={{ color: 'var(--cream)' }}>{title}</h2>
        <div className="testi-grid">
          {quotes.map((t, i) =>
          <figure key={i} className="testi-card" style={{ '--rot': t.rot }}>
              <blockquote className="testi-quote">{t.q}</blockquote>
              <figcaption className="testi-author">
                <span className="flag" style={{ background: t.flag }} />
                <span>{t.who} · {t.country} · <a href={editionUrl(t.edition)}>Edition {romanOf(t.edition)}</a></span>
              </figcaption>
            </figure>
          )}
        </div>
      </div>
    </section>);

}

function Footer() {
  return (
    <footer className="footer">
      <div className="footer-top">
        <div className="footer-word">
          <img className="footer-wordmark" src="/assets/logo-negative.png" alt="Into the Wild" />
        </div>
        <div className="footer-col">
          <h4>The project</h4>
          <ul>
            <li><a href="/#next">Edition V · 2027</a></li>
            <li><a href="/#notify">Get notified</a></li>
            <li>
              <a href="https://www.youtube.com/watch?v=M60l-IGsPkw&list=PLGqbOK8rCMslB8xaT9ZzqBiePgBYvK8nz" target="_blank" rel="noopener">Videos</a>
            </li>
          </ul>
        </div>
        <div className="footer-col">
          <h4>Editions</h4>
          <ul>
            {EDITIONS.map((e) =>
            <li key={e.n}><a href={editionUrl(e.n)}>{e.name} · {e.year}</a></li>
            )}
          </ul>
        </div>
        <div className="footer-col">
          <h4>Reach us</h4>
          <ul>
            <li><a href="mailto:info@xeracion.org">info@xeracion.org</a></li>
            <li><a href="https://instagram.com/xeracion" target="_blank" rel="noopener">Instagram · @xeracion</a></li>
            <li><a href="https://xeracion.org" target="_blank" rel="noopener">xeracion.org</a></li>
          </ul>
        </div>
      </div>
      <div className="footer-logos">
        <a href="https://xeracion.org" target="_blank" rel="noopener" className="footer-logo footer-logo-xeracion">
          <span className="footer-logo-label">Organised by</span>
          <img src="/assets/logo-xeracion-white.png" alt="Asociación Xeración" width="480" height="99" />
        </a>
        <div className="footer-logo footer-logo-eu">
          <img src="/assets/eu-cofunded-neg.png" alt="Co-funded by the European Union" width="640" height="137" />
        </div>
        <p className="footer-disclaimer">
          Co-funded by the European Union. Views and opinions expressed are however
          those of the author(s) only and do not necessarily reflect those of the
          European Union or the European Education and Culture Executive Agency
          (EACEA). Neither the European Union nor the granting authority can be held
          responsible for them.
        </p>
      </div>
      <div className="footer-bottom">
        <span>© 2023–2026 Into the Wild project · Made by Xeracion team</span>
      </div>
    </footer>);

}

Object.assign(window, { Nav, editionCover, Testimonials, Footer });
