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

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


    schema.terrain = new fields.StringField({ required: true, nullable: false, initial: "cave", choices: SYSTEM.CREATURE_TERRAIN_TYPES })
    schema.niche = new fields.StringField({ required: true, nullable: false, initial: "prey", choices: SYSTEM.CREATURE_NICHES })
    schema.size = new fields.StringField({ required: true, nullable: false, initial: "small", choices: SYSTEM.CREATURE_SIZES })

    schema.numberAppearing = new fields.StringField({ required: true, initial: "1d6" })
    schema.health = new fields.SchemaField({
      staminaValue: new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 }),
      staminaMax: new fields.NumberField({ ...requiredInteger, initial: 1, min: 1 }),
    })
    schema.damage = new fields.StringField({ required: true, initial: "1d6" })

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

    return schema
  }

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

  isEncumbered() {
    return false
  }

  async roll(rollType, rollItem) {
    let opponentTarget
    const hasTarget = opponentTarget !== undefined

    let roll = await HellbornRoll.prompt({
      rollType,
      rollItem,
      actorId: this.parent.id,
      actorName: this.parent.name,
      actorImage: this.parent.img,
      traits: this.parent.items.filter(i => i.type === "creature-trait" && i.system.isAdvantage),
      abilities: this.parent.items.filter(i => i.type === "creature-ability" && i.system.isAdvantage),
      isEncumbered: this.isEncumbered(),
      hasTarget,
      target: opponentTarget
    })
    if (!roll) return null

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

}
