import { Grammar } from "../grammar.js"
import { Misc } from "../misc.js"
import { PART_CARAC } from "./roll-part-carac.mjs"
import { PART_DIFF } from "./roll-part-diff.mjs"
import { RollPartSelect } from "./roll-part-select.mjs"
import { ROLLDIALOG_SECTION, RollPart } from "./roll-part.mjs"

export const PART_COMP = "comp"

const SELECT_SANS_COMPETENCE = { key: '', label: "Sans compétence", value: 0 }

export class RollPartComp extends RollPartSelect {

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

  get code() { return PART_COMP }
  get name() { return 'Compétences' }
  get section() { return ROLLDIALOG_SECTION.COMP }

  onReady(rollParts) {
    this.rollPartCarac = rollParts.find(it => it.code == PART_CARAC)
    this.rollPartDiff = rollParts.find(it => it.code == PART_DIFF)
  }

  loadRefs(rollData) {
    const refs = this.getRefs(rollData)
    const selected = this.getSelected(rollData)
    const all = this.$getActorComps(rollData)
    const selectedComp = selected.key
    if (selected.forced && selectedComp!= undefined ) {
      refs.all = all.filter(comp => Grammar.equalsInsensitive(comp.label, selectedComp))
      if (refs.all.length == 0) {
        if (selected.key && selected.key.length > 0) {
          refs.all = all.filter(comp => Grammar.includesLowerCaseNoAccent(comp.label, selectedComp))
        }
        else {
          refs.all = all.filter(comp => comp == SELECT_SANS_COMPETENCE)
        }
      }
    }
    else {
      refs.all = all
    }
    refs.comps = refs.all
    this.$selectComp(rollData)
    if (rollData.type.current == PART_COMP && selectedComp) {
      const current = this.getCurrent(rollData)
      const selectedCarac = RollPart.getSelectedPart(rollData, PART_CARAC)
      const selectedDiff = RollPart.getSelectedPart(rollData, PART_DIFF)
      if (!rollData.type.appelChance){
        this.rollPartCarac.selectByKey(rollData, selectedCarac?.key ?? current.comp.system.defaut_carac)
        this.rollPartDiff.setDiff(rollData, selectedDiff?.value ?? current.comp.system.default_diffLibre)
      }

    }
  }

  choices(refs) { return refs.comps }

  $getActorComps(rollData) {
    const competences = (rollData.active.actor?.getCompetences() ?? [])
      .map(RollPartComp.extractComp)
      .sort(Misc.ascending(it => Grammar.toLowerCaseNoAccentNoSpace(it.label)))
    /* TODO: filter competences */
    const listCompetences = [
      SELECT_SANS_COMPETENCE,
      ...competences
    ]
    return listCompetences
  }

  static extractComp(comp) {
    return {
      key: comp.name,
      label: comp.name,
      value: comp.system.niveau,
      comp: comp
    }
  }

  filterComps(rollData, allowed = [], sorting = undefined) {
    const sans = allowed.includes('')
    allowed = allowed.filter(it => it != undefined)
    const refs = this.getRefs(rollData)
    refs.comps = allowed.length > 0
      ? refs.all.filter(it => allowed.includes(it.label) || (sans && it.key == ''))
      : refs.all
    if (sorting && refs.comps.length > 0) {
      refs.comps.sort(sorting)
    }
    this.$selectComp(rollData)

  }

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

  setSpecialComp(rollData, comps) {
    this.getRefs(rollData).comps = comps.map(RollPartComp.extractComp)
      .sort(Misc.ascending(it => Grammar.toLowerCaseNoAccentNoSpace(it.label)))
  }

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

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

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

}
