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

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

    schema.description = new fields.HTMLField({ required: true, textSearch: true })
    schema.notes = new fields.HTMLField({ required: true, textSearch: true })
    schema.name = new fields.StringField({ required: true, nullable: false, initial: "" })

    schema.techAge = new fields.StringField({ required: true, choices: SYSTEM.TECH_AGES, initial: "lateatomic" })
    schema.robotSize = new fields.StringField({ required: true, choices: SYSTEM.ROBOT_SIZES, initial: "medium" })
    schema.durability = new fields.NumberField({ ...requiredInteger, initial: 1, min: 0 })
    schema.protection = new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })
    schema.brain = new fields.StringField({ required: true, nullable: false, initial: "" })
    schema.speed = new fields.StringField({ required: true, nullable: false, initial: "" })

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

    schema.skills = new fields.SchemaField(
      Object.values(SYSTEM.SKILLS).reduce((obj, characteristic) => {
        obj[characteristic.id] = skillField(characteristic.label)
        return obj
      }, {}),
    )

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

    schema.cost = new fields.StringField({ required: true, initial: "0" })

    return schema
  }

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

  prepareDerivedData() {
    super.prepareDerivedData();

    let encMax = this.enc.max // this.durability + (2 * this.skills.physical.value)
    let enc = 0
    for (let i of this.parent.items) {
      if (i.system?.enc) {
        enc += i.system.enc
      }
    }
    if (enc !== this.enc.value) {
      this.enc.value = enc
    }
  }

  isEncumbered() {
    return this.enc.value > this.enc.max
  }

  /** */
  /**
   * 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 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 })
  }
}
