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

export default class HellbornEnemy extends foundry.abstract.TypeDataModel {

  static defineSchema() {
    const fields = foundry.data.fields
    const requiredInteger = { required: true, nullable: false, integer: true }
    const schema = {}

    schema.enemyType = new fields.StringField({
      required: true,
      initial: "mook",
      choices: SYSTEM.ENEMY_TYPES
    })

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

      }
      return new fields.SchemaField(schema, { label })
    }

    schema.stats = new fields.SchemaField(
      Object.values(SYSTEM.STATS).reduce((obj, stat) => {
        obj[stat.id] = statField(stat.label)
        return obj
      }, {}),
    )
    schema.flavorText = new fields.StringField({ required: true, initial: "", textSearch: true })

    schema.trauma = new fields.SchemaField({
      current: new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 }),
      max: new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 }),
    })

    schema.darkness = new fields.SchemaField({
      current: new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 }),
      max: new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 }),
      value: new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 }),
    })

    schema.defense = new fields.SchemaField({
      base: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      bonus: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      total: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      resistance: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      maleficDR: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      resilience: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
    })

    schema.movement = new fields.SchemaField({
      base: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
      reduce: 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 = ["HELLBORN.Enemy"]

  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 === "trait"),
      hasTarget,
      target: opponentTarget
    })
    if (!roll) return null

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

}
