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

export default class HellbornVehicleSheet extends HellbornActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["vehicle"],
    position: {
      width: 680,
      height: 540,
    },
    window: {
      contentClasses: ["vehicle-content"],
    },
    actions: {
      createEquipment: HellbornVehicleSheet.#onCreateEquipment,
      createWeapon: HellbornVehicleSheet.#onCreateWeapon,
    },
  }

  /** @override */
  static PARTS = {
    main: {
      template: "systems/fvtt-hellborn/templates/vehicle-main.hbs",
    },
    tabs: {
      template: "templates/generic/tab-navigation.hbs",
    },
    equipment: {
      template: "systems/fvtt-hellborn/templates/vehicle-equipment.hbs",
    },
    description: {
      template: "systems/fvtt-hellborn/templates/vehicle-description.hbs",
    },
  }

   /** @override */
   tabGroups = {
    sheet: "equipment",
  }

  /**
   * Prepare an array of form header tabs.
   * @returns {Record<string, Partial<ApplicationTab>>}
   */
  #getTabs() {
    const tabs = {
      equipment: { id: "equipment", group: "sheet", icon: "fa-solid fa-shapes", label: "HELLBORN.Label.equipment" },
      description: { id: "description", group: "sheet", icon: "fa-solid fa-book", label: "HELLBORN.Label.description" },
    }
    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.enrichedDescription = await TextEditor.enrichHTML(this.document.system.description, { async: true })
    context.enrichedNotes = await TextEditor.enrichHTML(this.document.system.notes, { async: true })

    return context
  }

  _generateTooltip(type, target) {
  }

  /** @override */
  async _preparePartContext(partId, context) {
    const doc = this.document
    switch (partId) {
      case "main":
        break
      case "equipment":
        context.tab = context.tabs.equipment
        context.weapons = doc.itemTypes.weapon
        context.weapons.sort((a, b) => a.name.localeCompare(b.name))
        context.equipments = doc.itemTypes.equipment
        context.equipments.sort((a, b) => a.name.localeCompare(b.name))
        break
      case "description":
        context.tab = context.tabs.description
        context.enrichedDescription = await TextEditor.enrichHTML(doc.system.description, { async: true })
        context.enrichedNotes = await TextEditor.enrichHTML(doc.system.notes, { async: true })
        break
    }
    return context
  }

  /**
   * Creates a new attack item directly from the sheet and embeds it into the document.
   * @param {Event} event             The initiating click event.
   * @param {HTMLElement} target      The current target of the event listener.
   */
  static #onCreateEquipment(event, target) {
    this.document.createEmbeddedDocuments("Item", [{ name: game.i18n.localize("HELLBORN.Label.newEquipment"), type: "equipment" }])
  }

  static #onCreateWeapon(event, target) {
    this.document.createEmbeddedDocuments("Item", [{ name: game.i18n.localize("HELLBORN.Label.newWeapon"), type: "weapon" }])
  }

  async _onRoll(event, target) {
    const rollType = $(event.currentTarget).data("roll-type")
    let item
    let li
    switch (rollType) {
      case "damage":
        li = $(event.currentTarget).parents(".item");
        item = this.actor.items.get(li.data("item-id"));
        break
      default:
        throw new Error(`Unknown roll type ${rollType}`)
    }
    await this.document.system.roll(rollType, item)
  }


  async _onDrop(event) {
    if (!this.isEditable || !this.isEditMode) return
    const data = TextEditor.getDragEventData(event)

    // Handle different data types
    switch (data.type) {
      case "Item":
        const item = await fromUuid(data.uuid)
        return super._onDropItem(item)
    }
  }

}
