import { ITEM_TYPES } from "../constants.js"
import { CARACS } from "../rdd-carac.js"
import { ROLL_TYPE_JEU, RollConstants } from "./roll-constants.mjs"
import { PART_CARAC } from "./roll-part-carac.mjs"
import { PART_COMP, RollPartComp } from "./roll-part-comp.mjs"
import { RollPartSelect } from "./roll-part-select.mjs"
import { ROLLDIALOG_SECTION, RollPart } from "./roll-part.mjs"
import { RdDItem } from "../item.js"
import { Misc } from "../misc.js"

export const PART_JEU = "jeu"

export const COMPETENCE_JEU = 'Jeu'

export class RollPartJeu extends RollPartSelect {

  get code() { return PART_JEU }
  get section() { return ROLLDIALOG_SECTION.CHOIX }

  get rollTypes() { return [ROLL_TYPE_JEU] }
  isValid(rollData) {
    return rollData.active.actor.isPersonnage()
      && RollConstants.findCompetence(rollData, 'Jeu')
  }

  visible(rollData) { return RollPart.isRollType(rollData, ROLL_TYPE_JEU) }

  loadRefs(rollData) {
    const refs = this.getRefs(rollData)
    const compJeu = RollConstants.findCompetence(rollData, 'Jeu')
    if (!compJeu) {
      refs.jeux = []
      return
    }
    refs.jeux = rollData.active.actor.itemTypes[ITEM_TYPES.jeu]
      .map(it => RollPartJeu.$extractJeu(it, compJeu))

    if (refs.jeux.length > 0) {
      this.$selectJeu(rollData)
    }
  }

  choices(refs) { return refs.jeux }

  static $extractJeu(jeu, compJeu) {
    const caracs = jeu.system.caraccomp.toLowerCase().split(/[.,:\/-]/).map(it => it.trim())
    const base = RollPartJeu.$getJeuBase(jeu, compJeu, caracs)
    return {
      key: jeu.id,
      label: jeu.name,
      caracs: caracs,
      jeu: jeu,
      value: 0,
      base: base,
      comp: compJeu
    }
  }

  static $getJeuBase(jeu, compJeu, caracs) {
    if (jeu.system.base < compJeu.system.niveau) {
      return undefined
    }
    return new RdDItem({
      id: compJeu.id,
      name: `Jeu ${Misc.lowerFirst(jeu.name)}`,
      type: ITEM_TYPES.competence,
      img: compJeu.img,
      system: foundry.utils.mergeObject(
        {
          niveau: jeu.system.base,
          base: jeu.system.base,
          default_carac: caracs.length > 0 ? caracs[0] : CARACS.CHANCE
        },
        compJeu.system,
        { overwrite: false }
      )
    })
  }

  prepareContext(rollData) {
    const current = this.getCurrent(rollData)
    if (rollData.type.current == ROLL_TYPE_JEU && current) {
      rollData.type.opposed = true
    }
  }

  getAjustements(rollData) { return [] }

  $selectJeu(rollData, key) {
    this.selectByKey(rollData, key, 0)
    if (rollData.type.current == ROLL_TYPE_JEU) {
      RollPartJeu.forceCompJeu(rollData)
    }
  }

  static forceCompJeu(rollData) {
    const jeu = rollData.current[PART_JEU].base ?? rollData.current[PART_JEU].comp
    if (jeu) {
      rollData.refs[PART_COMP].comps = [jeu].map(it => RollPartComp.extractComp(it))
    }
    else {
      rollData.refs[PART_COMP].comps = rollData.refs[PART_COMP].all.filter(comp => comp.label == 'Jeu')
    }
    rollData.current[PART_COMP] = rollData.refs[PART_COMP].comps[0]
  }

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

    selectjeu?.addEventListener("change", e => {
      const selectOptions = e.currentTarget.options
      const index = selectOptions.selectedIndex
      this.$selectJeu(rollDialog.rollData, selectOptions[index]?.value)
      rollDialog.render()
    })
  }

  impactOtherPart(part, rollData) {
    if (this.visible(rollData)) {
      const current = this.getCurrent(rollData)
      switch (part.code) {
        case PART_CARAC: return part.filterCaracs(rollData, current.caracs)
        case PART_COMP: return part.filterComps(rollData, [current.comp?.name])
      }
    }
    return undefined
  }

  getSpecialComp(rollData) {
    const current = this.getCurrent(rollData)
    return current.base ? [current.base] : []
  }

}
