import { SYSTEM_RDD, SYSTEM_SOCKET_ID, RDD_CONFIG } from "./constants.js"
import { Migrations } from './migrations.js'

import { RdDUtility } from "./rdd-utility.js"
import { TMRUtility } from "./tmr-utility.js"
import { TMRRencontres } from "./tmr-rencontres.js"
import { RdDCalendrier } from "./time/rdd-calendrier.js"
import { RdDTimestamp } from "./time/rdd-timestamp.js"
import { DialogChronologie } from "./dialog-chronologie.js"

import { RdDResolutionTable } from "./rdd-resolution-table.js"
import { RdDTokenHud } from "./rdd-token-hud.js"
import { RdDCommands } from "./rdd-commands.js"
import { RdDCombatManager, RdDCombat } from "./rdd-combat.js"
import { ChatUtility } from "./chat-utility.js"
import { StatusEffects } from "./settings/status-effects.js"
import { RdDCompendiumOrganiser } from "./rdd-compendium-organiser.js"
import { ReglesOptionnelles } from "./settings/regles-optionnelles.js"
import { RdDHotbar } from "./rdd-hotbar-drop.js"
import { EffetsDraconiques } from "./tmr/effets-draconiques.js"
import { RdDDice } from "./rdd-dice.js"
import { RdDPossession } from "./rdd-possession.js"
import { Misc } from "./misc.js"

import { SystemCompendiums } from "./settings/system-compendiums.js"
import { Environnement } from "./environnement.js"

import { RdDActor } from "./actor.js"
import { RdDBaseActor } from "./actor/base-actor.js"
import { RdDCreature } from "./actor/creature.js"
import { RdDCommerce } from "./actor/commerce.js"
import { RdDEntite } from "./actor/entite.js"
import { RdDVehicule } from "./actor/vehicule.js"

import { RdDActorSheet } from "./actor-sheet.js"
import { RdDCommerceSheet } from "./actor/commerce-sheet.js"
import { RdDCreatureSheet } from "./actor/creature-sheet.js"
import { RdDActorEntiteSheet } from "./actor/entite-sheet.js"
import { RdDActorVehiculeSheet } from "./actor/vehicule-sheet.js"

import { RdDItem } from "./item.js"
import { RdDItemArmure } from "./item/armure.js"
import { RdDItemBlessure } from "./item/blessure.js"
import { RdDItemService } from "./item/service.js"
import { RdDItemMaladie } from "./item/maladie.js"
import { RdDItemPoison } from "./item/poison.js"
import { RdDItemSigneDraconique } from "./item/signedraconique.js"
import { RdDItemQueue } from "./item/queue.js"
import { RdDItemOmbre } from "./item/ombre.js"
import { RdDItemSort } from "./item-sort.js"
import { RdDItemTete } from "./item/tete.js"
import { RdDItemRace } from "./item/race.js"
import { RdDItemSouffle } from "./item/souffle.js"

import { RdDRencontre } from "./item/rencontre.js"

import { RdDItemSheet } from "./item-sheet.js"
import { RdDBlessureItemSheet } from "./item/sheet-blessure.js"
import { RdDServiceItemSheet } from "./item/sheet-service.js"
import { RdDRencontreItemSheet } from "./item/sheet-rencontre.js"
import { RdDHerbeItemSheet } from "./item/sheet-herbe.js"
import { RdDPlanteItemSheet } from "./item/sheet-plante.js"
import { RdDIngredientItemSheet } from "./item/sheet-ingredient.js"
import { RdDFauneItemSheet } from "./item/sheet-faune.js"
import { RdDConteneurItemSheet } from "./item/sheet-conteneur.js"
import { RdDItemInventaireSheet } from "./item/sheet-base-inventaire.js"
import { RdDSigneDraconiqueItemSheet } from "./item/sheet-signedraconique.js"

