import { Grammar } from "../grammar.js";
import { Misc } from "../misc.js";
import { RdDDice } from "../rdd-dice.js";
import { TMRUtility, TMRType } from "../tmr-utility.js";
import { tmrTokenZIndex } from "../tmr-constants.js";
import { Draconique } from "./draconique.js";
import { TMRAnimations } from "./animation.js";

export class Desorientation extends Draconique {

  type() { return 'souffle' }
  match(item) { return Draconique.isSouffleDragon(item) && Grammar.toLowerCaseNoAccent(item.name).includes('desorientation'); }
  manualMessage() { return false }

  async onActorCreateOwned(actor, souffle) {
    const type = await RdDDice.rollOneOf(this._typesPossibles(actor));
    console.log("désorientation", type);
    souffle.name += ": " + TMRType[type].name;
    await this._creerCasesTmr(actor, type, souffle);
  }

  _typesPossibles(actor) {
    const dejaDesorientes = Misc.distinct(actor.items.filter(it => this.isCase(it)).map(it => it.type));
    return Object.keys(TMRType).filter(it => !dejaDesorientes.includes(it));
  }

  code() { return 'desorientation' }
  tooltip(linkData) { return `Désorientation, cette case n'existe plus !` }
  img() { return 'systems/foundryvtt-reve-de-dragon/icons/tmr/desorientation.svg' }

  createSprite(pixiTMR) {
    return TMRAnimations.withAnimation(
      pixiTMR.sprite(this.code(), {
        zIndex: tmrTokenZIndex.trounoir,
        taille: () => pixiTMR.sizes.full,
      }),
      pixiTMR,
      TMRAnimations.rotation({
        frequence: delta => 2^(2 + Math.random() * 12) * 70,
        angle: delta => (Math.floor(Math.random() * 2) - 1) * 30
      })
    )
  }

  async _creerCasesTmr(actor, type, souffle) {
    const existants = actor.items.filter(it => this.isCase(it)).map(it => it.system.coord);
    let tmrs = TMRUtility.filterTMR(it => it.type == type && !existants.includes(it.coord));
    for (let tmr of tmrs) {
      await this.createCaseTmr(actor, 'Désorientation', tmr, souffle.id);
    }
  }


}