export class HealthBar {
  constructor(scene, x, y, width, height, maxHP) {
    this.scene = scene;
    this.maxHP = maxHP;
    this.currentHP = maxHP;
    this.width = width;
    this.height = height;

    this.bg = scene.add.rectangle(x, y, width, height, 0x333333).setOrigin(0, 0.5);
    this.bar = scene.add.rectangle(x, y, width, height, 0x22cc44).setOrigin(0, 0.5);
    this.text = scene.add.text(x + width / 2, y, `${maxHP}/${maxHP}`, {
      fontSize: '10px', color: '#ffffff'
    }).setOrigin(0.5, 0.5);
  }

  update(currentHP, maxHP) {
    this.currentHP = currentHP;
    if (maxHP !== undefined) this.maxHP = maxHP;
    const pct = Math.max(0, this.currentHP / this.maxHP);
    const color = pct > 0.5 ? 0x22cc44 : pct > 0.25 ? 0xffaa00 : 0xcc2222;
    this.bar.setSize(this.width * pct, this.height);
    this.bar.setFillStyle(color);
    this.text.setText(`${Math.max(0, Math.floor(this.currentHP))}/${this.maxHP}`);
  }

  setDepth(d) {
    this.bg.setDepth(d);
    this.bar.setDepth(d);
    this.text.setDepth(d);
    return this;
  }

  destroy() {
    this.bg.destroy();
    this.bar.destroy();
    this.text.destroy();
  }

  setVisible(v) {
    this.bg.setVisible(v);
    this.bar.setVisible(v);
    this.text.setVisible(v);
    return this;
  }
}
