import { STRING, HTMLSTRING } from "../common/field-types.mjs";
import { statField, santeField, attributField, compteurField } from "./_actor-helpers.mjs";

const fields = foundry.data.fields;

export default class RdDModelEntite extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    return {
      definition: new fields.SchemaField({
        categorieentite: new fields.StringField({ initial: "", ...STRING }),
        typeentite: new fields.StringField({ initial: "", ...STRING }),
      }),
      carac: new fields.SchemaField({
        taille: statField(10, "Taille"),
        reve: statField(10, "Rêve"),
        niveau: statField(0, "Niveau"),
      }),
      sante: new fields.SchemaField({
        endurance: santeField(10, 10, "Endurance"),
        resonnance: new fields.SchemaField({
          actors: new fields.ArrayField(
            new fields.StringField({ initial: "", ...STRING }),
            { initial: [], required: true, nullable: false }
          ),
        }),
      }),
      compteurs: new fields.SchemaField({
        etat: compteurField(0, "Etat général"),
      }),
      attributs: new fields.SchemaField({
        plusdom: attributField(0, "+dom", true),
        vitesse: attributField(0, "Vitesse", false, true),
        protection: attributField(0, "Protection naturelle", false),
      }),
      description: new fields.HTMLField({ initial: "", ...HTMLSTRING }),
      race: new fields.StringField({ initial: "", ...STRING }),
      notesmj: new fields.HTMLField({ initial: "", ...HTMLSTRING }),
    };
  }

  prepareDerivedData() {
    const actor = this.parent;

    this.attributs.plusdom.value = actor.getBonusDegat();
    this.sante.endurance.max = actor.getEnduranceMax();
    this.sante.endurance.value = Math.min(this.sante.endurance.value, this.sante.endurance.max);
  }
}
