import OathHammerActorSheet from "./base-actor-sheet.mjs"
import { SYSTEM } from "../../config/system.mjs"
import { rollInitiativeCheck, rollNPCSkill, rollNPCArmor, rollNPCSpell, rollNPCMiracle, rollNPCAttackDamage } from "../../rolls.mjs"

export default class OathHammerNPCSheet extends OathHammerActorSheet {
  static DEFAULT_OPTIONS = {
    classes: ["npc"],
    position: { width: 720, height: "auto" },
    window: { contentClasses: ["npc-content"] },
    actions: {
      rollInitiative:  OathHammerNPCSheet.#onRollInitiative,
      adjustGrit:      OathHammerNPCSheet.#onAdjustGrit,
      rollSkillNPC:    OathHammerNPCSheet.#onRollSkillNPC,
      rollArmor:       OathHammerNPCSheet.#onRollArmor,
      createSpell:     OathHammerNPCSheet.#onCreateSpell,
      createMiracle:   OathHammerNPCSheet.#onCreateMiracle,
      castNPCSpell:    OathHammerNPCSheet.#onCastNPCSpell,
      castNPCMiracle:  OathHammerNPCSheet.#onCastNPCMiracle,
      createNpcAttack: OathHammerNPCSheet.#onCreateNpcAttack,
      rollNpcAttack:   OathHammerNPCSheet.#onRollNpcAttack,
    },
  }

  static PARTS = {
    main:      { template: "systems/fvtt-oath-hammer/templates/actor/npc-sheet.hbs" },
    tabs:      { template: "templates/generic/tab-navigation.hbs" },
    skills:    { template: "systems/fvtt-oath-hammer/templates/actor/npc-skills.hbs" },
    combat:    { template: "systems/fvtt-oath-hammer/templates/actor/npc-combat.hbs" },
    traits:    { template: "systems/fvtt-oath-hammer/templates/actor/npc-traits.hbs" },
    magic:     { template: "systems/fvtt-oath-hammer/templates/actor/npc-magic.hbs" },
    equipment: { template: "systems/fvtt-oath-hammer/templates/actor/npc-equipment.hbs" },
    notes:     { template: "systems/fvtt-oath-hammer/templates/actor/npc-notes.hbs" },
  }

  tabGroups = { sheet: "skills" }

  #getTabs() {
    const isNPC = this.document.system.subtype === "npc"
    const hasMagic = this.document.items.some(i => i.type === "spell" || i.type === "miracle")
    const tabs = {
      skills: { id: "skills",    group: "sheet", icon: "fa-solid fa-dice-d6",        label: "OATHHAMMER.Tab.Skills" },
      combat: { id: "combat",    group: "sheet", icon: "fa-solid fa-swords",          label: "OATHHAMMER.Tab.Combat" },
      traits: { id: "traits",    group: "sheet", icon: "fa-solid fa-star",            label: "OATHHAMMER.Tab.Traits" },
      notes:  { id: "notes",     group: "sheet", icon: "fa-solid fa-book",            label: "OATHHAMMER.Tab.Notes" },
    }
    if (isNPC) {
      tabs.equipment = { id: "equipment", group: "sheet", icon: "fa-solid fa-backpack",     label: "OATHHAMMER.Tab.Equipment" }
    }
    if (hasMagic || !this.isPlayMode) {
      tabs.magic = { id: "magic", group: "sheet", icon: "fa-solid fa-wand-sparkles", label: "OATHHAMMER.Tab.Magic" }
    }
    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.subtypeChoices = Object.fromEntries(
      Object.entries(SYSTEM.NPC_SUBTYPES).map(([k, v]) => [k, game.i18n.localize(v)])
    )
    context.subtypeLabels = context.subtypeChoices

    const armorColor = this.document.system.armorDice?.colorDiceType ?? "white"
    context.armorDiceEmoji = armorColor === "black" ? "⬛" : armorColor === "red" ? "🔴" : "⬜"

    context.colorChoices = Object.fromEntries(
      Object.entries(SYSTEM.DICE_COLOR_TYPES).map(([k, v]) => [k, game.i18n.localize(v)])
    )

    context.traitTypeLabels = Object.fromEntries(
      Object.entries(SYSTEM.TRAIT_TYPE_CHOICES).map(([k, v]) => [k, v])
    )

