import { GoodType } from "../model/goods/good-type.enum";
import { PlanetUi } from "./planet.ui";

export class PlanetStatus {
  private container: Phaser.GameObjects.Container;
  private text: Phaser.GameObjects.Text;

  constructor(
    private scene: Phaser.Scene,
    private planetSprite: PlanetUi,
    private getPopulation: () => number,
    private getGrowthState: () => 'growing' | 'shrinking',
    private getCriticalGoods: () => GoodType[]
  ) {
    this.text = scene.add.text(0, 0, '', {
      fontSize: '12px',
      color: '#ffffff',
      backgroundColor: '#00000088',
      padding: { x: 5, y: 3 },
      align: 'center'
    }).setOrigin(0.5, 1);

    this.container = scene.add.container(planetSprite.getWorldPoint().x, planetSprite.getWorldPoint().y - (planetSprite.height / 2) - 5, [this.text]);
    this.container.setDepth(100); // über Planeten
  }

  update() {
    this.container.setScale(1 / this.scene.cameras.main.zoom)
    const population = Math.floor(this.getPopulation());
    const growth = this.getGrowthState();
    const criticalGoods = this.getCriticalGoods();

    let growthSymbol = '';
    if (growth === 'growing') {
      growthSymbol = '🚀';
    } else if (growth === 'shrinking') {
      growthSymbol = '⬇️';
    }

    let criticalText = '';
    if (criticalGoods.length > 0) {
      criticalText = ' 🛑 ' + criticalGoods.map(g => g.toString().substring(0, 2)).join(',');
    }

    let text = `${this.planetSprite.model.name}`;

    if (this.planetSprite.model.hasHarbour) {
      text += `, 👥 ${population} ${growthSymbol}${criticalText}`
    }

    this.text.setText(text);

    // Position immer über Planet aktualisieren
    this.container.setPosition(this.planetSprite.x, this.planetSprite.y  - (this.planetSprite.height / 2) - 5);
  }

  destroy() {
    this.text.destroy();
    this.container.destroy();
  }
}