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

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

/* -------------------------------------------- */
// Import Modules
import { MaleficesActor } from "./malefices-actor.js";
import { MaleficesUtility } from "./malefices-utility.js";
import { MaleficesCombat } from "./malefices-combat.js";
import { MaleficesItem } from "./malefices-item.js";
import { MaleficesHotbar } from "./malefices-hotbar.js"
import { MaleficesCharacterSummary } from "./malefices-summary-app.js"
import { MALEFICES_CONFIG } from "./malefices-config.js"
import { ClassCounter} from "https://www.uberwald.me/fvtt_appcount/count-class-ready.js"

// Import DataModels
import * as models from "./models/index.mjs"

// Import AppV2 Sheets
import * as sheets from "./applications/sheets/_module.mjs"

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

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

  console.log(`Initializing Malefices RPG`);

  game.system.malefices = {
    config: MALEFICES_CONFIG,
    MaleficesHotbar
  }

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

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

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

  /* -------------------------------------------- */
  // Define custom Entity classes
  CONFIG.Combat.documentClass = MaleficesCombat
  CONFIG.Actor.documentClass = MaleficesActor
  CONFIG.Actor.dataModels = {
    personnage: models.PersonnageDataModel,
    pnj: models.PnjDataModel
  }

  CONFIG.Item.documentClass = MaleficesItem
  CONFIG.Item.dataModels = {
    arme: models.ArmeDataModel,
    equipement: models.EquipementDataModel,
    archetype: models.ArchetypeDataModel,
    tarot: models.TarotDataModel,
    sortilege: models.SortilegeDataModel,
    elementbio: models.ElementbioDataModel
  }

  /* -------------------------------------------- */
  // Register AppV2 Actor Sheets
  foundry.documents.collections.Actors.unregisterSheet("core", foundry.appv1.sheets.ActorSheet);
  foundry.documents.collections.Actors.registerSheet("fvtt-malefices", sheets.MaleficesPersonnageSheet, { types: ["personnage"], makeDefault: true });
  foundry.documents.collections.Actors.registerSheet("fvtt-malefices", sheets.MaleficesNPCActorSheet, { types: ["pnj"], makeDefault: true });

  // Register AppV2 Item Sheets
  foundry.documents.collections.Items.unregisterSheet("core", foundry.appv1.sheets.ItemSheet);
  foundry.documents.collections.Items.registerSheet("fvtt-malefices", sheets.MaleficesArmeSheet, { types: ["arme"], makeDefault: true });
  foundry.documents.collections.Items.registerSheet("fvtt-malefices", sheets.MaleficesEquipementSheet, { types: ["equipement"], makeDefault: true });
  foundry.documents.collections.Items.registerSheet("fvtt-malefices", sheets.MaleficesArchetypeSheet, { types: ["archetype"], makeDefault: true });
  foundry.documents.collections.Items.registerSheet("fvtt-malefices", sheets.MaleficesTarotSheet, { types: ["tarot"], makeDefault: true });
  foundry.documents.collections.Items.registerSheet("fvtt-malefices", sheets.MaleficesSortilegeSheet, { types: ["sortilege"], makeDefault: true });
  foundry.documents.collections.Items.registerSheet("fvtt-malefices", sheets.MaleficesElementbioSheet, { types: ["elementbio"], makeDefault: true });

  MaleficesUtility.init()

});

/* -------------------------------------------- */
async function welcomeMessage() {
  if (game.user.isGM) {
    const content = await foundry.applications.handlebars.renderTemplate(
      'systems/fvtt-malefices/templates/chat/welcome-message.hbs', {}
    )
    ChatMessage.create({
      user: game.user.id,
      whisper: [game.user.id],
      content
    });
  }
}

/* -------------------------------------------- */
/*  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
    });
  }

  ClassCounter.registerUsageCount();
  welcomeMessage();
  MaleficesUtility.ready()
  MaleficesCharacterSummary.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.malefices.commands.processChatCommand(commands, content, msg)) {
      return false;
    }
  }
  return true;
});
