import { ENTITE_INCARNE, ENTITE_NONINCARNE } from "../constants.js";
import { TYPES } from "../item.js";
import { Misc } from "../misc.js";
import { RdDEncaisser } from "../rdd-roll-encaisser.js";
import { STATUSES } from "../settings/status-effects.js";
import { RdDBaseActorReve } from "./base-actor-reve.js";

export class RdDEntite extends RdDBaseActorReve {

  static get defaultIcon() {
    return "systems/foundryvtt-reve-de-dragon/icons/entites/darquoine.webp";
  }

  canReceive(item) {
    return item.type == TYPES.competencecreature
  }

  isEntite(typeentite = []) {
    return (typeentite.length == 0 || typeentite.includes(this.system.definition.typeentite));
  }
  isNonIncarnee() { return this.isEntite([ENTITE_NONINCARNE]) }

  getReveActuel() {
    return Misc.toInt(this.system.carac.reve?.value)
  }

  getForce() { return this.getReve() }
  getAgilite() { return this.getReve() }
  getChance() { return this.getReve() }

  getDraconicOuPossession() {
    return this.itemTypes[TYPES.competencecreature]
      .filter(it => it.system.categorie == 'possession')
      .sort(Misc.descending(it => it.system.niveau))
      .find(it => true);
  }

  async remiseANeuf() {
    await this.removeEffects(e => true);
    if (!this.isNonIncarnee()) {
      await this.update({
        'system.sante.endurance.value': this.system.sante.endurance.max
      });
    }
  }

  isDead() {
    return this.isNonIncarnee() ? false : this.system.sante.endurance.value <= 0
  }

  async santeIncDec(name, inc, isCritique = false) {
    if (name == 'endurance' && !this.isNonIncarnee()) {
      const oldValue = this.system.sante.endurance.value;
      const endurance = Math.max(0,
        Math.min(oldValue + inc,
          this.system.sante.endurance.max));
      await this.update({ "system.sante.endurance.value": endurance })
      await this.setEffect(STATUSES.StatusComma, endurance <= 0);
      return {
        perte: oldValue - endurance,
        newValue: endurance
      }
    }
    return {}
  }
  async encaisser() {
    if (this.isNonIncarnee()) {
      return
    }
    await RdDEncaisser.encaisser(this) 
  }

  isEffectAllowed(effectId) {
    return [STATUSES.StatusComma].includes(effectId);
  }

  async onAppliquerJetEncaissement(encaissement, attacker) {
    const perteEndurance = await this.santeIncDec("endurance", -encaissement.endurance);
    mergeObject(encaissement, {
      resteEndurance: perteEndurance.newValue,
      endurance: perteEndurance.perte
    });
  }

  isEntiteAccordee(attacker) {
    if (this.isEntite([ENTITE_INCARNE])) {
      let resonnance = this.system.sante.resonnance
      return (resonnance.actors.find(it => it == attacker.id))
    }
    return true
  }

  /* -------------------------------------------- */
  async setEntiteReveAccordee(attacker) {
    if (this.isEntite([ENTITE_INCARNE])) {
      let resonnance = duplicate(this.system.sante.resonnance);
      if (resonnance.actors.find(it => it == attacker.id)) {
        // déjà accordé
        return;
      }
      resonnance.actors.push(attacker.id);
      await this.update({ "system.sante.resonnance": resonnance });
    }
    else {
      super.setEntiteReveAccordee(attacker)
    }
  }


}
