import { CATEGORIES_COMPETENCES_CREATURES } from "../item/base-items.js"
import { CARACS } from "../rdd-carac.js"
import { RdDPossessionV2 } from "../combat/rdd-possession-v2.mjs"
import { ROLL_TYPE_POSSESSION } from "./roll-constants.mjs"
import { PART_CARAC } from "./roll-part-carac.mjs"
import { PART_COMP } from "./roll-part-comp.mjs"
import { RollPartSelect } from "./roll-part-select.mjs"
import { ROLLDIALOG_SECTION, RollPart } from "./roll-part.mjs"

export const PART_POSSESSION = "possession"

export class RollPartPossession extends RollPartSelect {

  /** TODO: remplacer selectOption par un sélecteur plus sympa (avec image de compétence, par exemple? */

  get code() { return PART_POSSESSION }
  get name() { return 'Possession' }
  get section() { return ROLLDIALOG_SECTION.CHOIX }

  get rollTypes() { return [ROLL_TYPE_POSSESSION] }
  visible(rollData) { return RollPart.isRollType(rollData, ROLL_TYPE_POSSESSION) }

  loadRefs(rollData) {
    const refs = this.getRefs(rollData)
    refs.all = this.$getActorConjurations(rollData)
    this.$selectPossession(rollData)
  }


  choices(refs) { return refs.all }

  $getActorConjurations(rollData) {
    const competences = rollData.active.actor.getBestDraconic({possession: true})
    return (competences ? [competences] : []).map(RollPartPossession.extractPossession)
  }

  static extractPossession(comp) {
    return {
      key: comp.id ?? comp.name,
      label: `${comp.system.categorie == CATEGORIES_COMPETENCES_CREATURES.possession.key ? 'Possession' : 'Conjuration'} (${comp.name})`,
      value: comp.system.niveau,
      comp: comp
    }
  }

  prepareContext(rollData) {
    this.$selectPossession(rollData)
  }

  getAjustements(rollData) {
    return []
  }

  async _onRender(rollDialog, context, options) {
    const rollData = rollDialog.rollData
    const select = rollDialog.element.querySelector(`roll-section[name="${this.code}"] select[name="select-possession"]`)
    const button = rollDialog.element.querySelector(`roll-section[name="${this.code}"] button[name="creer-possession"]`)
    select?.addEventListener("change", e => {
      const selectOptions = e.currentTarget.options
      const index = selectOptions.selectedIndex
      this.$selectPossession(rollData, selectOptions[index]?.value)
      rollDialog.render()
    })
    button?.addEventListener("click", async e => {
      e.preventDefault()
      await RdDPossessionV2.createPossessionIfMissing(rollData.active, rollData.opponent)
      rollDialog.render()
    })
  }

  $selectPossession(rollData, key) {
    this.selectByKey(rollData, key, 0)
  }

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

}
