const TYPES = Object.freeze({
  heat: {
    label: 'HEAT',
    temperature: 10,
    score: 18,
    color: '#ff6a00',
    glow: '#ff2f00',
    symbol: '▲'
  },
  coolant: {
    label: 'COOL',
    temperature: -12,
    score: 22,
    color: '#45e7ff',
    glow: '#008cff',
    symbol: '❄'
  },
  waste: {
    label: 'WASTE',
    temperature: 17,
    score: -35,
    color: '#b7d52a',
    glow: '#668800',
    symbol: '☢'
  },
  emp: {
    label: 'EMP',
    temperature: 28,
    score: -80,
    color: '#a892ff',
    glow: '#3d24ff',
    symbol: '⚡'
  }
});

function weightedType(elapsed) {
  const empChance = Math.min(0.08 + elapsed * 0.0007, 0.16);
  const roll = Math.random();
  if (roll < empChance) return 'emp';
  if (roll < empChance + 0.18) return 'waste';
  if (roll < empChance + 0.57) return 'heat';
  return 'coolant';
}

export class ReactorObject {
  constructor({ width, height, elapsed, lifetime }) {
    this.type = weightedType(elapsed);
    this.data = TYPES[this.type];
    this.radius = 24 + Math.random() * 12;
    this.x = 70 + Math.random() * Math.max(20, width - 140);
    this.y = 135 + Math.random() * Math.max(20, height - 235);
    this.age = 0;
    this.lifetime = lifetime * (0.86 + Math.random() * 0.28);
    this.rotation = Math.random() * Math.PI * 2;
    this.rotationSpeed = (Math.random() - 0.5) * 1.8;
    this.pulseOffset = Math.random() * Math.PI * 2;
    this.resolved = false;
  }

  update(delta) {
    this.age += delta;
    this.rotation += this.rotationSpeed * delta;
  }

  contains(x, y) {
    return Math.hypot(x - this.x, y - this.y) <= this.radius * 1.28;
  }

  get expired() {
    return this.age >= this.lifetime;
  }

  draw(ctx, time) {
    const life = Math.max(0, 1 - this.age / this.lifetime);
    const pulse = 1 + Math.sin(time * 5 + this.pulseOffset) * 0.08;
    const r = this.radius * pulse;

    ctx.save();
    ctx.translate(this.x, this.y);
    ctx.rotate(this.rotation);
    ctx.globalAlpha = Math.min(1, life * 2.8);
    ctx.shadowColor = this.data.glow;
    ctx.shadowBlur = 24;

    const gradient = ctx.createRadialGradient(-r * .25, -r * .25, 3, 0, 0, r);
    gradient.addColorStop(0, '#ffffff');
    gradient.addColorStop(.18, this.data.color);
    gradient.addColorStop(1, '#111015');
    ctx.fillStyle = gradient;
    ctx.beginPath();
    ctx.arc(0, 0, r, 0, Math.PI * 2);
    ctx.fill();

    ctx.shadowBlur = 0;
    ctx.strokeStyle = this.data.color;
    ctx.lineWidth = 3;
    ctx.beginPath();
    ctx.arc(0, 0, r * .78, 0, Math.PI * 2);
    ctx.stroke();

    ctx.rotate(-this.rotation);
    ctx.fillStyle = '#fff';
    ctx.font = `800 ${Math.round(r * .78)}px Segoe UI Symbol, Arial`;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText(this.data.symbol, 0, 1);

    ctx.strokeStyle = `rgba(255,255,255,${.15 + life * .45})`;
    ctx.lineWidth = 3;
    ctx.beginPath();
    ctx.arc(0, 0, r * 1.18, -Math.PI / 2, -Math.PI / 2 + Math.PI * 2 * life);
    ctx.stroke();
    ctx.restore();
  }
}

export function getObjectEffect(object) {
  return object.data;
}
