import { SYSTEM } from "../config/system.mjs"
import CthulhuEternalRoll from "../documents/roll.mjs"

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

    // Carac
    const characteristicField = (label) => {
      const schema = {
        value: new fields.NumberField({ ...requiredInteger, initial: 3, min: 0 }),
        feature: new fields.StringField({ required: true, nullable: false, initial: "" })
      }
      return new fields.SchemaField(schema, { label })
    }

    schema.characteristics = new fields.SchemaField(
      Object.values(SYSTEM.CHARACTERISTICS).reduce((obj, characteristic) => {
        obj[characteristic.id] = characteristicField(characteristic.label)
        return obj
      }, {}),
    )

    schema.wp = new fields.SchemaField({
      value: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      max: new fields.NumberField({ ...requiredInteger, initial: 3, min: 0 }),
      exhausted: new fields.BooleanField({ required: true, initial: false })
    })

    schema.hp = new fields.SchemaField({
      value: new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 }),
      max: new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 })
    })
    schema.movement = new fields.StringField({ required: true, initial: "" })
    schema.sanLoss = new fields.StringField({ required: true, initial: "1/1D6" })

    schema.armor = new fields.StringField({ required: true, initial: "" })
    schema.size = new fields.StringField({ required: true, initial: "medium", choices: SYSTEM.CREATURE_SIZE })
    schema.damageBonus = new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })

    schema.description = new fields.HTMLField({ required: true, textSearch: true })
    schema.notes = new fields.HTMLField({ required: true, textSearch: true })

    return schema
  }

  /** @override */
  static LOCALIZATION_PREFIXES = ["CTHULHUETERNAL.Creature"]

    /** */
    /**
     * 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 CthulhuEternalRoll.prompt({
        rollType,
        rollItem,
        isLowWP: false,
        isZeroWP: false,
        isExhausted: false,
        actorId: this.parent.id,
        actorName: this.parent.name,
        actorImage: this.parent.img,
        hasTarget,
        target: opponentTarget
      })
      if (!roll) return null
  
      await roll.toMessage({}, { messageMode: roll.options.rollMode })
    }
  
}
