import { ITEM_TYPES } from "../constants.js"
import { ROLL_TYPE_TACHE } 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_TACHE = "tache"

export class RollPartTache extends RollPartSelect {

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

  get rollTypes() { return [ROLL_TYPE_TACHE] }
  isValid(rollData) { return rollData.active.actor.isPersonnage() }
  visible(rollData) { return RollPart.isRollType(rollData, ROLL_TYPE_TACHE) }

  loadRefs(rollData) {
    const refs = this.getRefs(rollData)
    const selected = this.getSelected(rollData)
    refs.forced = selected.forced
    refs.all = rollData.active.actor.itemTypes[ITEM_TYPES.tache]
      .filter(tache => !selected.forced || tache.id == selected.key)
      .map(tache => RollPartTache.$extractTache(tache, rollData.active.actor))
      .filter(extract => !extract.finished)

    refs.taches = refs.all
    if (refs.taches.length > 0) {
      this.$selectTache(rollData)
    }
  }

  choices(refs) { return refs.taches }

  static $extractTache(tache, actor) {
    const tacheOuCommune = tache.getTacheOuCommune()
    return {
      key: tache.id,
      label: tache.name,
      tacheCommune: tache.system.tacheCommune,
      value: tacheOuCommune.system.difficulte,
      tache: tacheOuCommune,
      comp: actor.getCompetence(tacheOuCommune.system.competence),
      finished: tacheOuCommune.system.points_de_tache_courant >= tacheOuCommune.system.points_de_tache
    }
  }

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

  async _onRender(rollDialog, context, options) {
    const selectTache = rollDialog.element.querySelector(`roll-section[name="${this.code}"] select[name="select-tache"]`)
    const buttonCreate = rollDialog.element.querySelector(`roll-section[name="${this.code}"] button[name="create-tache"]`)
    const buttonEdit = rollDialog.element.querySelector(`roll-section[name="${this.code}"] button[name="edit-tache"]`)

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

    buttonCreate?.addEventListener(
      "click", async e => {
        e.preventDefault()
        await rollDialog.rollData.active.actor.createItem(ITEM_TYPES.tache, 'Nouvelle tache')
      })

    buttonEdit?.addEventListener(
      "click", e => {
        e.preventDefault()
        const current = this.getCurrent(rollDialog.rollData)
        current.tache?.sheet.render(true)
      })
  }

  getHooks(rollDialog) {
    return [
      { hook: "createItem", fn: (item, options, id) => this.onCreateUpdateItem(rollDialog, item, options, id) },
      { hook: "updateItem", fn: (item, options, id) => this.onCreateUpdateItem(rollDialog, item, options, id) }
    ]
  }

  onCreateUpdateItem(rollDialog, item, options, id) {
    if (item.type == ITEM_TYPES.tache) {
      if (item.parent?.id == rollDialog.rollData.active.actor.id || item.isTacheCommune()) {
        // TODO: filtrer davantage
        this.loadRefs(rollDialog.rollData)
        rollDialog.render()
      }
    }
  }

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

}