import { AppAstrologie } from "./sommeil/app-astrologie.js"
import { AutoAdjustDarkness } from "./time/auto-adjust-darkness.js"
import { RdDTMRDialog } from "./rdd-tmr-dialog.js"
import { OptionsAvancees } from "./settings/options-avancees.js"
import { ExportScriptarium } from "./actor/export-scriptarium/export-scriptarium.js"
import { AppPersonnageAleatoire } from "./actor/random/app-personnage-aleatoire.js"
import { RdDActorExportSheet } from "./actor/export-scriptarium/actor-encart-sheet.js"
import { RdDStatBlockParser } from "./apps/rdd-import-stats.js"
import { RdDJournalSheet } from "./journal/journal-sheet.js"
import { RdDPotionItemSheet } from "./item/sheet-potion.js"
import { RdDItemPotion } from "./item/potion.js"
import { RdDItemGemme } from "./item/gemme.js"
import { RdDGemmeItemSheet } from "./item/sheet-gemme.js"

/**
 * RdD system
 * Author: LeRatierBretonnien
 * Software License: GNU GPLv3
 */
export class SystemReveDeDragon {

  static start() {
    const system = new SystemReveDeDragon()
    Hooks.once('init', () => system.onInit())
    Hooks.once('diceSoNiceReady', (dice3d) => RdDDice.diceSoNiceReady(dice3d))
    Hooks.once('ready', () => system.onReady())
  }

  constructor() {
    this.config = RDD_CONFIG
    this.RdDUtility = RdDUtility
    this.RdDHotbar = RdDHotbar
    this.RdDStatBlockParser = RdDStatBlockParser
    this.itemClasses = {
      armure: RdDItemArmure,
      blessure: RdDItemBlessure,
      gemme: RdDItemGemme,
      maladie: RdDItemMaladie,
      ombre: RdDItemOmbre,
      poison: RdDItemPoison,
      potion: RdDItemPotion,
      queue: RdDItemQueue,
      race: RdDItemRace,
      rencontre: RdDRencontre,
      service: RdDItemService,
      signedraconique: RdDItemSigneDraconique,
      souffle: RdDItemSouffle,
      tete: RdDItemTete,
    }
    this.actorClasses = {
      commerce: RdDCommerce,
      creature: RdDCreature,
      entite: RdDEntite,
      personnage: RdDActor,
      vehicule: RdDVehicule,
    }
  }

