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

export default class FreeroundsIndicator extends HackAttachment {

  set issued(v) {
    this._issued = v;
    this._updateIndication();
  }

  set left(v) {
    this._left = v;
    this._updateIndication();
  }

  createContent() {
    const label = new AdvancedText([{key: 'free_rounds'}, ":"], {
      fontFamily: "Oxanium, Opensans",
      fill: 0,
      fontWeight: 400,
      fontSize: 12,
      align: "right",
    });
    tryToApplyFallbackFontFamily(label);
    label.anchor.set(1, 0.5);
    label.position.set(this.size.width * 0.5 + 15, this.size.height * 0.5);
    label.scale.set(Math.min(85 / label.width, 1));
    label.resolution = this.texture.resolution;

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

    this.valueText = value;
  }

  _updateIndication() {
    if (this._left === this._issued) {
      this.valueText.text = `${this._issued}`;
      this.valueText.position.set(this.size.width * 0.5 + 34, this.size.height * 0.5);
    } else {
      this.valueText.text = `${this._issued - this._left}/${this._issued}`;
      this.valueText.position.set(this.size.width * 0.5 + 40, this.size.height * 0.5);
    }
  }
}
