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

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

/* -------------------------------------------- */
// Import Modules
import { Hero6Actor } from "./hero6-actor.js";
import { Hero6ItemSheet } from "./hero6-item-sheet.js";
import { Hero6ActorSheet } from "./hero6-actor-sheet.js";
import { Hero6NPCSheet } from "./hero6-npc-sheet.js";
import { Hero6Utility } from "./hero6-utility.js";
import { Hero6Combat, Hero6CombatTracker } from "./hero6-combat.js";
import { Hero6Item } from "./hero6-item.js";
import { Hero6Hotbar } from "./hero6-hotbar.js"
import { Hero6Commands } from "./hero6-commands.js"
import { Hero6_CONFIG } from "./hero6-config.js";

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

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

  console.log(`Initializing Hero6 RPG`);
  
  game.system.hero6 = {
    Hero6Commands,
    config: Hero6_CONFIG
  }

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

  /* -------------------------------------------- */
  // Set an initiative formula for the system 
  CONFIG.Combat.initiative = {
    formula: "1d6",
    decimals: 2
  };
  
  /* -------------------------------  ------------- */
  game.socket.on("system.fvtt-hero-system-6", data => {
    Hero6Utility.onSocketMesssage(data)
  });

  /* -------------------------------------------- */
  // Define custom Entity classes
  CONFIG.Combat.documentClass = Hero6Combat
  CONFIG.Actor.documentClass = Hero6Actor
  CONFIG.Item.documentClass = Hero6Item
  CONFIG.ui.combat = Hero6CombatTracker;

  
  /* -------------------------------------------- */
  // Register sheet application classes
  Actors.unregisterSheet("core", ActorSheet);
  Actors.registerSheet("fvtt-hero-system-6", Hero6ActorSheet, { types: ["character"], makeDefault: true });
  Actors.registerSheet("fvtt-hero-system-6", Hero6ActorSheet, { types: ["minion"], makeDefault: false });

  Items.unregisterSheet("core", ItemSheet);
  Items.registerSheet("fvtt-hero-system-6", Hero6ItemSheet, { makeDefault: true });

  Hero6Utility.init()
});

/* -------------------------------------------- */
function welcomeMessage() {
  ChatMessage.create({
    user: game.user.id,
    whisper: [game.user.id],
    content: `<div id="welcome-message-dark-stars"><span class="rdd-roll-part">
    <strong>Welcome to Hero System 6E RPG.</strong>
    ` });
}

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

  // User warning
  if (!game.user.isGM && game.user.character == undefined) {
    ui.notifications.info("Warning ! No character linked to your user !");
    ChatMessage.create({
      content: "<b>WARNING</b> The player  " + game.user.name + " is not linked to a character !",
      user: game.user._id
    });
  }
  
  welcomeMessage();
  Hero6Utility.ready()
  Hero6Commands.ready()
  Hero6Combat.ready()
  
})

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

