// effects.jsx
// Global FX: particle field, light beams, ambient audio engine.
// Exposes: window.FX = { start, stop, setIntensity, sweep, pulse }
//          window.Audio2 = { start, stop, setMuted, isMuted }
(function(){

  // ===== Particle / beam field on #fx-canvas =====
  const canvas = document.getElementById('fx-canvas');
  const ctx = canvas.getContext('2d');
  let dpr = Math.min(window.devicePixelRatio || 1, 2);
  let W = 0, H = 0;
  function resize(){
    dpr = Math.min(window.devicePixelRatio || 1, 2);
    W = window.innerWidth;
    H = window.innerHeight;
    canvas.style.width  = W + 'px';
    canvas.style.height = H + 'px';
    canvas.width  = Math.floor(W * dpr);
    canvas.height = Math.floor(H * dpr);
    ctx.setTransform(dpr,0,0,dpr,0,0);
  }
  window.addEventListener('resize', resize);
  resize();

  // particles
  let particles = [];
  function makeParticles(n){
    particles = [];
    for (let i = 0; i < n; i++){
      particles.push({
        x: Math.random()*W,
        y: Math.random()*H,
        z: Math.random()*0.8 + 0.2,           // depth
        r: Math.random()*1.4 + 0.4,
        vx: (Math.random()-0.5)*0.08,
        vy: -Math.random()*0.18 - 0.05,
        a: Math.random()*0.6 + 0.2,
        aw: Math.random()*0.012 + 0.004,
      });
    }
  }

  let intensity = 1;
  let running = false;
  let raf = 0;
  let t0 = performance.now();

  // Sweep — a horizontal beam of light traveling once
  const sweeps = [];
  function sweep(opts){
    sweeps.push(Object.assign({
      y: H * 0.5,
      width: 220,
      duration: 1400,
      startedAt: performance.now(),
      color: 'rgba(78,179,196,0.55)',
    }, opts || {}));
  }

  // Pulse — radial flash from a point
  const pulses = [];
  function pulse(opts){
    pulses.push(Object.assign({
      x: W*0.5, y: H*0.5, radius: 0,
      maxR: Math.max(W,H)*0.7,
      duration: 1100,
      startedAt: performance.now(),
      color: 'rgba(26,142,162,0.45)',
    }, opts || {}));
  }

  // Ambient radial — slow breathing teal glow center
  function drawAmbient(t){
    // deep teal radial bloom that drifts subtly
    const cx = W*0.5 + Math.sin(t*0.00012)*W*0.05;
    const cy = H*0.55 + Math.cos(t*0.00009)*H*0.06;
    const r  = Math.max(W,H)*0.65;
    const g = ctx.createRadialGradient(cx, cy, 0, cx, cy, r);
    g.addColorStop(0, `rgba(11,118,137,${0.28*intensity})`);
    g.addColorStop(0.35, `rgba(11,118,137,${0.10*intensity})`);
    g.addColorStop(1, 'rgba(0,0,0,0)');
    ctx.fillStyle = g;
    ctx.fillRect(0,0,W,H);

    // distant horizontal beam (very subtle)
    const beamY = H*0.30;
    const bg = ctx.createLinearGradient(0, beamY-60, 0, beamY+60);
    bg.addColorStop(0,  'rgba(78,179,196,0)');
    bg.addColorStop(0.5,`rgba(78,179,196,${0.05*intensity})`);
    bg.addColorStop(1,  'rgba(78,179,196,0)');
    ctx.fillStyle = bg;
    ctx.fillRect(0, beamY-60, W, 120);
  }

  function drawParticles(t){
    for (const p of particles){
      p.x += p.vx * p.z;
      p.y += p.vy * p.z;
      p.a += p.aw;
      if (p.y < -10) { p.y = H + 10; p.x = Math.random()*W; }
      if (p.x < -10) p.x = W + 10;
      if (p.x > W + 10) p.x = -10;
      const alpha = (Math.sin(p.a)*0.5 + 0.5) * 0.6 * p.z * intensity;
      ctx.beginPath();
      ctx.fillStyle = `rgba(196,230,236,${alpha})`;
      ctx.arc(p.x, p.y, p.r * (0.6 + p.z*0.6), 0, Math.PI*2);
      ctx.fill();
    }
  }

  function drawSweeps(t){
    for (let i = sweeps.length - 1; i >= 0; i--){
      const s = sweeps[i];
      const k = (t - s.startedAt) / s.duration;
      if (k > 1) { sweeps.splice(i,1); continue; }
      // Position based on k 0->1, sweep across full screen
      const x = -s.width + (W + s.width*2) * k;
      // soft horizontal gradient brush
      const g = ctx.createLinearGradient(x - s.width, 0, x + s.width, 0);
      g.addColorStop(0,   'rgba(78,179,196,0)');
      g.addColorStop(0.5, s.color);
      g.addColorStop(1,   'rgba(78,179,196,0)');
      ctx.fillStyle = g;
      // ease in/out alpha
      const a = Math.sin(k*Math.PI);
      ctx.globalAlpha = a;
      ctx.fillRect(0, s.y - 80, W, 160);
      ctx.globalAlpha = 1;
    }
  }

  function drawPulses(t){
    for (let i = pulses.length - 1; i >= 0; i--){
      const p = pulses[i];
      const k = (t - p.startedAt) / p.duration;
      if (k > 1) { pulses.splice(i,1); continue; }
      const r = p.maxR * k;
      const alpha = (1 - k) * 0.6;
      const g = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, r);
      g.addColorStop(0,  p.color.replace(/[\d.]+\)/, `${alpha})`));
      g.addColorStop(0.7,p.color.replace(/[\d.]+\)/, `${alpha*0.2})`));
      g.addColorStop(1,  'rgba(0,0,0,0)');
      ctx.fillStyle = g;
      ctx.fillRect(0,0,W,H);
    }
  }

  function frame(){
    if (!running) return;
    const t = performance.now();
    ctx.clearRect(0,0,W,H);
    drawAmbient(t);
    drawParticles(t);
    drawSweeps(t);
    drawPulses(t);
    raf = requestAnimationFrame(frame);
  }

  function start(){
    if (running) return;
    makeParticles(140);
    running = true;
    t0 = performance.now();
    raf = requestAnimationFrame(frame);
  }
  function stop(){
    running = false;
    cancelAnimationFrame(raf);
    ctx.clearRect(0,0,W,H);
  }
  function setIntensity(v){
    intensity = Math.max(0, Math.min(2, v));
    const target = Math.floor(140 * intensity);
    if (Math.abs(target - particles.length) > 20){
      makeParticles(target);
    }
  }

  window.FX = { start, stop, setIntensity, sweep, pulse };

  // ===== Áudio removido =====
  // O áudio ambiente foi removido do teaser. Mantemos um stub no-op de
  // window.Audio2 para qualquer chamada remanescente não quebrar.
  window.Audio2 = {
    start: () => {},
    stop: () => {},
    setMuted: () => {},
    isMuted: () => true,
  };
})();
