/* Home.jsx — under the hero: the three tabs as numbered channels. On a fine pointer a preview
   follows the cursor (a project list, a collection cover, the featured broadcast). */
const { useState: useHomeState, useEffect: useHomeEffect, useRef: useHomeRef } = React;

function HomeIndex() {
  const photoTotal = PHOTO_COLLECTIONS.reduce((n, c) => n + c.photos.length, 0);
  const firstCollection = PHOTO_COLLECTIONS[0];
  const cover = firstCollection.photos.find((p) => p.id === firstCollection.cover) || firstCollection.photos[0];
  const featured = NEEL_VIDEOS.find((v) => v.featured);
  const tabs = [
    { n: "01", to: "/software", label: "Software", line: "agmux, Strix, Tare, Flint and more, with what each one does and where to get it.", meta: `${SOFTWARE_PROJECTS.length} projects` },
    { n: "02", to: "/photography", label: "Photography", line: "Series and travel work, each collection shareable and every photo at full resolution.", meta: `${PHOTO_COLLECTIONS.length} collections · ${photoTotal} photographs`, img: photoPreview(cover) },
    { n: "03", to: "/videography", label: "Videography", line: "News packages, features and recaps reported, shot and edited for InFocus News.", meta: `${NEEL_VIDEOS.length} videos`, img: VIDEO_THUMB_BASE + featured.thumbSrc },
  ];
  const [active, setActive] = useHomeState(null);
  const floatRef = useChannelFloat(active !== null);

  return (
    <Section style={{ paddingTop: "var(--section)", paddingBottom: "var(--section)" }}>
      <Rule label="Channels" style={{ marginBottom: 0 }} />
      <div className="channels" onMouseLeave={() => setActive(null)}>
        {tabs.map((t, i) => (
          <Link key={t.to} className="channel" to={t.to} onMouseEnter={() => setActive(i)} onFocus={() => setActive(null)}>
            <span className="channel-n">CH {t.n}</span>
            <span className="channel-title">{t.label}</span>
            <span className="channel-copy">
              <span className="body">{t.line}</span>
              <span className="label">{t.meta}</span>
            </span>
            <span className="channel-arrow" aria-hidden="true"><Icon name="arrow-right" size={18} /></span>
          </Link>
        ))}
      </div>
      <div className="channel-float" ref={floatRef} data-on={active !== null || undefined} aria-hidden="true">
        <div className="float-list" data-on={active === 0 || undefined}>
          <div className="float-bar"><i /><i /><i /><span>~/software</span></div>
          <div className="float-rows">
            {SOFTWARE_PROJECTS.slice(0, 6).map((p) => <div key={p.name}><span>{p.name}</span><span>{p.platform}</span></div>)}
          </div>
        </div>
        {tabs.map((t, i) => t.img && <img key={t.to} src={t.img} alt="" loading="lazy" data-on={active === i || undefined} />)}
      </div>
    </Section>
  );
}

/* Eases the preview toward the cursor every frame while it is showing. */
function useChannelFloat(on) {
  const ref = useHomeRef(null);
  const target = useHomeRef({ x: -999, y: -999 });
  const pos = useHomeRef(null);
  useHomeEffect(() => {
    const onMove = (e) => { target.current = { x: e.clientX, y: e.clientY }; };
    window.addEventListener("mousemove", onMove, { passive: true });
    return () => window.removeEventListener("mousemove", onMove);
  }, []);
  useHomeEffect(() => {
    if (!on || !ref.current) return undefined;
    const el = ref.current;
    const still = prefersReducedMotion();
    let raf = 0;
    const frame = () => {
      const t = target.current;
      const p = pos.current && !still ? pos.current : t;
      const next = { x: p.x + (t.x - p.x) * 0.16, y: p.y + (t.y - p.y) * 0.16 };
      pos.current = next;
      const w = el.offsetWidth;
      const x = next.x + 28 + w > window.innerWidth - 16 ? next.x - 28 - w : next.x + 28;
      el.style.transform = `translate3d(${x}px, ${next.y - el.offsetHeight / 2}px, 0)`;
      raf = requestAnimationFrame(frame);
    };
    raf = requestAnimationFrame(frame);
    return () => { cancelAnimationFrame(raf); pos.current = null; };
  }, [on]);
  return ref;
}

Object.assign(window, { HomeIndex });