    return context
  }

  /** @override */
  async _preparePartContext(partId, context) {
    const doc = this.document
    switch (partId) {
      case "skills":
        context.tab = context.tabs.skills
        context.skills = (doc.itemTypes.skillnpc ?? []).slice().sort((a, b) => a.name.localeCompare(b.name))
        break
      case "combat":
        context.tab = context.tabs.combat
        context.npcAttacks  = (doc.itemTypes.npcattack ?? []).map(a => ({
          id: a.id, uuid: a.uuid, img: a.img, name: a.name, system: a.system,
          _descTooltip: a.system.description?.replace(/<[^>]+>/g, "").slice(0, 300) ?? ""
        }))
        context.combatantInitiative = game.combat?.combatants.find(c => c.actor?.id === doc.id)?.initiative ?? null
        break
      case "traits":
        context.tab = context.tabs.traits
        context.traits = (doc.itemTypes.trait ?? []).slice().sort((a, b) => a.name.localeCompare(b.name)).map(t => ({
          id: t.id, uuid: t.uuid, img: t.img, name: t.name, system: t.system,
          _descTooltip: t.system.description?.replace(/<[^>]+>/g, "").slice(0, 300) ?? ""
        }))
        break
      case "magic":
        context.tab = context.tabs.magic
        context.spells   = (doc.itemTypes.spell ?? []).map(s => ({
          id: s.id, uuid: s.uuid, img: s.img, name: s.name, system: s.system,
          _descTooltip: s.system.effect?.replace(/<[^>]+>/g, "").slice(0, 300) ?? "",
          traditionLabel: game.i18n.localize(SYSTEM.SORCEROUS_TRADITIONS[s.system.tradition]?.label ?? s.system.tradition)
        }))
        context.miracles = (doc.itemTypes.miracle ?? []).map(m => ({
          id: m.id, uuid: m.uuid, img: m.img, name: m.name, system: m.system,
          _descTooltip: m.system.effect?.replace(/<[^>]+>/g, "").slice(0, 300) ?? "",
          traditionLabel: game.i18n.localize(SYSTEM.DIVINE_TRADITIONS[m.system.divineTradition] ?? m.system.divineTradition)
        }))
        break
      case "equipment":
        context.tab = context.tabs.equipment
        context.armors    = doc.itemTypes.armor ?? []
        context.equipment = doc.itemTypes.equipment ?? []
        break
      case "notes":
        context.tab = context.tabs.notes
        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
  }

  /** @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) return
    const ALLOWED = new Set(["skillnpc", "npcattack", "trait", "armor", "equipment", "spell", "miracle"])
    if (!ALLOWED.has(item.type)) return
    return this._onDropItem(item)
  }

  static async #onAdjustGrit(event, target) {
    const delta   = parseInt(target.dataset.delta, 10)
    const current = this.document.system.grit?.value ?? 0
    const max     = this.document.system.grit?.max ?? current
    await this.document.update({ "system.grit.value": Math.max(0, Math.min(max, current + delta)) })
  }

  static #onCreateNpcAttack() {
    this.document.createEmbeddedDocuments("Item", [{ name: game.i18n.localize("OATHHAMMER.NewItem.NpcAttack"), type: "npcattack" }])
  }

  static async #onRollNpcAttack(event, target) {
    const attack = this.document.items.get(target.dataset.itemId)
    if (!attack) return

    const bonusOptions = Array.from({ length: 13 }, (_, i) => {
      const v = i - 6
      return { value: v, label: v > 0 ? `+${v}` : String(v), selected: v === 0 }
    })

    const content = await foundry.applications.handlebars.renderTemplate(
      "systems/fvtt-oath-hammer/templates/npc-skill-dialog.hbs",
      {
        skillName:   attack.name,
        skillImg:    attack.img,
        dicePool:    attack.system.damageDice,
        colorEmoji:  attack.system.colorEmoji,
        colorType:   attack.system.colorDiceType,
        threshold:   attack.system.threshold,
        bonusOptions,
        showExplodeOn5: true,
        colorChoices: Object.fromEntries(
          Object.entries(SYSTEM.DICE_COLOR_TYPES).map(([k, v]) => [k, game.i18n.localize(v)])
        ),
        rollModes:   foundry.utils.duplicate(CONFIG.Dice.rollModes),
        visibility:  game.settings.get("core", "rollMode"),
      }
    )

    const result = await foundry.applications.api.DialogV2.wait({
      window:      { title: attack.name, resizable: true },
      classes:     ["fvtt-oath-hammer"], position: { width: 420 }, content,
      rejectClose: false,
      buttons: [{ label: game.i18n.localize("OATHHAMMER.Dialog.Roll"), icon: "fa-solid fa-burst",
        callback: (_ev, btn) => { const o = {}; for (const el of btn.form.elements) { if (!el.name) continue; o[el.name] = el.type === "checkbox" ? String(el.checked) : el.value } return o }
      }],
    })
    if (!result) return

    await rollNPCAttackDamage(this.document, attack, {
      bonus:      parseInt(result.bonus) || 0,
      explodeOn5: result.explodeOn5 === "true",
      visibility: result.visibility,
    })
  }

  static #onCreateSpell() {
    this.document.createEmbeddedDocuments("Item", [{ name: game.i18n.localize("OATHHAMMER.NewItem.Spell"), type: "spell" }])
  }

  static #onCreateMiracle() {
    this.document.createEmbeddedDocuments("Item", [{ name: game.i18n.localize("OATHHAMMER.NewItem.Miracle"), type: "miracle" }])
  }

  static async #onCastNPCSpell(event, target) {
    const spell = this.document.items.get(target.dataset.itemId)
    if (!spell) return

    const colorChoices = Object.fromEntries(
      Object.entries(SYSTEM.DICE_COLOR_TYPES).map(([k, v]) => [k, game.i18n.localize(v)])
    )
    const poolOptions = Array.from({ length: 10 }, (_, i) => {
      const v = i + 1
      return { value: v, label: String(v), selected: v === 3 }
    })
    const bonusOptions = Array.from({ length: 13 }, (_, i) => {
      const v = i - 6
      return { value: v, label: v > 0 ? `+${v}` : String(v), selected: v === 0 }
    })

    const content = await foundry.applications.handlebars.renderTemplate(
      "systems/fvtt-oath-hammer/templates/npc-magic-dialog.hbs",
      {
        itemName: spell.name, itemImg: spell.img,
        dv: spell.system.difficultyValue,
        poolOptions, bonusOptions, colorChoices, showColor: true,
        showExplodeOn5: true,
        rollModes:  foundry.utils.duplicate(CONFIG.Dice.rollModes),
        visibility: game.settings.get("core", "rollMode"),
      }
    )

    const result = await foundry.applications.api.DialogV2.wait({
      window:      { title: spell.name, resizable: true },
      classes:     ["fvtt-oath-hammer"], position: { width: 420 }, content,
      rejectClose: false,
      buttons: [{ label: game.i18n.localize("OATHHAMMER.Dialog.Roll"), icon: "fa-solid fa-wand-sparkles",
        callback: (_ev, btn) => { const o = {}; for (const el of btn.form.elements) { if (!el.name) continue; o[el.name] = el.type === "checkbox" ? String(el.checked) : el.value } return o }
      }],
    })
    if (!result) return

    await rollNPCSpell(this.document, spell, {
      dicePool:      parseInt(result.dicePool) || 3,
      bonus:         parseInt(result.bonus) || 0,
      colorOverride: result.colorOverride || null,
      explodeOn5:    result.explodeOn5 === "true",
      visibility:    result.visibility,
    })
  }

  static async #onCastNPCMiracle(event, target) {
    const miracle = this.document.items.get(target.dataset.itemId)
    if (!miracle) return

    const poolOptions = Array.from({ length: 10 }, (_, i) => {
      const v = i + 1
      return { value: v, label: String(v), selected: v === 3 }
    })
    const bonusOptions = Array.from({ length: 13 }, (_, i) => {
      const v = i - 6
      return { value: v, label: v > 0 ? `+${v}` : String(v), selected: v === 0 }
    })

    const content = await foundry.applications.handlebars.renderTemplate(
      "systems/fvtt-oath-hammer/templates/npc-magic-dialog.hbs",
      {
        itemName: miracle.name, itemImg: miracle.img,
        dv: null, showColor: false,
        poolOptions, bonusOptions,
        showExplodeOn5: true,
        rollModes:  foundry.utils.duplicate(CONFIG.Dice.rollModes),
        visibility: game.settings.get("core", "rollMode"),
      }
    )

    const result = await foundry.applications.api.DialogV2.wait({
      window:      { title: miracle.name, resizable: true },
      classes:     ["fvtt-oath-hammer"], position: { width: 420 }, content,
      rejectClose: false,
      buttons: [{ label: game.i18n.localize("OATHHAMMER.Dialog.Roll"), icon: "fa-solid fa-hands-praying",
        callback: (_ev, btn) => { const o = {}; for (const el of btn.form.elements) { if (!el.name) continue; o[el.name] = el.type === "checkbox" ? String(el.checked) : el.value } return o }
      }],
    })
    if (!result) return

    await rollNPCMiracle(this.document, miracle, {
      dicePool:   parseInt(result.dicePool) || 3,
      bonus:      parseInt(result.bonus) || 0,
      explodeOn5: result.explodeOn5 === "true",
      visibility: result.visibility,
    })
  }

  static async #onRollArmor() {
    const actor = this.document
    const sys = actor.system
    const colorType  = sys.armorDice?.colorDiceType || "white"
    const threshold  = colorType === "black" ? 2 : colorType === "red" ? 3 : 4
    const colorEmoji = colorType === "black" ? "⬛" : colorType === "red" ? "🔴" : "⬜"
    const dicePool   = sys.armorDice?.value ?? 0

    const colorChoices = Object.fromEntries(
      Object.entries(SYSTEM.DICE_COLOR_TYPES).map(([k, v]) => [k, game.i18n.localize(v)])
    )
    const bonusOptions = Array.from({ length: 13 }, (_, i) => {
      const v = i - 6
      return { value: v, label: v > 0 ? `+${v}` : String(v), selected: v === 0 }
    })

    const content = await foundry.applications.handlebars.renderTemplate(
      "systems/fvtt-oath-hammer/templates/npc-skill-dialog.hbs",
      {
        skillName:   game.i18n.localize("OATHHAMMER.Label.ArmorDice"),
        skillImg:    actor.img,
        dicePool,
        colorEmoji,
        colorType,
        threshold,
        bonusOptions,
        colorChoices,
        showExplodeOn5: true,
        rollModes:   foundry.utils.duplicate(CONFIG.Dice.rollModes),
        visibility:  game.settings.get("core", "rollMode"),
      }
    )

    const result = await foundry.applications.api.DialogV2.wait({
      window:      { title: game.i18n.localize("OATHHAMMER.Label.ArmorDice"), resizable: true },
      classes:     ["fvtt-oath-hammer"], position: { width: 420 }, content,
      rejectClose: false,
      buttons: [{ label: game.i18n.localize("OATHHAMMER.Dialog.Roll"), icon: "fa-solid fa-dice-d6",
        callback: (_ev, btn) => { const o = {}; for (const el of btn.form.elements) { if (!el.name) continue; o[el.name] = el.type === "checkbox" ? String(el.checked) : el.value } return o }
      }],
    })
    if (!result) return

    await rollNPCArmor(actor, {
      bonus:         parseInt(result.bonus) || 0,
      colorOverride: result.colorOverride || null,
      explodeOn5:    result.explodeOn5 === "true",
      visibility:    result.visibility,
    })
  }

  static async #onRollInitiative() {
    const actor = this.document
    const combatant = game.combat?.combatants.find(c => c.actor?.id === actor.id)
    if (combatant) {
      await game.combat.rollInitiative([combatant.id])
    } else {
      await rollInitiativeCheck(actor)
    }
  }

  static async #onRollSkillNPC(event, target) {
    const itemId = target.dataset.itemId
    const item   = this.document.items.get(itemId)
    if (!item) return

    const colorChoices = Object.fromEntries(
      Object.entries(SYSTEM.DICE_COLOR_TYPES).map(([k, v]) => [k, game.i18n.localize(v)])
    )
    const bonusOptions = Array.from({ length: 13 }, (_, i) => {
      const v = i - 6
      return { value: v, label: v > 0 ? `+${v}` : String(v), selected: v === 0 }
    })

    const content = await foundry.applications.handlebars.renderTemplate(
      "systems/fvtt-oath-hammer/templates/npc-skill-dialog.hbs",
      {
        skillName:   item.name,
        skillImg:    item.img,
        dicePool:    item.system.dicePool,
        colorEmoji:  item.system.colorEmoji,
        colorType:   item.system.colorDiceType,
        threshold:   item.system.threshold,
        bonusOptions,
        colorChoices,
        showExplodeOn5: true,
        rollModes:   foundry.utils.duplicate(CONFIG.Dice.rollModes),
        visibility:  game.settings.get("core", "rollMode"),
      }
    )

    const result = await foundry.applications.api.DialogV2.wait({
      window:      { title: item.name, resizable: true },
      classes:     ["fvtt-oath-hammer"], position: { width: 420 }, content,
      rejectClose: false,
      buttons: [{ label: game.i18n.localize("OATHHAMMER.Dialog.Roll"), icon: "fa-solid fa-dice-d6",
        callback: (_ev, btn) => { const o = {}; for (const el of btn.form.elements) { if (!el.name) continue; o[el.name] = el.type === "checkbox" ? String(el.checked) : el.value } return o }
      }],
    })
    if (!result) return

    await rollNPCSkill(this.document, item, {
      bonus:         parseInt(result.bonus) || 0,
      colorOverride: result.colorOverride || null,
      explodeOn5:    result.explodeOn5 === "true",
      visibility:    result.visibility,
    })
  }

}
