import { RdDItemSigneDraconique } from "../item/signedraconique.js"
import { RollBasicParts } from "./roll-basic-parts.mjs"
import { DIFF, ROLL_TYPE_MEDITATION } from "./roll-constants.mjs"
import { PART_MEDITATION } from "./roll-part-meditation.mjs"
import { RollType } from "./roll-type.mjs"

export class RollTypeMeditation extends RollType {
  get code() { return ROLL_TYPE_MEDITATION }
  get name() { return `Méditation draconique` }

  visible(rollData) { return rollData.active.actor.isHautRevant() }
  title(rollData) {
    const current = rollData.current[PART_MEDITATION]
    const theme = current?.meditation?.system.theme
    return theme ? 'médite sur ' + theme : 'médite'
  }

  onSelect(rollData) {
    this.setDiffType(rollData, DIFF.AUCUN)
  }

  callbacks(rollOptions) { return [RollTypeMeditation.$onRollMeditation] }

  static async $onRollMeditation(rollData) {
    const actor = rollData.active.actor
    const meditation = rollData.current.meditation?.meditation
    const rolled = rollData.rolled
    if (meditation && rolled) {
      if (rolled.isSuccess) {
        await actor.createEmbeddedDocuments('Item', [RdDItemSigneDraconique.prepareSigneDraconiqueMeditation(meditation, rolled)])
      }
      if (rolled.isEPart) {
        await actor.updateEmbeddedDocuments('Item', [{ _id: meditation.id, 'system.malus': meditation.system.malus - 1 }])
      }
      await actor.santeIncDec("fatigue", 2)
    }
  }

  async onMonteeTMR(savedRoll, mode){
    const actor = RollBasicParts.getTokenActor(savedRoll).actor
    return actor?.displayTMR(mode)
  }
}