/* neutron.jsx — Blurry neutron emission animation for dark hero banners */

function NeutronAnimation() {
  const canvasRef = React.useRef(null);

  React.useEffect(() => {
    const canvas = canvasRef.current;
    if (!canvas) return;
    const ctx = canvas.getContext('2d');
    let animId;
    const dpr = window.devicePixelRatio || 1;

    const resize = () => {
      const rect = canvas.getBoundingClientRect();
      canvas.width = rect.width * dpr;
      canvas.height = rect.height * dpr;
      ctx.scale(dpr, dpr);
    };
    resize();
    window.addEventListener('resize', resize);

    /* Particles: nucleus protons/neutrons + emitted neutrons */
    const nucleons = [];
    for (let i = 0; i < 12; i++) {
      const angle = (i / 12) * Math.PI * 2;
      const r = 12 + Math.random() * 8;
      nucleons.push({
        x: Math.cos(angle) * r,
        y: Math.sin(angle) * r,
        radius: 4 + Math.random() * 3,
        isProton: i % 2 === 0,
        phase: Math.random() * Math.PI * 2,
      });
    }

    /* Emitted neutrons */
    const emitted = [];
    function spawnNeutron(time) {
      const angle = Math.random() * Math.PI * 2;
      emitted.push({
        angle,
        dist: 20,
        speed: 0.4 + Math.random() * 0.6,
        radius: 3 + Math.random() * 2,
        alpha: 1,
        born: time,
        trail: [],
      });
    }

    let lastSpawn = -Math.random() * 3;
    let nextInterval = 1 + Math.random() * 2;
    const cxRatio = 0.5 + Math.random() * 0.4;
    const cyRatio = 0.3 + Math.random() * 0.4;

    function animate(ts) {
      const time = ts * 0.001;
      const w = canvas.width / dpr;
      const h = canvas.height / dpr;
      ctx.clearRect(0, 0, w, h);

      const cx = w * cxRatio;
      const cy = h * cyRatio;

      /* Spawn neutrons at random intervals */
      if (time - lastSpawn > nextInterval) {
        spawnNeutron(time);
        lastSpawn = time;
        nextInterval = 1.2 + Math.random() * 3;
      }

      /* Draw glow around nucleus */
      const grd = ctx.createRadialGradient(cx, cy, 5, cx, cy, 50);
      grd.addColorStop(0, 'rgba(99, 102, 241, 0.12)');
      grd.addColorStop(0.5, 'rgba(99, 102, 241, 0.04)');
      grd.addColorStop(1, 'rgba(99, 102, 241, 0)');
      ctx.fillStyle = grd;
      ctx.beginPath();
      ctx.arc(cx, cy, 50, 0, Math.PI * 2);
      ctx.fill();

      /* Draw nucleus */
      nucleons.forEach((n) => {
        const jx = Math.sin(time * 1.5 + n.phase) * 2;
        const jy = Math.cos(time * 1.2 + n.phase) * 2;
        ctx.beginPath();
        ctx.arc(cx + n.x + jx, cy + n.y + jy, n.radius, 0, Math.PI * 2);
        ctx.fillStyle = n.isProton
          ? 'rgba(129, 140, 248, 0.5)'
          : 'rgba(199, 210, 254, 0.35)';
        ctx.fill();
      });

      /* Draw & update emitted neutrons */
      for (let i = emitted.length - 1; i >= 0; i--) {
        const n = emitted[i];
        n.dist += n.speed;
        n.alpha = Math.max(0, 1 - (n.dist - 20) / 120);

        const nx = cx + Math.cos(n.angle) * n.dist;
        const ny = cy + Math.sin(n.angle) * n.dist;

        /* Trail */
        n.trail.push({ x: nx, y: ny, alpha: n.alpha });
        if (n.trail.length > 12) n.trail.shift();

        n.trail.forEach((t, ti) => {
          const ta = t.alpha * (ti / n.trail.length) * 0.3;
          ctx.beginPath();
          ctx.arc(t.x, t.y, n.radius * 0.6, 0, Math.PI * 2);
          ctx.fillStyle = `rgba(199, 210, 254, ${ta})`;
          ctx.fill();
        });

        /* Main particle */
        const pgrd = ctx.createRadialGradient(nx, ny, 0, nx, ny, n.radius * 3);
        pgrd.addColorStop(0, `rgba(199, 210, 254, ${n.alpha * 0.6})`);
        pgrd.addColorStop(0.4, `rgba(129, 140, 248, ${n.alpha * 0.3})`);
        pgrd.addColorStop(1, 'rgba(129, 140, 248, 0)');
        ctx.fillStyle = pgrd;
        ctx.beginPath();
        ctx.arc(nx, ny, n.radius * 3, 0, Math.PI * 2);
        ctx.fill();

        ctx.beginPath();
        ctx.arc(nx, ny, n.radius, 0, Math.PI * 2);
        ctx.fillStyle = `rgba(224, 231, 255, ${n.alpha * 0.8})`;
        ctx.fill();

        if (n.alpha <= 0) emitted.splice(i, 1);
      }

      animId = requestAnimationFrame(animate);
    }

    animId = requestAnimationFrame(animate);
    return () => {
      cancelAnimationFrame(animId);
      window.removeEventListener('resize', resize);
    };
  }, []);

  return <canvas ref={canvasRef} className="neutron-canvas"></canvas>;
}

Object.assign(window, { NeutronAnimation });
