import { SYSTEM } from "../config/system.mjs"
import HamalronTirageTarot from "../documents/tirage-tarot.mjs"

export default class HamalronPersonnage extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    const fields = foundry.data.fields
    const requiredInteger = { required: true, nullable: false, integer: true }
    const schema = {}


    // Stats
    const symboleCarte = (label) => {
      const schema = {
        label: new fields.StringField({ required: true, initial: label }),
        value: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
        max: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),

      }
      return new fields.SchemaField(schema, { label })
    }
    schema.cartesSucces = new fields.SchemaField(
      Object.values(SYSTEM.TAROT_SYMBOLES).reduce((obj, stat) => {
        obj[stat.id] = symboleCarte(stat.label)
        return obj
      }, {}),
    )

    schema.resistances = new fields.SchemaField(
      Object.values(SYSTEM.TAROT_SYMBOLES).reduce((obj, stat) => {
        obj[stat.id] = symboleCarte(stat.label)
        return obj
      }, {}),
    )

    schema.historial = new fields.HTMLField({ required: true, textSearch: true })
    schema.progression = new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })

    schema.biodata = new fields.SchemaField({
      age: new fields.StringField({ required: true, nullable: false, initial: "" }),
      gender: new fields.StringField({ required: true, nullable: false, initial: "" }),
      height: new fields.StringField({ required: true, nullable: false, initial: "" }),
      eyes: new fields.StringField({ required: true, nullable: false, initial: "" }),
      birthplace: new fields.StringField({ required: true, nullable: false, initial: "" }),
      hair: new fields.StringField({ required: true, nullable: false, initial: "" }),
      home: new fields.StringField({ required: true, nullable: false, initial: "" }),
      weight: new fields.StringField({ required: true, nullable: false, initial: "" }),
    })

    return schema
  }

  /** @override */
  static LOCALIZATION_PREFIXES = ["HAMALRON.Personnage"]

  prepareDerivedData() {
    super.prepareDerivedData();

  }

  /** */
  /**
   * Rolls a dice for a character.
   * @param {("save"|"resource|damage")} rollType The type of the roll.
   * @param {number} rollItem The target value for the roll. Which caracteristic or resource. If the roll is a damage roll, this is the id of the item.
   * @returns {Promise<null>} - A promise that resolves to null if the roll is cancelled.
   */
  async roll(rollType, rollItem) {
    let opponentTarget
    const hasTarget = opponentTarget !== undefined

    let roll = await HamalronTirageTarot.prompt({
      rollType,
      rollItem,
      actorId: this.parent.id,
      actorName: this.parent.name,
      actorImage: this.parent.img,
      hasTarget,
      target: opponentTarget
    })
    if (!roll) return null

    await roll.toMessage({}, { rollMode: roll.options.rollMode })
  }
}
