/**
 * Te Deum system
 * Author: Uberwald
 * Software License: Prop
 */

/* -------------------------------------------- */

/* -------------------------------------------- */
// Import Modules
import { TeDeumActor } from "./actors/tedeum-actor.js";
import { TeDeumActorPJSheet } from "./actors/tedeum-actor-sheet.js";

import { TeDeumPJSchema } from "./data/tedeum-schema-pj.js";
import { TeDeumArmeSchema } from "./data/tedeum-schema-arme.js";
import { TeDeumArmureSchema } from "./data/tedeum-schema-armure.js";
import { TeDeumCompetenceSchema } from "./data/tedeum-schema-competence.js";
import { TeDeumEquipementSchema } from "./data/tedeum-schema-equipement.js";
import { TeDeumOrigineSchema } from "./data/tedeum-schema-origine.js";
import { TeDeumEducationSchema } from "./data/tedeum-schema-education.js";
import { TeDeumGraceSchema } from "./data/tedeum-schema-grace.js";
import { TeDeumBlessureSchema } from "./data/tedeum-schema-blessure.js";
import { TeDeumMaladieSchema } from "./data/tedeum-schema-maladie.js";

import { TeDeumItem } from "./items/tedeum-item.js";
import { TeDeumItemSheet } from "./items/tedeum-item-sheet.js";

import { TeDeumHotbar } from "./app/tedeum-hotbar.js"
import { TeDeumCombat } from "./app/tedeum-combat.js";

import { TeDeumUtility } from "./common/tedeum-utility.js";
import { TEDEUM_CONFIG } from "./common/tedeum-config.js";

/* -------------------------------------------- */
/*  Foundry VTT Initialization                  */
/* -------------------------------------------- */

/************************************************************************************/
Hooks.once("init", async function () {

  console.log(`Initializing TeDeum RPG`);

  game.system.tedeum = {
    config: TEDEUM_CONFIG,
    TeDeumHotbar
  }
  console.log(`Initializing TeDeum RPG 2`);

  // preload handlebars templates
  TeDeumUtility.preloadHandlebarsTemplates();

  // Set an initiative formula for the system 
  CONFIG.Combat.initiative = {
    formula: "1d6",
    decimals: 1
  };

  game.socket.on("system.fvtt-te-deum", data => {
    TeDeumUtility.onSocketMesssage(data)
  });

  //CONFIG.Combat.documentClass = TeDeumCombat
  CONFIG.Actor.documentClass = TeDeumActor;
  CONFIG.Item.documentClass = TeDeumItem
  CONFIG.Actor.dataModels = {
    pj: TeDeumPJSchema,
    pnj: TeDeumPJSchema,
  };
  CONFIG.Item.dataModels = {
    arme: TeDeumArmeSchema,
    competence: TeDeumCompetenceSchema,
    equipement: TeDeumEquipementSchema,
    armure: TeDeumArmureSchema,
    origine: TeDeumOrigineSchema,
    education: TeDeumEducationSchema,
    grace: TeDeumGraceSchema,
    blessure: TeDeumBlessureSchema,
    maladie: TeDeumMaladieSchema,
  };
  
  console.log("TeDeum RPG | Ready");

  Actors.unregisterSheet("core", ActorSheet);
  Actors.registerSheet("fvtt-te-deum", TeDeumActorPJSheet, { types: ["pj"], makeDefault: true });
  Actors.registerSheet("fvtt-te-deum", TeDeumActorPJSheet, { types: ["pnj"], makeDefault: true });
  
  Items.unregisterSheet("core", ItemSheet);
  Items.registerSheet("fvtt-te-deum", TeDeumItemSheet, { makeDefault: true });

  TeDeumUtility.init() 
});

/* -------------------------------------------- */
function welcomeMessage() {
  if (game.user.isGM) {
    ChatMessage.create({
      user: game.user.id,
      whisper: [game.user.id],
      content: `<div id="welcome-message-tedeum"><span class="rdd-roll-part">
      <strong>Bienvenu dans Te Deum Pour Un Massacre !</strong>` });
  }
}

/* -------------------------------------------- */
/*  Foundry VTT Initialization                  */
/* -------------------------------------------- */
Hooks.once("ready", function () {

  // User warning
  if (!game.user.isGM && game.user.character == undefined) {
    ui.notifications.info("Attention ! Aucun personnage relié au joueur !");
    ChatMessage.create({
      content: "<b>WARNING</b> Le joueur  " + game.user.name + " n'est pas relié à un personnage !",
      user: game.user._id
    });
  }
  
  import("https://www.uberwald.me/fvtt_appcount/count-class-ready.js").then(moduleCounter=>{
    console.log("ClassCounter loaded", moduleCounter)
    moduleCounter.ClassCounter.registerUsageCount()
  }).catch(err=>
    console.log("No stats available, giving up.")
  )
  TeDeumUtility.ready();

  welcomeMessage();

})


/* -------------------------------------------- */
/*  Foundry VTT Initialization                  */
/* -------------------------------------------- */
Hooks.on("chatMessage", (html, content, msg) => {
  if (content[0] == '/') {
    let regExp = /(\S+)/g;
    let commands = content.match(regExp);
    if (game.system.tedeum.commands.processChatCommand(commands, content, msg)) {
      return false;
    }
  }
  return true;
});

