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

export default class CthulhuEternalWeapon 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 })

    let setting = game.settings.get("fvtt-cthulhu-eternal", "settings-era") || "modern"
    schema.settings = new fields.StringField({ required: true, initial: setting, choices: SYSTEM.AVAILABLE_SETTINGS })

    schema.weaponType = new fields.StringField({ required: true, initial: "melee", choices: SYSTEM.WEAPON_TYPE })
    schema.hasDirectSkill = new fields.BooleanField({ required: true, initial: false })
    schema.directSkillValue = new fields.NumberField({ required: true, initial: 0, min: 0, max: 99 })

    schema.hasDamageDistance = new fields.BooleanField({ required: true, initial: false })
    schema.damageDistance = new fields.SchemaField(Array.fromRange(6, 1).reduce((damageDistance, i) => {
      damageDistance[`dist${i}`] = new fields.SchemaField({
        damage: new fields.StringField({ required: true, initial: "1d6" }),
        distance: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 })
      })
      return damageDistance
    }, {}));

    schema.hasSelectiveFire = new fields.BooleanField({ required: true, initial: false })
    schema.hasSight = new fields.BooleanField({ required: true, initial: false })
    schema.isStunning = new fields.BooleanField({ required: true, initial: false })
    schema.damage = new fields.StringField({ required: true, initial: "1d6" })
    schema.applyDamageBonus = new fields.BooleanField({ required: true, initial: false })
    schema.baseRange = new fields.StringField({ required: true, initial: "" })
    schema.rangeUnit = new fields.StringField({ required: true, initial: "yard", choices: SYSTEM.WEAPON_RANGE_UNIT })
    schema.lethality = new fields.NumberField({ required: true, initial: 0, min: 0 })
    schema.killRadius = new fields.NumberField({ required: true, initial: 0, min: 0 })
    schema.armorPiercing = new fields.NumberField({ required: true, initial: 0, min: 0 })
    schema.weaponSubtype = new fields.StringField({ required: true, initial: "basicfirearm", choices: SYSTEM.WEAPON_SUBTYPE })
    schema.state = new fields.StringField({ required: true, initial: "pristine", choices: SYSTEM.EQUIPMENT_STATES })
    schema.ammo = new fields.SchemaField({
      value: new fields.NumberField({ ...requiredInteger, initial: 6, min: 0 }),
      max: new fields.NumberField({ ...requiredInteger, initial: 6, min: 0 })
    })

    schema.resourceLevel = new fields.NumberField({ required: true, initial: 0, min: 0 })

    return schema
  }

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

  get weaponCategory() {
    return game.i18n.localize(CATEGORY[this.category].label)
  }

  isRanged() {
    return this.weaponType.match("ranged")
  }

  isFireArm() {
    return this.weaponType === "rangedfirearm"
  }
}
