import { useId, useRef, type PointerEvent } from "react";
import Logo from "./Logo";

const timeline = [
  { y: 9, color: "var(--magenta)", spans: [[-6, 52], [51, 106], [168, 45], [227, 90], [339, 61], [412, 88]] },
  { y: 26, color: "var(--counted)", spans: [[12, 73], [104, 33], [145, 86], [257, 55], [323, 82], [434, 60]] },
  { y: 43, color: "var(--cyan)", spans: [[-5, 40], [44, 41], [95, 99], [213, 49], [278, 87], [380, 43], [433, 71]] },
  { y: 60, color: "var(--magenta)", spans: [[4, 89], [111, 100], [225, 35], [274, 49], [338, 98], [453, 48]] },
  { y: 77, color: "var(--counted)", spans: [[-4, 47], [60, 57], [131, 35], [186, 92], [292, 51], [360, 34], [408, 99]] },
];

export default function GiantName() {
  const id = useId();
  const points = useRef<DOMPoint[]>([]);
  const trail = useRef<SVGPolylineElement>(null);
  const tip = useRef<SVGCircleElement>(null);

  const move = (event: PointerEvent<SVGSVGElement>) => {
    if (event.pointerType === "touch" || !window.matchMedia("(hover: hover) and (pointer: fine) and (prefers-reduced-motion: no-preference)").matches) return;

    const svg = event.currentTarget;
    const screen = svg.getScreenCTM();
    if (!screen) return;

    const point = new DOMPoint(event.clientX, event.clientY).matrixTransform(screen.inverse());
    const previous = points.current.at(-1);
    if (previous) {
      const distance = Math.hypot(point.x - previous.x, point.y - previous.y);
      if (distance < 7) return;
      if (distance > 65) points.current = [];
    }
    points.current.push(point);
    if (points.current.length > 6) points.current.shift();

    trail.current?.setAttribute("points", points.current.map(({ x, y }) => `${x},${y}`).join(" "));
    tip.current?.setAttribute("cx", String(point.x));
    tip.current?.setAttribute("cy", String(point.y));
    svg.setAttribute("data-revealing", "");
  };

  const leave = (event: PointerEvent<SVGSVGElement>) => {
    points.current = [];
    event.currentTarget.removeAttribute("data-revealing");
  };

  return (
    <div id="closing-wordmark" className="flex items-center gap-[3vw] select-none">
      <Logo className="w-[20%] flex-none" />
      <svg
        className="footer-wordmark isolate min-w-0 flex-1 overflow-visible"
        viewBox="-10 0 520 112"
        role="img"
        aria-label="Parallax"
        onPointerMove={move}
        onPointerLeave={leave}
        onPointerCancel={leave}
      >
        <defs>
          <clipPath id={`${id}-letters`}>
            <use href={`#${id}-text`} />
          </clipPath>
          <filter id={`${id}-soften`} filterUnits="userSpaceOnUse" x="-10" y="0" width="520" height="112">
            <feGaussianBlur stdDeviation="7" />
          </filter>
          <mask id={`${id}-trail`} maskUnits="userSpaceOnUse" x="-10" y="0" width="520" height="112">
            <g filter={`url(#${id}-soften)`}>
              <polyline ref={trail} fill="none" stroke="white" strokeWidth="30" strokeLinecap="round" strokeLinejoin="round" />
              <circle ref={tip} r="27" fill="white" />
            </g>
          </mask>
        </defs>
        <text id={`${id}-text`} className="fill-ink font-sans text-[112px] font-extrabold tracking-[-.045em]" x="0" y="86" textLength="500" lengthAdjust="spacingAndGlyphs">Parallax</text>
        <g className="footer-wordmark-reveal" aria-hidden="true" clipPath={`url(#${id}-letters)`}>
          <g mask={`url(#${id}-trail)`}>
            <rect x="-10" width="520" height="112" fill="var(--card)" />
            {timeline.map(({ y, color, spans }) => (
              <g key={y} fill={color}>
                {spans.map(([x, width]) => <rect key={x} x={x} y={y} width={width} height="12" rx="2" />)}
              </g>
            ))}
          </g>
        </g>
      </svg>
    </div>
  );
}
