import { Text } from "pixi.js";
import Locator from "../../services/Locator";
import { capitalizeFirstLetter, tryToApplyFallbackFontFamily } from "../../utils/utils";
import HackAttachment from "../HackAttachment.js";
import AdvancedText from "../../utils/AdvancedText";

export default class BalanceIndicator extends HackAttachment {

  set value(v) {
    this._value = v;
    this.valueText.text = v;
  }

  get value() {
    return this._value;
  }

  createContent() {
    const label = new AdvancedText({key: "balance"}, {
      fontFamily: "Oxanium, Opensans",
      fill: 0xffffff,
      fontWeight: 400,
      fontSize: 12,
      align: "center",
    });
    tryToApplyFallbackFontFamily(label);
    label.anchor.set(0.5, 1);
    label.position.set(this.size.width * 0.5, this.size.height * 0.5);
    label.resolution = this.texture.resolution;

    const value = new Text("0", {
      fontFamily: "Oxanium, Opensans",
      fill: 0xffffff,
      fontWeight: "bold",
      fontSize: 14,
      align: "center",
    });
    tryToApplyFallbackFontFamily(value);
    value.anchor.set(0.5, 0);
    value.position.set(this.size.width * 0.5, this.size.height * 0.5);
    value.resolution = this.texture.resolution;

    this.container.addChild(label, value);
    this.container.position.x += 5

    this.valueText = value;
  }

  update() {
    this.scaleTextIfWidthReachMaxLenght();
    super.update();
  }

  scaleTextIfWidthReachMaxLenght() {
    if (this.valueText.width > this.texture.width) {
      const scaleFactor = this.texture.width / this.valueText.width / 1.2;
      this.valueText.scale.set(scaleFactor);
    }
  }
}
