/**
 * Data model pour les armes
 */
export default class ArmeDataModel extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    const fields = foundry.data.fields;
    return {
      description: new fields.HTMLField({ initial: "" }),
      rarete: new fields.NumberField({ initial: 0, integer: true }),
      quantite: new fields.NumberField({ initial: 0, integer: true }),
      prix: new fields.NumberField({ initial: 0, integer: true }),
      equipped: new fields.BooleanField({ initial: false }),
      categorie: new fields.StringField({ initial: "trait" }),
      armetype: new fields.StringField({ initial: "trait" }),
      degats: new fields.NumberField({ initial: 0, integer: true }),
      precision: new fields.NumberField({ initial: 0, integer: true }),
      cadence: new fields.StringField({ initial: "" }),
      enraiement: new fields.StringField({ initial: "" }),
      magasin: new fields.NumberField({ initial: 0, integer: true }),
      charge: new fields.NumberField({ initial: 0, integer: true }),
      portee: new fields.StringField({ initial: "" }),
      legalite: new fields.StringField({ initial: "libre" }),
      dissimulation: new fields.StringField({ initial: "tresfacile" }),
      zone: new fields.NumberField({ initial: 0, integer: true }),
      temps: new fields.StringField({ initial: "" }),
      allumage: new fields.StringField({ initial: "" }),
      special: new fields.StringField({ initial: "" })
    };
  }

  static migrateData(data) {
    for (const key of ["rarete", "quantite", "prix", "degats", "precision", "magasin", "charge", "zone"]) {
      if (typeof data[key] === "string") {
        const v = parseInt(data[key]);
        data[key] = Number.isNaN(v) ? 0 : v;
      }
    }
    return data;
  }
}