  /* -------------------------------------------- */
  /*  Foundry VTT Initialization                  */
  /* -------------------------------------------- */
  onInit() {
    game.system.rdd = this
    this.AppAstrologie = AppAstrologie

    console.log(`Initializing Reve de Dragon System Settings`)

    // preload handlebars templates
    RdDUtility.preloadHandlebarsTemplates()
    AppPersonnageAleatoire.preloadHandlebars()
    RdDItemSort.preloadHandlebars()

    /* -------------------------------------------- */
    ReglesOptionnelles.initSettings()
    OptionsAvancees.initSettings()
    AutoAdjustDarkness.initSettings()
    RdDTimestamp.initSettings()
    RdDCalendrier.initSettings()
    SystemCompendiums.initSettings()
    DialogChronologie.initSettings()
    EffetsDraconiques.initSettings()
    RdDTMRDialog.initSettings()
    Environnement.initSettings()

    this.initSettings()

    /* -------------------------------------------- */
    // Set an initiative formula for the system 
    CONFIG.Combat.initiative = { formula: "1+(1d6/10)", decimals: 2 }

    /* -------------------------------------------- */
    console.log(`Initializing Reve de Dragon Socket handlers`)
    game.socket.on(SYSTEM_SOCKET_ID, async (sockmsg) => {
      console.log(">>>>> MSG RECV", sockmsg)
      try {
        RdDUtility.onSocketMessage(sockmsg)
        RdDCombat.onSocketMessage(sockmsg)
        ChatUtility.onSocketMessage(sockmsg)
        RdDBaseActor.onSocketMessage(sockmsg)
      } catch (e) {
        console.error('game.socket.on(SYSTEM_SOCKET_ID) Exception: ', sockmsg, ' => ', e)
      }
    })

    /* -------------------------------------------- */
    // Define custom Entity classes
    console.log(`Initializing Reve de Dragon Documents`)
    CONFIG.Actor.documentClass = RdDBaseActor
    CONFIG.Item.documentClass = RdDItem
    CONFIG.RDD = {
      resolutionTable: RdDResolutionTable.resolutionTable,
      carac_array: RdDUtility.getCaracArray(),
      ajustementsConditions: RdDUtility.getAjustementsConditions(),
      difficultesLibres: RdDUtility.getDifficultesLibres()
    }

    /* -------------------------------------------- */
    // Register sheet application classes
    Actors.unregisterSheet("core", ActorSheet)
    Actors.registerSheet(SYSTEM_RDD, RdDCommerceSheet, { types: ["commerce"], makeDefault: true })
    Actors.registerSheet(SYSTEM_RDD, RdDActorSheet, { types: ["personnage"], makeDefault: true })
    Actors.registerSheet(SYSTEM_RDD, RdDCreatureSheet, { types: ["creature"], makeDefault: true })
    Actors.registerSheet(SYSTEM_RDD, RdDActorVehiculeSheet, { types: ["vehicule"], makeDefault: true })
    Actors.registerSheet(SYSTEM_RDD, RdDActorEntiteSheet, { types: ["entite"], makeDefault: true })
    Items.unregisterSheet("core", ItemSheet)
    RdDActorExportSheet.init()

    Items.registerSheet(SYSTEM_RDD, RdDItemInventaireSheet, {
      types: [
        "objet", "arme", "armure", "livre", "munition",
        "monnaie", "nourritureboisson",
      ],
      makeDefault: true
    })
    Items.registerSheet(SYSTEM_RDD, RdDItemSheet, {
      types: [
        "competence", "competencecreature",
        "recettealchimique", "musique", "chant", "danse", "jeu", "race",
        "recettecuisine", "oeuvre", "meditation",
        "queue", "ombre", "souffle", "tete", "casetmr", "sort", "sortreserve",
        "nombreastral", "tache", "maladie", "poison", "possession",
        "tarot", "extraitpoetique", "empoignade"
      ],
      makeDefault: true
    })
    
    RdDItemSheet.register(RdDBlessureItemSheet)
    RdDItemSheet.register(RdDConteneurItemSheet)
    RdDItemSheet.register(RdDFauneItemSheet)
    RdDItemSheet.register(RdDGemmeItemSheet)
    RdDItemSheet.register(RdDHerbeItemSheet)
    RdDItemSheet.register(RdDIngredientItemSheet)
    RdDItemSheet.register(RdDPlanteItemSheet)
    RdDItemSheet.register(RdDPotionItemSheet)
    RdDItemSheet.register(RdDRencontreItemSheet)
    RdDItemSheet.register(RdDServiceItemSheet)
    RdDItemSheet.register(RdDSigneDraconiqueItemSheet)
    RdDJournalSheet.register()

    // préparation des différents modules
    console.log(`Initializing Reve de Dragon Hooks and handlers`)
    CONFIG.Combat.documentClass = RdDCombatManager
    ChatUtility.init()
    RdDUtility.initHooks()
    RdDDice.init()
    RdDCommands.init()
    RdDCombatManager.init()
    RdDTokenHud.init()
    RdDBaseActor.init()
    RdDCompendiumOrganiser.init()
    EffetsDraconiques.init()
    TMRUtility.init()
    RdDHotbar.initHooks()
    RdDPossession.init()
    TMRRencontres.init()
    ExportScriptarium.init()
  }

