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

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

  /** @override */
  static PARTS = {
    main: {
      template: "systems/fvtt-cthulhu-eternal/templates/vehicle-main.hbs",
    },
    tabs: {
      template: "templates/generic/tab-navigation.hbs",
    },
    equipment: {
      template: "systems/fvtt-cthulhu-eternal/templates/vehicle-equipment.hbs",
    },
    description: {
      template: "systems/fvtt-cthulhu-eternal/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: "CTHULHUETERNAL.Label.equipment" },
      description: { id: "description", group: "sheet", icon: "fa-solid fa-book", label: "CTHULHUETERNAL.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 foundry.applications.ux.TextEditor.implementation.enrichHTML(this.document.system.description, { async: true })
    context.enrichedNotes = await foundry.applications.ux.TextEditor.implementation.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.gears = doc.itemTypes.gear
        break
      case "description":
        context.tab = context.tabs.description
        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
  }

  /**
   * 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 #onCreateGear(event, target) {
    this.document.createEmbeddedDocuments("Item", [{ name: game.i18n.localize("CTHULHUETERNAL.Label.newGear"), type: "gear" }])
  }

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


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

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

}
