import { TEMPLATE_DESCRIPTION } from "../common/_module.mjs";
import { TEMPLATE_TEMPOREL } from "../common/_module.mjs";
import { INTEGER, INTEGER_SIGNED, STRING } from "../common/field-types.mjs";

const fields = foundry.data.fields;

export default class RdDModelBlessure extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    return Object.assign({},
      TEMPLATE_DESCRIPTION.fields(),
      TEMPLATE_TEMPOREL.fields(),
      {
        gravite: new fields.NumberField({ label: "Gravité", initial: 0, ...INTEGER }),
        difficulte: new fields.NumberField({ label: "Difficulté des soins", initial: 0, ...INTEGER_SIGNED }),
        label: new fields.StringField({ label: "Label", initial: "", ...STRING }),
        localisation: new fields.StringField({ label: "Localisation", initial: "", ...STRING }),
        origine: new fields.StringField({ label: "Origine", initial: "", ...STRING }),
        vie: new fields.NumberField({ label: "Perte de Vie", initial: 0, ...INTEGER_SIGNED }),
        endurance: new fields.StringField({ label: "Perte d'Endurance", initial: "", ...STRING }),
        premierssoins: new fields.SchemaField({
          tache: new fields.NumberField({ initial: 0, ...INTEGER }),
          done: new fields.BooleanField({ initial: false }),
          bonus: new fields.NumberField({ initial: 0, ...INTEGER }),
        }),
        soinscomplets: new fields.SchemaField({
          done: new fields.BooleanField({ initial: false }),
          bonus: new fields.NumberField({ initial: 0, ...INTEGER }),
        }),
      }
    )
  }

  prepareDerivedData() {
    const gravite = this.gravite
    const definitions = [
      { gravite: 0, label: 'Contusion/éraflure' },
      { gravite: 2, label: 'Légère' },
      { gravite: 4, label: 'Grave' },
      { gravite: 6, label: 'Critique' },
      { gravite: 8, label: 'Mort' },
    ]
    this.label = definitions.find(d => d.gravite >= gravite)?.label ?? 'Inconnue'
  }
}
