// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Oliver Rümpelein
// SPDX-FileCopyrightText: 2021 Gesina Schwalbe
//
// SPDX-License-Identifier: MIT

import { DS4ActiveEffectConfig } from "../apps/active-effect-config";
import { DS4CharacterActorSheet } from "../apps/actor/character-sheet";
import { DS4CreatureActorSheet } from "../apps/actor/creature-sheet";
import { DS4ItemSheet } from "../apps/item-sheet";
import { DS4 } from "../config";
import { DS4Check } from "../dice/check";
import { createCheckRoll } from "../dice/check-factory";
import { DS4Roll } from "../dice/roll";
import { registerSlayingDiceModifier } from "../dice/slaying-dice-modifier";
import { DS4ActiveEffect } from "../documents/active-effect";
import { DS4ActorProxy } from "../documents/actor/proxy";
import { DS4ChatMessage } from "../documents/chat-message";
import { DS4ItemProxy } from "../documents/item/proxy";
import { DS4TokenDocument } from "../documents/token-document";
import { registerHandlebarsHelpers } from "../handlebars/handlebars-helpers";
import { registerHandlebarsPartials } from "../handlebars/handlebars-partials";
import { macros } from "../macros/macros";
import { migration } from "../migration/migration";
import { registerSystemSettings } from "../settings";
import { preloadFonts } from "../ui/fonts";
import { logger } from "../utils/logger";
import { getGame } from "../utils/utils";

export function registerForInitHook() {
  Hooks.once("init", init);
}

async function init() {
  logger.info(`Initializing the DS4 Game System\n${DS4.ASCII}`);

  getGame().ds4 = {
    DS4Actor: DS4ActorProxy,
    DS4Item: DS4ItemProxy,
    DS4,
    createCheckRoll,
    migration,
    macros,
  };

  CONFIG.DS4 = DS4;

  CONFIG.Actor.documentClass = DS4ActorProxy;
  CONFIG.Item.documentClass = DS4ItemProxy;
  CONFIG.ActiveEffect.documentClass = DS4ActiveEffect;
  CONFIG.ChatMessage.documentClass = DS4ChatMessage;
  CONFIG.Token.documentClass = DS4TokenDocument;

  CONFIG.ActiveEffect.legacyTransferral = false;

  CONFIG.Actor.typeLabels = DS4.i18n.actorTypes;
  CONFIG.Item.typeLabels = DS4.i18n.itemTypes;

  CONFIG.Dice.types.push(DS4Check);
  CONFIG.Dice.terms.s = DS4Check;

  CONFIG.Dice.rolls.unshift(DS4Roll);

  registerSlayingDiceModifier();

  registerSystemSettings();

  DocumentSheetConfig.unregisterSheet(Actor, "core", ActorSheet);
  DocumentSheetConfig.registerSheet(Actor, "ds4", DS4CharacterActorSheet, {
    types: ["character"],
    makeDefault: true,
  });
  DocumentSheetConfig.registerSheet(Actor, "ds4", DS4CreatureActorSheet, { types: ["creature"], makeDefault: true });
  DocumentSheetConfig.unregisterSheet(Item, "core", ItemSheet);
  DocumentSheetConfig.registerSheet(Item, "ds4", DS4ItemSheet, { makeDefault: true });
  DocumentSheetConfig.unregisterSheet(ActiveEffect, "core", ActiveEffectConfig);
  DocumentSheetConfig.registerSheet(ActiveEffect, "ds4", DS4ActiveEffectConfig, { makeDefault: true });

  preloadFonts();
  await registerHandlebarsPartials();
  registerHandlebarsHelpers();
}
