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

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

/* -------------------------------------------- */
// Import Modules
import { DarkStarsActor } from "./dark-stars-actor.js";
import { DarkStarsItemSheet } from "./dark-stars-item-sheet.js";
import { DarkStarsActorSheet } from "./dark-stars-actor-sheet.js";
import { DarkStarsNPCSheet } from "./dark-stars-npc-sheet.js";
import { DarkStarsUtility } from "./dark-stars-utility.js";
import { DarkStarsCombat } from "./dark-stars-combat.js";
import { DarkStarsItem } from "./dark-stars-item.js";
import { DarkStarsHotbar } from "./dark-stars-hotbar.js"
import { DarkStarsCommands } from "./dark-stars-commands.js"
import { DARKSTARS_CONFIG } from "./dark-stars-config.js";

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

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

  console.log(`Initializing DarkStars RPG`);
  
  game.system.darkstars = {
    DarkStarsCommands,
    config: DARKSTARS_CONFIG
  }

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

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

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

  /* -------------------------------------------- */
  // Define custom Entity classes
  CONFIG.Combat.documentClass = DarkStarsCombat
  CONFIG.Actor.documentClass = DarkStarsActor
  CONFIG.Item.documentClass = DarkStarsItem
  
  /* -------------------------------------------- */
  // Register sheet application classes
  Actors.unregisterSheet("core", ActorSheet);
  Actors.registerSheet("fvtt-dark-stars", DarkStarsActorSheet, { types: ["character"], makeDefault: true });
  Actors.registerSheet("fvtt-dark-stars", DarkStarsNPCSheet, { types: ["npc"], makeDefault: false });

  Items.unregisterSheet("core", ItemSheet);
  Items.registerSheet("fvtt-dark-stars", DarkStarsItemSheet, { makeDefault: true });

  DarkStarsUtility.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 the DarkStars 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
    });
  }
  
  // CSS patch for v9
  if (game.version) {
    let sidebar = document.getElementById("sidebar");
    sidebar.style.width = "min-content";
  }

  welcomeMessage();
  DarkStarsUtility.ready()
  DarkStarsCommands.init()
})

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

