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

export default class OathHammerRegimentSheet extends OathHammerActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["regiment"],
    position: { width: 680, height: 620 },
    window: { contentClasses: ["regiment-content"] },
    actions: {
      adjustGrit:      OathHammerRegimentSheet.#onAdjustGrit,
      rollArmor:       OathHammerRegimentSheet.#onRollArmor,
      rollSkillNPC:    OathHammerRegimentSheet.#onRollSkillNPC,
      createNpcAttack: OathHammerRegimentSheet.#onCreateNpcAttack,
      rollNpcAttack:   OathHammerRegimentSheet.#onRollNpcAttack,
      createSkill:     OathHammerRegimentSheet.#onCreateSkill,
      createTrait:     OathHammerRegimentSheet.#onCreateTrait,
      openLeader:      OathHammerRegimentSheet.#onOpenLeader,
      clearLeader:     OathHammerRegimentSheet.#onClearLeader,
    },
  }

  /** @override */
  static PARTS = {
    main: { template: "systems/fvtt-oath-hammer/templates/actor/regiment-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/regiment-combat.hbs" },
    traits: { template: "systems/fvtt-oath-hammer/templates/actor/npc-traits.hbs" },
    notes: { template: "systems/fvtt-oath-hammer/templates/actor/npc-notes.hbs" },
  }

  tabGroups = { sheet: "skills" }

  #getTabs() {
    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" },
    }
    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()
    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])
    )
    // Resolve leader actor
    const leaderUuid = this.document.system.leaderUuid
    if (leaderUuid) {
      const leader = await fromUuid(leaderUuid)
      context.leader = leader ? { id: leader.id, uuid: leader.uuid, name: leader.name, img: leader.img } : null
    } else {
      context.leader = null
    }
    return context
  }

  /** @override */
  async _preparePartContext(partId, context) {
    const doc = this.document
    switch (partId) {
      case "main":
        break
      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) ?? ""
        }))
        break
      case "traits":
        context.tab = context.tabs.traits
        context.traits = (doc.itemTypes.trait ?? []).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 "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)

    // Actor drop → set as unit leader (must be token-linked)
    if (data.type === "Actor") {
      const actor = await fromUuid(data.uuid)
      if (!actor) return
      if (!actor.prototypeToken?.actorLink) {
        ui.notifications.warn(game.i18n.localize("OATHHAMMER.Warning.LeaderNotLinked"))
        return
      }
      return this.document.update({ "system.leaderUuid": actor.uuid })
    }

    if (data.type !== "Item") return
    const item = await fromUuid(data.uuid)
    if (!item) return
    const ALLOWED = new Set(["skillnpc", "npcattack", "trait"])
    if (!ALLOWED.has(item.type)) return
    return this._onDropItem(item)
  }

  // ── Actions ────────────────────────────────────────────────────────────────

  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 async #onRollArmor() {
    const doc = this.document
    const armorDice = doc.system.armorDice
    if (!armorDice?.value) return ui.notifications.info("No armor dice to roll.")
    const colorType  = armorDice.colorDiceType || "white"
    const threshold  = colorType === "black" ? 2 : colorType === "red" ? 3 : 4
    const colorEmoji = colorType === "black" ? "⬛" : colorType === "red" ? "🔴" : "⬜"
    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: doc.img, dicePool: armorDice.value,
        colorType, colorEmoji, threshold, bonusOptions,
        colorChoices: { white: "⬜ White (4+)", red: "🔴 Red (3+)", black: "⬛ Black (2+)" },
        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: `${doc.name} — ${game.i18n.localize("OATHHAMMER.Roll.ArmorRoll")}`, 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(doc, {
      bonus:        parseInt(result.bonus) || 0,
      colorOverride: result.colorOverride || null,
      explodeOn5:   result.explodeOn5 === "true",
      visibility:   result.visibility,
    })
  }

  static async #onRollSkillNPC(event, target) {
    const skill = this.document.items.get(target.dataset.itemId)
    if (!skill) return
    const colorType  = skill.system.colorDiceType || "white"
    const threshold  = colorType === "black" ? 2 : colorType === "red" ? 3 : 4
    const colorEmoji = colorType === "black" ? "⬛" : colorType === "red" ? "🔴" : "⬜"
    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: skill.name, skillImg: skill.img, dicePool: skill.system.dicePool,
        colorType, colorEmoji, threshold, bonusOptions,
        colorChoices: { white: "⬜ White (4+)", red: "🔴 Red (3+)", black: "⬛ Black (2+)" },
        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: `${skill.name} — ${game.i18n.localize("OATHHAMMER.Tab.Skills")}`, 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, skill, {
      bonus:        parseInt(result.bonus) || 0,
      colorOverride: result.colorOverride || null,
      explodeOn5:   result.explodeOn5 === "true",
      visibility:   result.visibility,
    })
  }

  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 colorType  = attack.system.colorDiceType || "white"
    const threshold  = colorType === "black" ? 2 : colorType === "red" ? 3 : 4
    const colorEmoji = colorType === "black" ? "⬛" : colorType === "red" ? "🔴" : "⬜"
    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,
        colorType, colorEmoji, threshold, bonusOptions,
        showExplodeOn5: true,
        colorChoices: { white: "⬜ White (4+)", red: "🔴 Red (3+)", black: "⬛ Black (2+)" },
        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} — ${game.i18n.localize("OATHHAMMER.Dialog.Damage")}`, 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 rollNPCAttackDamage(this.document, attack, {
      bonus:        parseInt(result.bonus) || 0,
      colorOverride: result.colorOverride || null,
      explodeOn5:   result.explodeOn5 === "true",
      visibility:   result.visibility,
    })
  }

  static #onCreateSkill() {
    this.document.createEmbeddedDocuments("Item", [{
      name: game.i18n.localize("OATHHAMMER.NewItem.SkillNPC"), type: "skillnpc"
    }])
  }

  static #onCreateTrait() {
    this.document.createEmbeddedDocuments("Item", [{
      name: game.i18n.localize("OATHHAMMER.NewItem.Trait"), type: "trait"
    }])
  }

  static async #onOpenLeader() {
    const leaderUuid = this.document.system.leaderUuid
    if (!leaderUuid) return
    const leader = await fromUuid(leaderUuid)
    if (leader) leader.sheet.render(true)
  }

  static async #onClearLeader() {
    await this.document.update({ "system.leaderUuid": null })
  }
}
