import { RDD_CONFIG } from "../constants.js"
import { ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE } from "./roll-constants.mjs"
import { PART_ATTAQUE } from "./roll-part-attaque.mjs"
import { RollPartCheckbox } from "./roll-part-checkbox.mjs"
import { PART_DEFENSE } from "./roll-part-defense.mjs"
import { RollPart } from "./roll-part.mjs"

const ECAILLES = "ecailles"

export class RollPartEcailles extends RollPartCheckbox {

  get code() { return ECAILLES }

  get rollTypes() { return [ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE] }
  isValid(rollData) {
    return RollPart.isRollType(rollData, ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE)
  }

  visible(rollData) {
    return this.getEcailles(rollData) != 0

  }

  getEcailles(rollData) {
    const arme = this.getArme(rollData)
    return arme?.system.magique ? arme?.system.ecaille_efficacite : 0
  }

  getArme(rollData) {
    return RollPart.isRollType(rollData, ROLL_TYPE_ATTAQUE)
      ? rollData.current[PART_ATTAQUE]?.arme
      : RollPart.isRollType(rollData, ROLL_TYPE_DEFENSE)
        ? rollData.current[PART_DEFENSE]?.arme
        : undefined
  }

  prepareContext(rollData) {
    this.loadRefs(rollData)
    super.prepareContext(rollData)
  }

  getCheckboxIcon(rollData) {
    return this.getEcailles(rollData) > 0
      ? `<img src="${RDD_CONFIG.icons.magique}">`
      : `<img src="${RDD_CONFIG.icons.armebrisee}">`
  }

  getCheckboxLabel(rollData) {
    return this.getEcailles(rollData) > 0
      ? "Efficacité"
      : "Défauts"
  }

  getCheckboxValue(rollData) { return this.getEcailles(rollData) }
}
