import { RDD_CONFIG } from "../constants.js"
import { Misc } from "../misc.js"
import { RdDPossessionV2 } from "../combat/rdd-possession-v2.mjs"
import { ReglesOptionnelles } from "../settings/regles-optionnelles.js"
import { StatusEffects } from "../settings/status-effects.js"
import { ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE, ROLL_TYPE_POSSESSION } from "./roll-constants.mjs"
import { ROLLDIALOG_SECTION, RollPart } from "./roll-part.mjs"

export const PART_SIGN = "sign"

export class RollPartSign extends RollPart {

  get code() { return PART_SIGN }
  get section() { return ROLLDIALOG_SECTION.AJUSTEMENTS }

  loadRefs(rollData) {
    this.setFromState(rollData)
  }

  restore(rollData) {
    this.setCurrent(rollData, this.getSaved(rollData))
  }

  store(rollData, targetData) {
    this.setSaved(targetData, this.getCurrent(rollData))
  }

  isCombat(rollData) {
    return [ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE, ROLL_TYPE_POSSESSION].includes(rollData.type.current)
  }

  prepareContext(rollData) {
    this.setFromState(rollData)
  }

  setFromState(rollData) {
    if (rollData.type.retry) {
      return
    }
    const actor = rollData.active.actor;
    const isCombat = this.isCombat(rollData)
    const current = this.getCurrent(rollData)
    current.armeDisparate = isCombat ? current.armeDisparate : 'norm'
    current.surprise = actor.getSurprise(isCombat, current.forceRequise ?? 0)
    current.reasons = actor.getEffects(it => StatusEffects.niveauSurprise(it, isCombat) > 0, current.forceRequise ?? 0)
      .map(it => { return { img: it.img, label: game.i18n.localize(it.name) } })
    current.diviseur = 1
    if (current.surprise == 'demi') {
      current.diviseur *= 2
    }
    if (isCombat && actor.isDemiReve()) {
      current.reasons.push({ img: RDD_CONFIG.icons.demiReve, label: 'Demi-rêve en combat' })
    }
    if (this.isParadeArmeDisparate(current)) {
      current.diviseur *= 2
      current.reasons.push({ img: RDD_CONFIG.icons.armesDisparates, label: 'Armes disparates' })
    }
    if (this.isAttaqueFinesse(rollData)) {
      current.diviseur *= 2
      current.reasons.push({ img: RDD_CONFIG.particuliere.finesse.img, label: 'Particulière en finesse' })
    }

    if (!ReglesOptionnelles.isUsing('tripleSignificative')) {
      current.diviseur = Math.min(current.diviseur, 4);
    }

    current.reason = current.reasons.map(it => it.label).join(', ')
  }

  isAttaqueFinesse(rollData) {
    const attackerRoll = rollData.attackerRoll
    if (rollData.attackerRoll){
      switch (rollData.type.current) {
        case ROLL_TYPE_DEFENSE:
          return attackerRoll?.particuliere == RDD_CONFIG.particuliere.finesse.key
        case ROLL_TYPE_POSSESSION:
          return RdDPossessionV2.isDefense(rollData.type.possession.action) && attackerRoll?.particuliere == RDD_CONFIG.particuliere.finesse.key
      }
    }
    return false
  }

  isParadeArmeDisparate(current) {
    return current.armeDisparate == 'sign'
  }

  getAjustements(rollData) {
    const current = this.getCurrent(rollData)
    if (current.surprise == 'demi') {
      return [
        {
          label: 'Significative requise ' + Misc.getFractionOneN(current.diviseur)
        },
        ...current.reasons.map(it => {
          return {
            label: it.img
              ? `<img src="${it.img}"> ${it.label}`
              : `<i class="fa-solid fa-triangle-exclamation"></i> ${it.label}`
          }
        })
      ]
    }
    return []
  }

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

    input?.addEventListener("change", e => {
      this.getCurrent(rollDialog.rollData).value = parseInt(e.currentTarget.value)
      rollDialog.render()
    })
  }

  setArme(rollData, armeDisparate, forceRequise) {
    const current = this.getCurrent(rollData)
    current.armeDisparate = armeDisparate
    current.forceRequise = forceRequise
  }
}