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

const fields = foundry.data.fields;

export default class RdDModelGemme extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    return Object.assign({},
      TEMPLATE_DESCRIPTION.fields(),
      TEMPLATE_INVENTAIRE.fields(),
      {
        type: new fields.StringField({ label: "Type", initial: "", ...STRING }),
        taille: new fields.NumberField({ label: "Taille", initial: 0, ...INTEGER }),
        purete: new fields.NumberField({ label: "Pureté", initial: 0, integer: true, required: true, nullable: false, min: 0, max: 7 }),
        inertie: new fields.NumberField({ label: "Inertie", initial: 0, ...INTEGER }),
        enchantabilite: new fields.NumberField({ label: "Enchantabilité", initial: 0, ...INTEGER }),
        magique: new fields.BooleanField({ label: "Magique", initial: false }),
        pr: new fields.NumberField({ label: "Points de rêve", initial: 0, ...INTEGER }),
        purifie: new fields.BooleanField({ label: "Purifiée", initial: false }),
        prpermanent: new fields.BooleanField({ label: "PR permanent", initial: false }),
        prdate: new fields.NumberField({ label: "Date d'enchantement", initial: 0, integer: true }),
        temporel: new fields.SchemaField({
          debut: new fields.SchemaField({
            indexDate: new fields.NumberField({ initial: -1, integer: true }),
            indexMinute: new fields.NumberField({ initial: 0, integer: true }),
          }),
          fin: new fields.SchemaField({
            indexDate: new fields.NumberField({ initial: -1, integer: true }),
            indexMinute: new fields.NumberField({ initial: 0, integer: true }),
          }),
        }),
      }
    )
  }

  prepareDerivedData() {
    const taille = this.taille
    const purete = this.purete
    const qualite = this.qualite
    this.inertie = 7 - purete
    this.enchantabilite = Math.max(taille - this.inertie, 0)
    this.cout = (taille * purete) + qualite
  }
}
