const { HandlebarsApplicationMixin } = foundry.applications.api

/**
 * Base item sheet for Ecryme using Application V2.
 * Subclasses must define static PARTS including header, tabs, description, and optionally details.
 */
export default class EcrymeBaseItemSheet extends HandlebarsApplicationMixin(foundry.applications.sheets.ItemSheetV2) {

  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["fvtt-ecryme", "item"],
    position: {
      width: 520,
      height: "auto",
    },
    form: {
      submitOnChange: true,
    },
    window: {
      resizable: true,
    },
    dragDrop: [{ dragSelector: "[data-drag]", dropSelector: null }],
    actions: {
      editImage: EcrymeBaseItemSheet.#onEditImage,
    },
  }

  /** Active tab group tracking */
  tabGroups = {
    primary: "description",
  }

  /**
   * Build the tabs definition, adding a "details" tab only if the subclass has a "details" PART.
   * @returns {Record<string, object>}
   */
  _getTabs() {
    const tabs = {
      description: { id: "description", group: "primary", label: "ECRY.ui.description" },
    }
    if (this.constructor.PARTS?.details) {
      tabs.details = { id: "details", group: "primary", label: "ECRY.ui.details" }
    }
    for (const tab of Object.values(tabs)) {
      tab.active = this.tabGroups[tab.group] === tab.id
      tab.cssClass = tab.active ? "active" : ""
    }
    return tabs
  }

  /** @override */
  async _prepareContext() {
    const context = {
      fields: this.document.schema.fields,
      systemFields: this.document.system.schema.fields,
      item: this.document,
      system: this.document.system,
      source: this.document.toObject(),
      config: game.ecryme.config,
      isEditable: this.isEditable,
      tabs: this._getTabs(),
    }
    return context
  }

  /** @override */
  async _preparePartContext(partId, context) {
    context = await super._preparePartContext(partId, context)
    if (partId === "description") {
      context.tab = context.tabs.description
      context.enrichedDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(
        this.document.system.description, { async: true }
      )
    }
    return context
  }

  _canDragStart(selector) { return this.isEditable }
  _canDragDrop(selector) { return this.isEditable && this.document.isOwner }
  _onDragStart(event) {}
  _onDragOver(event) {}
  async _onDrop(event) {}
  // #endregion

  // #region Actions
  static async #onEditImage(event, target) {
    const attr = target.dataset.edit
    const current = foundry.utils.getProperty(this.document, attr)
    const { img } = this.document.constructor.getDefaultArtwork?.(this.document.toObject()) ?? {}
    const fp = new FilePicker({
      current,
      type: "image",
      redirectToRoot: img ? [img] : [],
      callback: (path) => {
        this.document.update({ [attr]: path })
      },
      top: this.position.top + 40,
      left: this.position.left + 10,
    })
    return fp.browse()
  }
  // #endregion
}
