
const competenceTroncs = [["Esquive", "Dague", "Corps à corps"],
["Epée à 1 main", "Epée à 2 mains", "Hache à 1 main", "Hache à 2 mains", "Lance", "Masse à 1 main", "Masse à 2 mains"]];

const competence_xp_par_niveau = [5, 5, 5, 10, 10, 10, 10, 15, 15, 15, 15, 20, 20, 20, 20, 30, 30, 40, 40, 60, 60, 100, 100, 100, 100, 100, 100, 100, 100, 100];

export class RdDItemCompetence extends Item {

  /* -------------------------------------------- */
  static isCompetenceArme(competence) {
    switch (competence.data.categorie) {
      case 'melee':
        return competence.name.toLowerCase() != 'esquive';
      case 'tir':
      case 'lancer':
        return true;
    }
    return false;
  }

  /* -------------------------------------------- */
  static isArmeUneMain(competence) {
    return competence?.name.toLowerCase().includes("1 main");
  }
  static isArme2Main(competence) {
    return competence?.name.toLowerCase().includes("2 main");
  }

  /* -------------------------------------------- */
  static isMalusEncombrementTotal(competence) {
    return competence?.name.toLowerCase().match(/(natation|acrobatie)/);
  }

  /* -------------------------------------------- */
  static setRollDataCreature(rollData) {
    rollData.carac = { "carac_creature": { label: rollData.competence.name, value: rollData.competence.data.carac_value } };
    rollData.competence = duplicate(rollData.competence);
    rollData.competence.data.defaut_carac = "carac_creature";
    rollData.competence.data.categorie = "creature";
    rollData.selectedCarac = rollData.carac.carac_creature
  }
  /* -------------------------------------------- */
  static isTronc(compName) {
    for (let troncList of competenceTroncs) {
      for (let troncName of troncList) {
        if (troncName == compName)
          return troncList;
      }
    }
    return false;
  }
  /* -------------------------------------------- */
  static computeCompetenceXPCost(competence) {
    let minLevel = competence.data.base;
    if (minLevel == competence.data.niveau) return 0;
    if (competence.data.niveau < -10) return 0;

    let xp = 0;
    for (let i = minLevel + 1; i <= competence.data.niveau; i++) {
      xp += competence_xp_par_niveau[i + 10];
      //console.log(i, i+10, competence_xp_par_niveau[i+10]);
    }
    if (competence.data.categorie == 'draconic') {
      xp += competence.data.xp_sort;
    }
    return xp;
  }

  /* -------------------------------------------- */
  static computeCompetenceTroncXP(competences) {
    let xp = 0;
    for (let troncList of competenceTroncs) {
      let minNiveau = 0;
      for (let troncName of troncList) {
        let comp = RdDItemCompetence.findCompetence(competences, troncName);
        if (comp) {
          minNiveau = Math.min(comp.data.niveau, minNiveau);
        }
      }
      minNiveau = Math.max(minNiveau, 0); // Clamp à 0, pour le tronc commun
      let minNiveauXP = competence_xp_par_niveau[minNiveau + 10];
      xp += minNiveauXP;
      for (let troncName of troncList) {
        let comp = RdDItemCompetence.findCompetence(competences, troncName);
        if (comp) {
          xp += competence_xp_par_niveau[comp.data.niveau + 10] - minNiveauXP;
        }
      }
    }
    return xp;
  }
  /* -------------------------------------------- */
  static findCompetence(list, name) {
    name = name.toLowerCase();
    return list.find(item => item.name.toLowerCase() == name && (item.type == "competence" || item.type == "competencecreature"))
  }

  /* -------------------------------------------- */
  static getCompetenceNextXp(niveau) {
    return competence_xp_par_niveau[niveau + 10];
  }

}