import AwEActorSheet from "./base-actor-sheet.mjs"

export default class AwECharacterSheet extends AwEActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["character"],
    position: {
      width: 960,
      height: 780
    },
    window: {
      contentClasses: ["character-content"]
    },
    actions: {
      createAbility: AwECharacterSheet.#onCreateAbility,
      createWeapon: AwECharacterSheet.#onCreateWeapon,
      createKit: AwECharacterSheet.#onCreateKit,
      createEquipment: AwECharacterSheet.#onCreateEquipment,
      flowPointsPlus: AwECharacterSheet.#onFlowPointsPlus,
      flowPointsMinus: AwECharacterSheet.#onFlowPointsMinus
    }
  }

  /** @override */
  static PARTS = {
    main: {
      template: "systems/fvtt-adventures-with-emmy/templates/character-main.hbs"
    },
    tabs: {
      template: "templates/generic/tab-navigation.hbs"
    },
    biography: {
      template: "systems/fvtt-adventures-with-emmy/templates/character-biography.hbs"
    },
    equipment: {
      template: "systems/fvtt-adventures-with-emmy/templates/character-equipment.hbs"
    }
  }

  /** @override */
  tabGroups = {
    sheet: "main"
  }

  /**
   * Prepare an array of form header tabs.
   * @returns {Record<string, Partial<ApplicationTab>>} The tab objects.
   */
  #getTabs() {
    const tabs = {
      main: { id: "main", group: "sheet", icon: "fa-solid fa-user", label: "AWEMMY.Sheet.Tab.Main" },
      biography: { id: "biography", group: "sheet", icon: "fa-solid fa-book", label: "AWEMMY.Sheet.Tab.Biography" },
      equipment: { id: "equipment", group: "sheet", icon: "fa-solid fa-backpack", label: "AWEMMY.Sheet.Tab.Equipment" }
    }
    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()
    return context
  }

  /** @override */
  async _preparePartContext(partId, context) {
    const doc = this.document
    switch (partId) {
      case "main":
        context.tab = context.tabs.main
        context.abilities = doc.itemTypes.ability
        break
      case "biography":
        context.tab = context.tabs.biography
        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
      case "equipment":
        context.tab = context.tabs.equipment
        context.kits = doc.itemTypes.kit
        context.weapons = doc.itemTypes.weapon
        context.equipments = doc.itemTypes.equipment
        break
    }
    return context
  }

  /** @override */
  async _onDrop(event) {
    if (!this.isEditable || !this.isEditMode) return
    const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event)
    if (data.type === "Item") {
      const item = await fromUuid(data.uuid)
      return this._onDropItem(item)
    }
  }

  /**
   * Create a new ability item.
   * @param {Event} event - The triggering event.
   * @param {HTMLElement} target - The target element.
   */
  static #onCreateAbility(event, target) {
    this.document.createEmbeddedDocuments("Item", [{ name: "New Ability", type: "ability" }])
  }

  /**
   * Create a new weapon item.
   * @param {Event} event - The triggering event.
   * @param {HTMLElement} target - The target element.
   */
  static #onCreateWeapon(event, target) {
    this.document.createEmbeddedDocuments("Item", [{ name: "New Weapon", type: "weapon" }])
  }

  /**
   * Create a new kit item.
   * @param {Event} event - The triggering event.
   * @param {HTMLElement} target - The target element.
   */
  static #onCreateKit(event, target) {
    this.document.createEmbeddedDocuments("Item", [{ name: "New Kit", type: "kit" }])
  }

  /**
   * Create a new equipment item.
   * @param {Event} event - The triggering event.
   * @param {HTMLElement} target - The target element.
   */
  static #onCreateEquipment(event, target) {
    this.document.createEmbeddedDocuments("Item", [{ name: "New Equipment", type: "equipment" }])
  }

  /**
   * Increase flow points by 1.
   * @param {Event} event - The triggering event.
   * @param {HTMLElement} target - The target element.
   */
  static #onFlowPointsPlus(event, target) {
    const current = this.actor.system.flowPoints.value
    this.actor.update({ "system.flowPoints.value": current + 1 })
  }

  /**
   * Decrease flow points by 1.
   * @param {Event} event - The triggering event.
   * @param {HTMLElement} target - The target element.
   */
  static #onFlowPointsMinus(event, target) {
    const current = this.actor.system.flowPoints.value
    this.actor.update({ "system.flowPoints.value": Math.max(0, current - 1) })
  }
}
