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

const ALLOWED_ITEM_TYPES = new Set(["building", "equipment", "weapon", "armor"])

export default class OathHammerSettlementSheet extends OathHammerActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["settlement"],
    position: {
      width: 840,
      height: "auto",
    },
    window: {
      contentClasses: ["settlement-content"],
    },
    actions: {
      adjustCurrency: OathHammerSettlementSheet.#onAdjustCurrency,
      adjustQty:      OathHammerSettlementSheet.#onAdjustQty,
      toggleConstructed: OathHammerSettlementSheet.#onToggleConstructed,
    },
  }

  /** @override */
  static PARTS = {
    main: {
      template: "systems/fvtt-oath-hammer/templates/actor/settlement-sheet.hbs",
    },
    tabs: {
      template: "templates/generic/tab-navigation.hbs",
    },
    overview: {
      template: "systems/fvtt-oath-hammer/templates/actor/settlement-overview.hbs",
    },
    buildings: {
      template: "systems/fvtt-oath-hammer/templates/actor/settlement-buildings.hbs",
    },
    inventory: {
      template: "systems/fvtt-oath-hammer/templates/actor/settlement-inventory.hbs",
    },
  }

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

  #getTabs() {
    const tabs = {
      overview:  { id: "overview",  group: "sheet", icon: "fa-solid fa-city",        label: "OATHHAMMER.Tab.Overview" },
      buildings: { id: "buildings", group: "sheet", icon: "fa-solid fa-building",     label: "OATHHAMMER.Tab.Buildings" },
      inventory: { id: "inventory", group: "sheet", icon: "fa-solid fa-boxes-stacked", label: "OATHHAMMER.Tab.Inventory" },
    }
    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.archetypeChoices = Object.fromEntries(
      Object.entries(SYSTEM.SETTLEMENT_ARCHETYPES).map(([k, v]) => [k, game.i18n.localize(v)])
    )
    return context
  }

  /** @override */
  async _preparePartContext(partId, context) {
    const doc = this.document
    switch (partId) {
      case "main":
        break
      case "overview":
        context.tab = context.tabs.overview
        context.enrichedDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(
          doc.system.description, { async: true }
        )
        break
      case "buildings":
        context.tab = context.tabs.buildings
        context.buildings = doc.itemTypes.building
        break
      case "inventory": {
        context.tab = context.tabs.inventory
        context.weapons    = doc.itemTypes.weapon
        context.armors     = doc.itemTypes.armor
        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") return
    const item = await fromUuid(data.uuid)
    if (!item || !ALLOWED_ITEM_TYPES.has(item.type)) return
    return this._onDropItem(item)
  }

  static async #onAdjustCurrency(event, target) {
    const field = target.dataset.field
    const delta = parseInt(target.dataset.delta, 10)
    if (!field || isNaN(delta)) return
    const current = foundry.utils.getProperty(this.document, field) ?? 0
    await this.document.update({ [field]: Math.max(0, current + delta) })
  }

  static async #onAdjustQty(event, target) {
    const itemId = target.dataset.itemId
    const delta  = parseInt(target.dataset.delta, 10)
    if (!itemId || isNaN(delta)) return
    const item = this.document.items.get(itemId)
    if (!item) return
    const current = item.system.quantity ?? 0
    await item.update({ "system.quantity": Math.max(0, current + delta) })
  }

  static async #onToggleConstructed(event, target) {
    const itemId = target.dataset.itemId
    if (!itemId) return
    const item = this.document.items.get(itemId)
    if (!item) return
    await item.update({ "system.constructed": !item.system.constructed })
  }
}
