/* global React */
/* ============================================================
   Sneeuw — zachte, doorlopende sneeuwval op de homepage in
   kerstmodus (D22); de tegenhanger van Stardust bij het eindfeest.
   Decoratief: pointer-events:none, stopt zodra de homepage
   verdwijnt, en doet niets bij bewegingsreductie.
   ============================================================ */
function Snowfall() {
  const canvasRef = React.useRef(null);

  React.useEffect(() => {
    if (window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
    const canvas = canvasRef.current;
    if (!canvas) return;
    const ctx = canvas.getContext("2d");
    const dpr = Math.min(window.devicePixelRatio || 1, 2);
    let W = 0, H = 0;
    const resize = () => {
      W = window.innerWidth; H = window.innerHeight;
      canvas.width = W * dpr; canvas.height = H * dpr;
      canvas.style.width = W + "px"; canvas.style.height = H + "px";
      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
    };
    resize();
    window.addEventListener("resize", resize);

    // Twee vlok-sprites, vooraf getekend (goedkoop per frame), in de huisstijl-groenblauw zodat ze
    // op de crèmekleurige achtergrond zichtbaar zijn (wit zou wegvallen): een zacht stipje en een kristal.
    const S = 64;
    const TEAL = "130,182,174"; // teal-300
    const dot = document.createElement("canvas");
    dot.width = dot.height = S;
    const dctx = dot.getContext("2d");
    const g = dctx.createRadialGradient(S / 2, S / 2, 0, S / 2, S / 2, S / 2);
    g.addColorStop(0, `rgba(${TEAL},1)`);
    g.addColorStop(0.5, `rgba(${TEAL},0.8)`);
    g.addColorStop(1, `rgba(${TEAL},0)`);
    dctx.fillStyle = g;
    dctx.fillRect(0, 0, S, S);

    const crystal = document.createElement("canvas");
    crystal.width = crystal.height = S;
    const cctx = crystal.getContext("2d");
    cctx.translate(S / 2, S / 2);
    cctx.strokeStyle = `rgb(${TEAL})`;
    cctx.lineWidth = S / 11;
    cctx.lineCap = "round";
    for (let i = 0; i < 6; i++) { // zes armen met elk twee zijtakjes
      cctx.rotate(Math.PI / 3);
      cctx.beginPath();
      cctx.moveTo(0, 0); cctx.lineTo(0, -S * 0.44);
      cctx.moveTo(0, -S * 0.24); cctx.lineTo(-S * 0.1, -S * 0.34);
      cctx.moveTo(0, -S * 0.24); cctx.lineTo(S * 0.1, -S * 0.34);
      cctx.stroke();
    }

    const rnd = (a, b) => a + Math.random() * (b - a);
    const make = () => {
      const isCrystal = Math.random() < 0.3;
      const r = isCrystal ? rnd(6, 11) : rnd(2.5, 5.5); // straal; grotere vlokken vallen iets sneller
      return { x: rnd(0, W), y: -r * 2, r, isCrystal, rot: rnd(0, Math.PI), vrot: rnd(-0.01, 0.01),
        vy: 0.3 + r * 0.07 + rnd(0, 0.25), amp: rnd(0.2, 0.7), freq: rnd(0.008, 0.02), ph: rnd(0, Math.PI * 2), a: rnd(0.35, 0.6) }; // dekking: bewust subtiel
    };
    const count = Math.round(Math.min(140, Math.max(50, (W * H) / 9000)));
    const flakes = [];
    for (let i = 0; i < count; i++) { const f = make(); f.y = rnd(-H * 0.3, H); flakes.push(f); }

    let frame = 0, raf;
    function tick() {
      ctx.clearRect(0, 0, W, H);
      const fadeIn = Math.min(1, frame / 60); // ~1s zacht invloeien
      for (const f of flakes) {
        f.y += f.vy;
        f.x += Math.sin(frame * f.freq + f.ph) * f.amp + 0.12; // heen-en-weer dwarrelen + licht briesje
        if (f.y - f.r > H) Object.assign(f, make());
        if (f.x > W + f.r) f.x = -f.r; else if (f.x < -f.r) f.x = W + f.r;
        ctx.globalAlpha = f.a * fadeIn;
        if (f.isCrystal) {
          f.rot += f.vrot;
          ctx.save();
          ctx.translate(f.x, f.y); ctx.rotate(f.rot);
          ctx.drawImage(crystal, -f.r, -f.r, f.r * 2, f.r * 2);
          ctx.restore();
        } else {
          ctx.drawImage(dot, f.x - f.r, f.y - f.r, f.r * 2, f.r * 2);
        }
      }
      frame++;
      raf = requestAnimationFrame(tick);
    }
    raf = requestAnimationFrame(tick);

    return () => { cancelAnimationFrame(raf); window.removeEventListener("resize", resize); };
  }, []);

  return (
    <canvas ref={canvasRef} aria-hidden="true"
      style={{ position: "fixed", inset: 0, width: "100%", height: "100%", pointerEvents: "none", zIndex: 9999 }} />
  );
}

Object.assign(window, { Snowfall });
