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

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

    schema.agility = new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })
    schema.armor = new fields.StringField({ required: true, initial: "" })
    schema.armorCritical = new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })
    schema.cargo = new fields.StringField({ required: true, initial: "" })
    schema.crewList = new fields.ArrayField(new fields.StringField({ required: true }), { initial: [] })
    schema.crewCapacity = new fields.StringField({ required: true, initial: "" })
    schema.npcCrew = new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })
    schema.force = new fields.NumberField({ ...requiredInteger, initial: 1, min: 1 })
    schema.range = new fields.StringField({ required: true, initial: "1d6" })
    schema.speed = new fields.StringField({ required: true, initial: "1d6" })
    schema.techAge = new fields.StringField({ required: true, choices: SYSTEM.TECH_AGES, initial: "lateatomic" })
    schema.tonnage = new fields.StringField({ required: true, initial: "" })
    schema.damages = new fields.StringField({ required: true, initial: "" })

    schema.cost = new fields.StringField({ required: true, initial: "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 = ["FTLNOMAD.Vehicle"]

  isEncumbered() {
    return false
  }

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

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

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

}
