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

export default class FTLNomadStarship 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.hullType = new fields.StringField({ required: true, initial: "small", choices: SYSTEM.STARSHIP_HULL })
    schema.endurance = new fields.StringField({ required: true, initial: "" })
    schema.armor = new fields.StringField({ required: true, initial: "" })
    schema.armorCritical = new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })
    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.cargo = new fields.StringField({ required: true, initial: "" })
    schema.guns = new fields.StringField({ required: true, initial: "1d6" })
    schema.travelMultiplier = new fields.StringField({ required: true, initial: "" })
    schema.cost = new fields.StringField({ required: true, initial: "0" })
    schema.monthlyCost = new fields.StringField({ required: true, initial: "0" })
    schema.damages = new fields.StringField({ required: true, initial: "" })
    schema.techAge = new fields.StringField({ required: true, choices: SYSTEM.TECH_AGES, initial: "lateatomic" })

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

    return schema
  }

  /** @override */
  static LOCALIZATION_PREFIXES = ["FTLNOMAD.Starship"]

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

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


}
