import HackAttachment from "../HackAttachment.js";
import {Text} from "pixi.js";
import { tryToApplyFallbackFontFamily } from "../../utils/utils";


export default class AutospinsIndicator extends HackAttachment {
  #value = 0;
  createContent() {
    const value = new Text("0", {
      fontFamily: "Oxanium, Opensans",
      fill: 0xffffff,
      fontSize: 16,
      align: "center",
    });
    tryToApplyFallbackFontFamily(value);
    value.anchor.set(0.5, 0.5);
    value.position.set(this.size.width * 0.5, this.size.height * 0.5);
    value.resolution = this.texture.resolution;
    this.container.addChild(value);
    this.valueText = value;
  }

  set left(v) {
    if (!this.slot) return;
    this.#value = v;
    if (v === Infinity) {
      this.slot.setAttachment(this.attachments["autospin_infinity"]);
      return;
    }
    this.slot.setAttachment(this.attachments["placeholder"]);
    this.valueText.text = v;
  }

  attachTo(spine, slotName) {
    super.attachTo(spine, slotName);
    const slotIndex = spine.skeleton.findSlotIndex(slotName);
    this.slot = spine.skeleton.findSlot(slotName);
    this.attachments = spine.spineData.defaultSkin.attachments[slotIndex];
  }

  resize() {
    if (this.#value === "infinity") {
      this.slot.setAttachment(this.attachments["autospin_infinity"]);
      return;
    }
    this.slot.setAttachment(this.attachments["placeholder"]);
  }
}
