import { SYSTEM_RDD } from "../constants.js";
import { Draconique } from "./draconique.js";
import { PixiTMR } from "./pixi-tmr.js";

const IMAGE_CARTE_TMR = 'image-carte-tmr';
const TMR_V1 = "systems/foundryvtt-reve-de-dragon/assets/ui/tmr-v1.webp";
const TMR_V2 = "systems/foundryvtt-reve-de-dragon/assets/ui/tmr-v2.webp";
const TMR_V3_COULEUR = "systems/foundryvtt-reve-de-dragon/assets/ui/tmr-v3-couleur.webp";
const TMR_MAPS = [TMR_V3_COULEUR, TMR_V2, TMR_V1];

export class CarteTmr extends Draconique {

  static initSettings() {
    game.settings.register(SYSTEM_RDD, IMAGE_CARTE_TMR,
      {
        name: 'Carte des TMR',
        hint: "Choix de l'image de la carte des TMR",
        scope: "client",
        requiresReload: true,
        config: true,
        choices: {
          [TMR_V3_COULEUR]: "TMR Scriptarium v3 couleur",
          [TMR_V2]: "TMR Multisim v2",
          [TMR_V1]: "TMR NEF v1",
        },
        default: TMR_V3_COULEUR,
        type: String
      })
  }

  type() { return '' }
  match(item) { return false; }
  manualMessage() { return false }
  async onActorCreateOwned(actor, item) { }

  code() { return 'tmr' }
  img() { return game.settings.get(SYSTEM_RDD, IMAGE_CARTE_TMR) }

  static registerAllMaps() {
    TMR_MAPS.forEach((path, i) => PixiTMR.register(`tmr_${i}`, path))
  }

  imgForActor(actor) {
    return actor?.system?.reve?.tmrcarte || this.img()
  }

  createSprite(pixiTMR, actor) {
    const img = this.imgForActor(actor)
    const sprite = new PIXI.Sprite(PIXI.utils.TextureCache[img]);
    // Setup the position of the TMR
    sprite.x = pixiTMR.pixiApp.screen.x;
    sprite.y = pixiTMR.pixiApp.screen.y;
    sprite.width = pixiTMR.pixiApp.screen.width;
    sprite.height = pixiTMR.pixiApp.screen.height;
    // Rotate around the center
    sprite.anchor.set(0);
    sprite.buttonMode = true;
    sprite.tmrObject = pixiTMR;
    return sprite;
  }


}
