import CelestopolActorSheet from "./base-actor-sheet.mjs"
import { SYSTEM } from "../../config/system.mjs"

export default class CelestopolCharacterSheet extends CelestopolActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["character"],
    position: { width: 920, height: 660 },
    window: { contentClasses: ["character-content"] },
    actions: {
      createAnomaly:    CelestopolCharacterSheet.#onCreateAnomaly,
      createAspect:     CelestopolCharacterSheet.#onCreateAspect,
      createAttribute:  CelestopolCharacterSheet.#onCreateAttribute,
      createEquipment:  CelestopolCharacterSheet.#onCreateEquipment,
      useAnomaly:       CelestopolCharacterSheet.#onUseAnomaly,
      resetAnomalyUses: CelestopolCharacterSheet.#onResetAnomalyUses,
    },
  }

  /** @override */
  static PARTS = {
    main:       { template: "systems/fvtt-celestopol/templates/character-main.hbs" },
    tabs:       { template: "templates/generic/tab-navigation.hbs" },
    competences:{ template: "systems/fvtt-celestopol/templates/character-competences.hbs" },
    blessures:  { template: "systems/fvtt-celestopol/templates/character-blessures.hbs" },
    factions:   { template: "systems/fvtt-celestopol/templates/character-factions.hbs" },
    biography:  { template: "systems/fvtt-celestopol/templates/character-biography.hbs" },
  }

  tabGroups = { sheet: "competences" }

  #getTabs() {
    const tabs = {
      competences:{ id: "competences", group: "sheet", icon: "fa-solid fa-dice-d6", label: "CELESTOPOL.Tab.competences" },
      blessures:  { id: "blessures",   group: "sheet", icon: "fa-solid fa-heart-crack", label: "CELESTOPOL.Tab.blessures" },
      factions:   { id: "factions",    group: "sheet", icon: "fa-solid fa-flag", label: "CELESTOPOL.Tab.factions" },
      biography:  { id: "biography",   group: "sheet", icon: "fa-solid fa-book", label: "CELESTOPOL.Tab.biography" },
    }
    for (const v of Object.values(tabs)) {
      v.active   = this.tabGroups[v.group] === v.id
      v.cssClass = v.active ? "active" : ""
    }
    return tabs
  }

  /** @override */
  async _prepareContext() {
    const context  = await super._prepareContext()
    context.tabs   = this.#getTabs()
    context.stats  = SYSTEM.STATS
    context.skills = SYSTEM.SKILLS
    context.anomalyTypes = SYSTEM.ANOMALY_TYPES
    context.factions     = SYSTEM.FACTIONS
    context.woundLevels  = SYSTEM.WOUND_LEVELS
    return context
  }

  /** @override */
  async _preparePartContext(partId, context) {
    context.systemFields = this.document.system.schema.fields
    const doc = this.document

    switch (partId) {
      case "main":
        break

      case "competences":
        context.tab = context.tabs.competences
        context.anomaly    = doc.itemTypes.anomaly[0] ?? null
        context.aspects    = doc.itemTypes.aspect
        context.attributes = doc.itemTypes.attribute
        if (context.anomaly) {
          const def = SYSTEM.ANOMALY_DEFINITIONS[context.anomaly.system.subtype] ?? SYSTEM.ANOMALY_DEFINITIONS.none
          context.anomalySkillLabels = def.technicalSkills.map(key => {
            if (key === "lune") return game.i18n.localize("CELESTOPOL.Anomaly.moonDie")
            for (const skills of Object.values(SYSTEM.SKILLS)) {
              if (skills[key]) return game.i18n.localize(skills[key].label)
            }
            return key
          })
        } else {
          context.anomalySkillLabels = []
        }
        break

      case "blessures":
        context.tab = context.tabs.blessures
        break

      case "factions":
        context.tab = context.tabs.factions
        break

      case "biography":
        context.tab = context.tabs.biography
        context.equipments = doc.itemTypes.equipment
        context.equipments.sort((a, b) => a.name.localeCompare(b.name))
        context.enrichedDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(
          doc.system.description, { async: true })
        context.enrichedNotes = await foundry.applications.ux.TextEditor.implementation.enrichHTML(
          doc.system.notes, { async: true })
        break
    }
    return context
  }

  static #onCreateAnomaly() {
    if (this.document.itemTypes.anomaly.length > 0) {
      ui.notifications.warn(game.i18n.localize("CELESTOPOL.Anomaly.maxAnomaly"))
      return
    }
    this.document.createEmbeddedDocuments("Item", [{
      name: game.i18n.localize("CELESTOPOL.Item.newAnomaly"), type: "anomaly",
    }])
  }

  static #onCreateAspect() {
    this.document.createEmbeddedDocuments("Item", [{
      name: game.i18n.localize("CELESTOPOL.Item.newAspect"), type: "aspect",
    }])
  }

  static #onCreateAttribute() {
    this.document.createEmbeddedDocuments("Item", [{
      name: game.i18n.localize("CELESTOPOL.Item.newAttribute"), type: "attribute",
    }])
  }

  static #onCreateEquipment() {
    this.document.createEmbeddedDocuments("Item", [{
      name: game.i18n.localize("CELESTOPOL.Item.newEquipment"), type: "equipment",
    }])
  }

  static async #onUseAnomaly(event, target) {
    const itemId  = target.dataset.itemId
    const anomaly = this.document.items.get(itemId)
    if (!anomaly) return
    const current = anomaly.system.usesRemaining
    if (current <= 0) {
      ui.notifications.warn(game.i18n.localize("CELESTOPOL.Anomaly.noUsesLeft"))
      return
    }
    await anomaly.update({ "system.usesRemaining": current - 1 })
  }

  static async #onResetAnomalyUses(event, target) {
    const itemId  = target.dataset.itemId
    const anomaly = this.document.items.get(itemId)
    if (!anomaly) return
    await anomaly.update({ "system.usesRemaining": anomaly.system.level })
  }
}
