import { RDD_CONFIG } from "../constants.js"
import { RdDEmpoignade } from "../combat/rdd-empoignade.js"
import { COMBAT_ROLL_TYPES, ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE } from "./roll-constants.mjs"
import { RollPartCheckbox } from "./roll-part-checkbox.mjs"
import { RollPart } from "./roll-part.mjs"

const EMPOIGNADE_TAILLE = "empoignade-taille"

export class RollPartEmpoignadeTaille extends RollPartCheckbox {

  get code() { return EMPOIGNADE_TAILLE }

  get rollTypes() { return [ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE] }
  isValid(rollData) {
    return RdDEmpoignade.isCombatantEmpoignade(rollData.ids.actorId, rollData.ids.actorTokenId)
  }

  visible(rollData) {
    return COMBAT_ROLL_TYPES.includes(rollData.type.current) &&
      RdDEmpoignade.isEmpoignadeEnCours(rollData.active.actor) &&
      this.getTailleDiff(rollData) != 0
  }

  getTailleDiff(rollData) {
    const taille = rollData.active.actor.getTaille()
    const tailleOpponent = rollData.opponent.actor.getTaille()
    const diff = taille - tailleOpponent
    const diffTailleAbs = Math.max(0, Math.abs(diff) - 1)
    const signDiff = Math.sign(diff)
    return signDiff * diffTailleAbs
  }

  getCheckboxIcon(rollData) { return `<img src="${RDD_CONFIG.icons.empoignade}">` }
  getCheckboxLabel(rollData) {
    return `Taille ${rollData.active.actor.getTaille()} vs ${rollData.opponent.actor.getTaille()} `
  }
  getCheckboxValue(rollData) {
    return this.getTailleDiff(rollData)
  }
}
