import { SYSTEM } from "../config/system.mjs"
import { WEAPON_TYPE } from "../config/weapon.mjs"

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

    schema.description = new fields.HTMLField({ required: true, textSearch: true })
    schema.weaponType = new fields.StringField({ required: true, initial: "melee", choices: SYSTEM.WEAPON_TYPE })
    schema.weaponClass = new fields.StringField({ required: true, initial: "shortblade", choices: SYSTEM.WEAPON_CLASS })

    schema.damageType = new fields.SchemaField({
      typeP: new fields.BooleanField(),
      typeB: new fields.BooleanField(),
      typeS: new fields.BooleanField()
    })
    schema.damage = new fields.SchemaField({
      damageS: new fields.StringField({required: true, initial: ""}),
      damageM: new fields.StringField({required: true, initial: ""})
    })
    schema.applyStrengthDamageBonus = new fields.BooleanField({ required: true, initial: true })

    schema.hands = new fields.StringField({ required: true, initial: "1", choices: {"1": "1", "2": "2"} })
    schema.isAgile = new fields.BooleanField({ required: true, initial: false })

    schema.defenseMax = new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 })
    schema.secondsToAttack = new fields.StringField({required: true, initial: ""})
    schema.combatProgressionDice = new fields.StringField({required: true, initial: "d4", choices: SYSTEM.COMBAT_PROGRESSION_DICE})

    schema.speed = new fields.SchemaField({
      simpleAim: new fields.StringField({required: true, initial: ""}),
      carefulAim: new fields.StringField({required: true, initial: ""}),
      focusedAim: new fields.StringField({required: true, initial: ""})
    })

    schema.defense = new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 })
    schema.weaponRange = new fields.SchemaField({
      pointBlank: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 }),
      short: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 }),
      medium: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 }),
      long: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 }),
      extreme: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 }),
      outOfSkill: new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 })
    })

    schema.bonuses = new fields.SchemaField({
      attackBonus: new fields.NumberField({ ...requiredInteger, required: true, initial: 0 }),
      damageBonus: new fields.NumberField({ ...requiredInteger, required: true, initial: 0 }),
      defenseBonus: new fields.NumberField({ ...requiredInteger, required: true, initial: 0 })
    })

    schema.encLoad = new fields.NumberField({ required: true, initial: 0, min: 0 })
    schema.cost = new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 })
    schema.money = new fields.StringField({ required: true, initial: "tinbit", choices: SYSTEM.MONEY })
    schema.equipped = new fields.BooleanField({ required: true, initial: false })

    return schema
  }

  /** @override */
  static LOCALIZATION_PREFIXES = ["LETHALFANTASY.Weapon"]

  get weaponCategory() {
    return game.i18n.localize(WEAPON_TYPE[this.weaponType] || this.weaponType)
  }

}
