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

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

/* -------------------------------------------- */
// Import Modules
import { YggdrasillActor } from "./yggdrasill-actor.js";
import { YggdrasillItemSheet } from "./yggdrasill-item-sheet.js";
import { YggdrasillActorSheet } from "./yggdrasill-actor-sheet.js";
import { YggdrasillFigurantSheet } from "./yggdrasill-figurant-sheet.js";
import { YggdrasillUtility } from "./yggdrasill-utility.js";
import { YggdrasillCombat } from "./yggdrasill-combat.js";

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

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

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

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

  /* -------------------------------------------- */
  game.socket.on("system.fvtt-yggdrasill", data => {
    YggdrasillUtility.onSocketMesssage(data);
  });

  /* -------------------------------------------- */
  // Define custom Entity classes
  CONFIG.Actor.documentClass = YggdrasillActor;
  CONFIG.Combat.documentClass = YggdrasillCombat;
  CONFIG.Yggdrasill = {
  }

  /* -------------------------------------------- */
  // Register sheet application classes
  Actors.unregisterSheet("core", ActorSheet);
  Actors.registerSheet("fvtt-yggdrasill", YggdrasillActorSheet, { types: ["personnage"], makeDefault: true });
  Actors.registerSheet("fvtt-yggdrasill", YggdrasillFigurantSheet, { types: ["figurant"], makeDefault: false });
  Items.unregisterSheet("core", ItemSheet);
  Items.registerSheet("fvtt-yggdrasill", YggdrasillItemSheet, { makeDefault: true });

  // Init/registers
  Hooks.on('renderChatLog', (log, html, data) => {
    //YggdrasillUtility.registerChatCallbacks(html);
  });
  
});

/* -------------------------------------------- */
function welcomeMessage() {
  //ChatUtility.removeMyChatMessageContaining('<div id="welcome-message-sos">');
  ChatMessage.create({
    user: game.user.id,
    whisper: [game.user.id],
    content: `<div id="welcome-message-yggdrasill"><span class="rdd-roll-part"><strong>Bienvenue à Yggdrasill !</strong>
    <p>Yggdrasill est un Jeu de Rôle publié par 7ième Cercle : https://www.7emecercle.com/7C_site/jeux-de-roles/yggdrasill/</p>
    <br>Yggdrasill est une propriété de 7ième Cercle.
    </div>
    ` });
}

/* -------------------------------------------- */
// Register world usage statistics
function registerUsageCount( registerKey ) {
  if ( game.user.isGM ) {
    game.settings.register(registerKey, "world-key", {
      name: "Unique world key",
      scope: "world",
      config: false,
      default: "",
      type: String
    });

    let worldKey = game.settings.get(registerKey, "world-key")
    if ( worldKey == undefined || worldKey == "" ) {
      worldKey = randomID(32)
      game.settings.set(registerKey, "world-key", worldKey )
    }
    let regURL = `https://www.uberwald.me/fvtt_appcount/count.php?name="${registerKey}"&worldKey="${worldKey}"&version="${game.release.generation}.${game.release.build}"&system="${game.system.id}"&systemversion="${game.system.version}"`
    $.ajax(regURL)
    /* -------------------------------------------- */
  }
}

/* -------------------------------------------- */
/*  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
    });
  }
  registerUsageCount("fvtt-yggdrasill")
  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);
  }
  return true;
});
