
import { ITEM_TYPES } from "./constants.js";
import { Grammar } from "./grammar.js";
import { RdDCombatManager } from "./rdd-combat.js";

export const CATEGORIES_COMPETENCES_CREATURES = {
  "generale": { base: 0, label: "Générale" },
  "naturelle": { base: 0, label: "Arme naturelle" },
  "melee": { base: 0, label: "Mêlée" },
  "parade": { base: 0, label: "Parade" },
  "tir": { base: 0, label: "Tir" },
  "lancer": { base: 0, label: "Lancer" },
  "possession": { base: 0, label: "Possession" },
}

/* -------------------------------------------- */
export class RdDItemCompetenceCreature extends Item {

  /* -------------------------------------------- */
  static setRollDataCreature(rollData) {
    const code = Grammar.toLowerCaseNoAccentNoSpace(rollData.competence.name);
    const selectedCarac = { code: code, label: rollData.competence.name, value: rollData.competence.system.carac_value };
    rollData.carac = { [code]: selectedCarac }
    rollData.competence.system.defaut_carac = code
    rollData.selectedCarac = selectedCarac
    rollData.arme = RdDItemCompetenceCreature.armeCreature(rollData.competence);
  }

  /* -------------------------------------------- */
  static armeCreature(item) {
    const categorieAttaque = RdDItemCompetenceCreature.getCategorieAttaque(item)
    if (categorieAttaque != undefined) {
      // cloner pour ne pas modifier la compétence
      return foundry.utils.mergeObject(item, {
        action: item.isCompetencePossession() ? 'possession' : 'attaque',
        system: {
          competence: item.name,
          cac: categorieAttaque == "naturelle" ? "naturelle" : "",
          niveau: item.system.niveau,
          initiative: RdDCombatManager.calculInitiative(item.system.niveau, item.system.carac_value),
          equipe: true,
          resistance: 100,
          dommagesReels: item.system.dommages,
          penetration: 0,
          force: 0,
          rapide: true,
        }
      }, { inplace: false, });
    }
    return undefined;
  }

  /* -------------------------------------------- */
  static isAttaque(item) {
    return RdDItemCompetenceCreature.getCategorieAttaque(item) != undefined
  }

  static getCategorieAttaque(item) {
    if (item.type == ITEM_TYPES.competencecreature) {
      switch (item.system.categorie) {
        case "melee":
        case "tir":
        case "lancer":
        case "naturelle":
        case "possession":
          return item.system.categorie
      }
    }
    return undefined
  }

  static isDommages(item) {
    if (item.type == ITEM_TYPES.competencecreature) {
      switch (item.system.categorie) {
        case "melee":
        case "tir":
        case "lancer":
        case "naturelle":
          return true
      }
    }
    return false
  }

  static isParade(item) {
    if (item.type == ITEM_TYPES.competencecreature) {
      return item.system.categorie_parade || item.system.isparade
    }
    return false
  }

}
