import { SYSTEM } from "./module/config/system.mjs"
globalThis.SYSTEM = SYSTEM

import * as models from "./module/models/_module.mjs"
import * as documents from "./module/documents/_module.mjs"
import * as applications from "./module/applications/_module.mjs"

Hooks.once("init", function () {
  console.info("Adventures with Emmy | Initializing System")
  console.info(SYSTEM.ASCII)

  globalThis.adventuresWithEmmy = game.system
  game.system.CONST = SYSTEM

  game.system.api = { applications, models, documents }

  CONFIG.Actor.documentClass = documents.AwEActor
  CONFIG.Actor.dataModels = {
    character: models.AwECharacter,
    creature: models.AwECreature
  }

  CONFIG.Item.documentClass = documents.AwEItem
  CONFIG.Item.dataModels = {
    ability: models.AwEAbility,
    field: models.AwEField,
    specialization: models.AwESpecialization,
    archetype: models.AwEArchetype,
    background: models.AwEBackground,
    kit: models.AwEKit,
    weapon: models.AwEWeapon,
    equipment: models.AwEEquipment
  }

  // Register actor sheets
  foundry.documents.collections.Actors.unregisterSheet("core", foundry.appv1.sheets.ActorSheet)
  foundry.documents.collections.Actors.registerSheet("fvtt-adventures-with-emmy", applications.AwECharacterSheet, {
    types: ["character"], makeDefault: true
  })
  foundry.documents.collections.Actors.registerSheet("fvtt-adventures-with-emmy", applications.AwECreatureSheet, {
    types: ["creature"], makeDefault: true
  })

  // Register item sheets
  foundry.documents.collections.Items.unregisterSheet("core", foundry.appv1.sheets.ItemSheet)
  foundry.documents.collections.Items.registerSheet("fvtt-adventures-with-emmy", applications.AwEAbilitySheet, {
    types: ["ability"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("fvtt-adventures-with-emmy", applications.AwEFieldSheet, {
    types: ["field"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("fvtt-adventures-with-emmy", applications.AwESpecializationSheet, {
    types: ["specialization"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("fvtt-adventures-with-emmy", applications.AwEArchetypeSheet, {
    types: ["archetype"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("fvtt-adventures-with-emmy", applications.AwEBackgroundSheet, {
    types: ["background"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("fvtt-adventures-with-emmy", applications.AwEKitSheet, {
    types: ["kit"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("fvtt-adventures-with-emmy", applications.AwEWeaponSheet, {
    types: ["weapon"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("fvtt-adventures-with-emmy", applications.AwEEquipmentSheet, {
    types: ["equipment"], makeDefault: true
  })

  CONFIG.ChatMessage.documentClass = documents.AwEChatMessage
  CONFIG.Dice.rolls.push(documents.AwERoll)
  CONFIG.Combatant.documentClass = documents.AwECombatant

  // Handlebars helpers
  Handlebars.registerHelper("abs", (value) => Math.abs(value ?? 0))
})

Hooks.once("ready", function () {
  console.info("Adventures with Emmy | System Ready")
})
