/**
 * RdD system
 * Author: Uberwald
 * Software License: Prop
 */

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

/* -------------------------------------------- */
// Import Modules
import { VadentisActor } from "./vadentis-actor.js";
import { VadentisItemSheet } from "./vadentis-item-sheet.js";
import { VadentisActorSheet } from "./vadentis-actor-sheet.js";
import { VadentisUtility } from "./vadentis-utility.js";
import { VadentisCombat } from "./vadentis-combat.js";

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

/************************************************************************************/
Hooks.once("init", async function () {
  console.log(`Initializing Vadentis`);

  /* -------------------------------------------- */
  // preload handlebars templates
  VadentisUtility.preloadHandlebarsTemplates();

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

  /* -------------------------------------------- */
  game.socket.on("system.foundryvtt-vadentis", data => {
    VadentisUtility.onSocketMesssage(data);
  });

  /* -------------------------------------------- */
  // Define custom Entity classes
  CONFIG.Actor.entityClass = VadentisActor;
  CONFIG.Combat.entityClass = VadentisCombat;
  CONFIG.Vadentis = {
  }

  /* -------------------------------------------- */
  // Register sheet application classes
  Actors.unregisterSheet("core", ActorSheet);
  Actors.registerSheet("foundryvtt-vadentis", VadentisActorSheet, { types: ["personnage"], makeDefault: true });
  Items.unregisterSheet("core", ItemSheet);
  Items.registerSheet("foundryvtt-vadentis", VadentisItemSheet, { makeDefault: true });

  // Init/registers
  Hooks.on('renderChatLog', (log, html, data) => {
    VadentisUtility.registerChatCallbacks(html);
  });
  // Init/registers
  Hooks.on('updateCombat', (combat, round, diff, id) => {
    VadentisUtility.updateCombat(combat, round, diff, id);
  });
  
});

/* -------------------------------------------- */
function welcomeMessage() {
  //ChatUtility.removeMyChatMessageContaining('<div id="welcome-message-sos">');
  ChatMessage.create({
    user: game.user._id,
    whisper: [game.user._id],
    content: `<div id="welcome-message-vadentis"><span class="rdd-roll-part">Bienvenue !</div>
    ` });
}

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

  // User warning
  if (!game.user.isGM && game.user.character == undefined) {
    ui.notifications.info("Attention ! Vous n'est connecté à aucun personnage");
    ChatMessage.create({
      content: "<b>WARNING</b> Le joueur  " + game.user.name + " n'est pas connecté à un personnage !",
      user: game.user._id
    });
  }
  welcomeMessage();
});

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