  initSettings() {
    // TODO: déplacer vers les modules correspondants
    game.settings.register(SYSTEM_RDD, "accorder-entite-cauchemar", {
      name: "Accorder le rêve aux entités",
      hint: "A quel moment les personnages doivent accorder leur rêve aux entités de cauchemar",
      scope: "world",
      config: true,
      type: String,
      choices: {
        "avant-attaque": "Avant l'attaque",
        "avant-defense": "Avant la défense",
        "avant-encaissement": "Avant l'encaissement",
      },
      default: "avant-encaissement"
    })

    /* -------------------------------------------- */
    game.settings.register(SYSTEM_RDD, "supprimer-dialogues-combat-chat", {
      name: "Supprimer les dialogues de combat",
      hint: "Si désactivée, tous les dialogues de combat sont conservés dans la conversation",
      scope: "world",
      config: true,
      default: true,
      type: Boolean
    })

    /* -------------------------------------------- */
    game.settings.register(SYSTEM_RDD, "activer-sons-audio", {
      name: "Activer les bruitages intégrés",
      hint: "Si activé, certaines actions en jeu déclenchent un son d'ambiance",
      scope: "world",
      config: true,
      default: true,
      type: Boolean
    })

    /* -------------------------------------------- */
    game.settings.register(SYSTEM_RDD, "appliquer-famine-soif", {
      name: "Notifier de la famine et la soif pour",
      hint: "Indique si les cas de famine et de soif seront indiqués durant Château Dormant",
      scope: "world",
      config: true,
      type: String,
      choices: {
        "aucun": "ni la famine, ni la soif",
        "famine": "seulement la famine",
        "famine-soif": "la famine et la soif",
      },
      default: "aucun"
    })
  }

  static async setupAccueil() {
    let exists = game.scenes.find(j => j.name == "Accueil RdD");
    if (!exists) {
      const scenes = await SystemCompendiums.loadCompendium("foundryvtt-reve-de-dragon.scenes-rdd")
      let newDocuments = scenes.filter(i => i.name == "Accueil RdD");
      await game.scenes.documentClass.create(newDocuments);
      game.scenes.find(i => i.name == "Accueil RdD").activate();
    }
  }

  async onReady() {

    /* -------------------------------------------- */
    /*  Foundry VTT Initialization                  */
    /* -------------------------------------------- */
    game.system.rdd.calendrier = new RdDCalendrier()
    if (Misc.isFirstConnectedGM()) {
      new Migrations().migrate()
      this.messageDeBienvenue()
      import("https://www.uberwald.me/fvtt_appcount/count-class-ready.js").then(moduleCounter => {
        console.log("ClassCounter loaded", moduleCounter)
        moduleCounter.ClassCounter.registerUsageCount()
      }).catch(err =>
        console.log("No stats available, giving up.")
      )
    }

    StatusEffects.onReady()
    RdDDice.onReady()

    RdDStatBlockParser.parseStatBlock()
    /* -------------------------------------------- */
    /* Affiche/Init le calendrier */
    game.system.rdd.calendrier.display()
    // Avertissement si joueur sans personnage
    if (!game.user.isGM && game.user.character == undefined) {
      ui.notifications.info("Attention ! Vous n'êtes connecté à aucun personnage !")
      ChatMessage.create({
        content: "<b>ATTENTION</b> Le joueur " + game.user.name + " n'est connecté à aucun personnage !",
        user: game.user.id
      })
    }

    SystemReveDeDragon.setupAccueil()
  }

  /* -------------------------------------------- */
  messageDeBienvenue() {
    if (game.user.isGM) {
      ChatUtility.removeChatMessageContaining('<div id="message-bienvenue-rdd">')
      ChatMessage.create({
        user: game.user.id,
        content: `<div id="message-bienvenue-rdd"><span class="rdd-roll-part">Bienvenue dans le Rêve des Dragons !</span>
      <br>Vous trouverez quelques informations pour démarrer dans ce document : @Compendium[foundryvtt-reve-de-dragon.rappel-des-regles.7uGrUHGdPu0EmIu2]{Documentation MJ/Joueurs}
      <br>La commande <code>/aide</code> dans le chat permet de voir les commandes spécifiques à Rêve de Dragon.</div>
      ` })
    }
  }
}

SystemReveDeDragon.start()
