import MGNEActorSheet from "./base-actor-sheet.mjs"
import { SYSTEM } from "../../config/system.mjs"
import { buildCharacterSelectOptions } from "./select-options.mjs"

export default class MGNECharacterSheet extends MGNEActorSheet {
  static DEFAULT_OPTIONS = {
    classes: ["character"],
    position: {
      width: 1040,
      height: 760,
    },
  }

  static PARTS = {
    main: { template: "systems/fvtt-machine-gods-noxian-expanse/templates/character-main.hbs" },
    tabs: { template: "systems/fvtt-machine-gods-noxian-expanse/templates/character-tabs.hbs" },
    overview: { template: "systems/fvtt-machine-gods-noxian-expanse/templates/character-overview.hbs" },
    daily: { template: "systems/fvtt-machine-gods-noxian-expanse/templates/character-daily.hbs" },
    equipment: { template: "systems/fvtt-machine-gods-noxian-expanse/templates/character-equipment.hbs" },
    features: { template: "systems/fvtt-machine-gods-noxian-expanse/templates/character-features.hbs" },
    notes: { template: "systems/fvtt-machine-gods-noxian-expanse/templates/character-notes.hbs" },
  }

  tabGroups = { sheet: "overview" }

  getTabs() {
    const tabs = {
      overview: { id: "overview", group: "sheet", label: game.i18n.localize("MGNE.Tabs.overview") },
      daily: { id: "daily", group: "sheet", label: game.i18n.localize("MGNE.Tabs.daily") },
      equipment: { id: "equipment", group: "sheet", label: game.i18n.localize("MGNE.Tabs.equipment") },
      features: { id: "features", group: "sheet", label: game.i18n.localize("MGNE.Tabs.features") },
      notes: { id: "notes", group: "sheet", label: game.i18n.localize("MGNE.Tabs.notes") },
    }
    for (const tab of Object.values(tabs)) {
      tab.active = this.tabGroups[tab.group] === tab.id
      tab.cssClass = tab.active ? "active" : ""
    }
    return tabs
  }

  async _prepareContext() {
    const context = await super._prepareContext()
    context.tabs = this.getTabs()
    context.abilityList = SYSTEM.abilityOrder.map(id => ({
      id,
      ...SYSTEM.abilities[id],
      value: context.source.system.abilities?.[id]?.value ?? 0,
    }))
    context.selectOptions = {
      ...context.selectOptions,
      ...buildCharacterSelectOptions(context.system),
    }
    return context
  }

  async _preparePartContext(partId, context) {
    const doc = this.document
    switch (partId) {
      case "overview":
        context.tab = context.tabs.overview
        context.valueConditions = Object.entries(doc.system.conditions ?? {})
          .filter(([id]) => SYSTEM.conditions[id]?.hasValue)
          .map(([id, cond]) => ({
            id,
            label: SYSTEM.conditions[id].label,
            value: cond.value,
            options: context.selectOptions.conditionValues,
          }))
        context.flagConditions = Object.entries(doc.system.conditions ?? {})
          .filter(([id]) => !SYSTEM.conditions[id]?.hasValue)
          .map(([id, cond]) => ({
            id,
            label: SYSTEM.conditions[id].label,
            active: cond.active,
          }))
        break
      case "daily":
        context.tab = context.tabs.daily
        break
      case "equipment":
        context.tab = context.tabs.equipment
        context.weapons = doc.itemTypes.weapon
        context.armors = doc.itemTypes.armor
        context.shields = doc.itemTypes.shield
        context.equipmentItems = doc.itemTypes.equipment
        context.cores = doc.itemTypes["resonance-core"]
        context.artifacts = doc.itemTypes.artifact
        break
      case "features":
        context.tab = context.tabs.features
        context.features = doc.itemTypes.feature
        break
      case "notes":
        context.tab = context.tabs.notes
        break
    }
    return context
  }
}
