import { ITEM_TYPES } from "../constants.js"
import { Misc } from "../misc.js"
import { CARACS } from "../rdd-carac.js"
import { ROLL_TYPE_OEUVRE } 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_OEUVRE = "oeuvre"

const ARTS = [
  { type: ITEM_TYPES.oeuvre, action: "interpréte l'oeuvre", competence: it => it.system.competence, caracs: it => [it.system.default_carac] },
  { type: ITEM_TYPES.musique, action: "joue le morceau:", competence: it => 'Musique', caracs: it => [CARACS.OUIE] },
  { type: ITEM_TYPES.chant, action: "chante", competence: it => 'Chant', caracs: it => [CARACS.OUIE] },
  {
    type: ITEM_TYPES.danse, action: "danse:", competence: it => 'Danse', caracs: it => {
      const caracs = []
      if (it.system.agilite) { caracs.push(CARACS.AGILITE) }
      if (it.system.apparence) { caracs.push(CARACS.APPARENCE) }
      return caracs
    }
  },
]

export class RollPartOeuvre extends RollPartSelect {
  onReady(rollParts) {
    ARTS.forEach(art => art.label = Misc.typeName('Item', art.type))
    ARTS.map(it => `roll-oeuvre-${it.type}`)
      .forEach(art =>
        foundry.applications.handlebars.loadTemplates({ [art]: `systems/foundryvtt-reve-de-dragon/templates/roll/${art}.hbs` })
      )
  }

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

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

  loadRefs(rollData) {
    const refs = this.getRefs(rollData)
    refs.oeuvres = rollData.active.actor.items
      .filter(it => it.isOeuvre() && RollPartOeuvre.getArt(it))
      .map(it => RollPartOeuvre.$extractOeuvre(it, rollData.active.actor))
    if (refs.oeuvres.length > 0) {
      this.$selectOeuvre(rollData)
    }
  }

  choices(refs) { return refs.oeuvres }

  static $extractOeuvre(oeuvre, actor) {
    const art = RollPartOeuvre.getArt(oeuvre)
    return {
      key: oeuvre.id,
      label: oeuvre.name,
      art: art,
      caracs: art.caracs(oeuvre),
      qualite: oeuvre.system.niveau,
      value: -oeuvre.system.niveau,
      oeuvre: oeuvre,
      comp: actor.getCompetence(art.competence(oeuvre))
    }
  }

  static getArt(oeuvre) {
    return ARTS.find(it => it.type == oeuvre.type)
  }

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

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

    selectOeuvre?.addEventListener("change", e => {
      const selectOptions = e.currentTarget.options
      const index = selectOptions.selectedIndex
      this.$selectOeuvre(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
  }

}
