import { Misc } from "../misc.js"
import { ROLLDIALOG_SECTION, RollPart } from "./roll-part.mjs"

export const PART_TRICHER = "tricher"

export class RollPartTricher extends RollPart {

  get code() { return PART_TRICHER }
  get section() { return ROLLDIALOG_SECTION.CONDITIONS }

  visible(rollData) { return game.user.isGM }

  prepareContext(rollData) {
    const current = this.getCurrent(rollData)
    current.resultat = Misc.inRange(current.resultat == undefined ? -1 : current.resultat, -1, 100)
  }

  async _onRender(rollDialog, context, options) {
    const input = rollDialog.element.querySelector(`roll-section[name="${this.code}"] input[name="${this.code}"]`)

    input?.addEventListener("input", e => this.onInputChange(e, rollDialog))
  }

  onInputChange(event, rollDialog) {
    this.getCurrent(rollDialog.rollData).resultat = parseInt(event.currentTarget.value)
  }
}
