/*
  particles.js — Save Pet
  Lightweight particle system for juice: pop bursts, coin sparkle,
  floating hearts on hit, and confetti on level win.
*/
window.SP = window.SP || {};

SP.Particles = (function () {
  "use strict";

  function spawnBurst(list, x, y, opts) {
    opts = opts || {};
    const count = opts.count || 10;
    const color = opts.color || "#FFD54F";
    const speed = opts.speed || 140;
    const life = opts.life || 0.6;
    for (let i = 0; i < count; i++) {
      const a = Math.random() * Math.PI * 2;
      const sp = speed * (0.4 + Math.random() * 0.6);
      list.push({
        x: x, y: y,
        vx: Math.cos(a) * sp, vy: Math.sin(a) * sp - (opts.upBias || 0),
        life: life * (0.7 + Math.random() * 0.6), maxLife: life,
        size: (opts.size || 4) * (0.6 + Math.random() * 0.8),
        color: Array.isArray(color) ? color[Math.floor(Math.random() * color.length)] : color,
        gravity: opts.gravity !== undefined ? opts.gravity : 220,
        shape: opts.shape || "circle",
        rot: Math.random() * Math.PI * 2,
        rotSpeed: (Math.random() - 0.5) * 8
      });
    }
  }

  function spawnConfetti(list, x, y, count) {
    const colors = ["#FF6B9D", "#FFD54F", "#4ECDC4", "#6FCB6A", "#8FA6FF"];
    spawnBurst(list, x, y, {
      count: count || 26, color: colors, speed: 260, life: 1.3,
      size: 7, gravity: 340, shape: "rect", upBias: 260
    });
  }

  function update(list, dt) {
    for (let i = list.length - 1; i >= 0; i--) {
      const p = list[i];
      p.vy += p.gravity * dt;
      p.x += p.vx * dt;
      p.y += p.vy * dt;
      p.rot += p.rotSpeed * dt;
      p.life -= dt;
      if (p.life <= 0) list.splice(i, 1);
    }
  }

  function drawHeart(ctx, x, y, s) {
    ctx.save();
    ctx.translate(x, y);
    ctx.beginPath();
    ctx.moveTo(0, s * 0.3);
    ctx.bezierCurveTo(s * 0.5, -s * 0.4, s * 1.2, s * 0.3, 0, s * 1.1);
    ctx.bezierCurveTo(-s * 1.2, s * 0.3, -s * 0.5, -s * 0.4, 0, s * 0.3);
    ctx.fill();
    ctx.restore();
  }

  function drawStar(ctx, x, y, inner, outer, points) {
    ctx.save();
    ctx.translate(x, y);
    ctx.beginPath();
    for (let i = 0; i < points * 2; i++) {
      const rad = i % 2 === 0 ? outer : inner;
      const a = (Math.PI / points) * i - Math.PI / 2;
      const px = Math.cos(a) * rad, py = Math.sin(a) * rad;
      if (i === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py);
    }
    ctx.closePath();
    ctx.fill();
    ctx.restore();
  }

  function draw(ctx, list) {
    list.forEach(function (p) {
      const t = Math.max(0, p.life / p.maxLife);
      ctx.globalAlpha = t;
      ctx.fillStyle = p.color;
      if (p.shape === "heart") {
        drawHeart(ctx, p.x, p.y, p.size * t + p.size * 0.4);
      } else if (p.shape === "star") {
        drawStar(ctx, p.x, p.y, p.size * 0.5, p.size, 5);
      } else if (p.shape === "rect") {
        ctx.save();
        ctx.translate(p.x, p.y);
        ctx.rotate(p.rot);
        ctx.fillRect(-p.size / 2, -p.size / 3, p.size, p.size * 0.66);
        ctx.restore();
      } else {
        ctx.beginPath();
        ctx.arc(p.x, p.y, p.size * t + p.size * 0.3, 0, Math.PI * 2);
        ctx.fill();
      }
    });
    ctx.globalAlpha = 1;
  }

  return { spawnBurst: spawnBurst, spawnConfetti: spawnConfetti, update: update, draw: draw };
})();
