// Import Commons Modules
import { L5R5E } from "./config.js";
import { HelpersL5r5e } from "./helpers.js";
import { SocketHandlerL5r5e } from "./socket-handler.js";
import { RegisterSettings } from "./settings.js";
import { PreloadTemplates } from "./preloadTemplates.js";
import { RegisterHandlebars } from "./handlebars.js";
import HooksL5r5e from "./hooks.js";
// Actors
import { ActorL5r5e } from "./actor.js";
import { CharacterSheetL5r5e } from "./actors/character-sheet.js";
import { NpcSheetL5r5e } from "./actors/npc-sheet.js";
import { RulerL5r5e, TokenRulerL5r5e } from "./tatical-grid-rulers.js";
// Dice and rolls
import { L5rBaseDie } from "./dice/dietype/l5r-base-die.js";
import { AbilityDie } from "./dice/dietype/ability-die.js";
import { RingDie } from "./dice/dietype/ring-die.js";
import { RollL5r5e } from "./dice/roll.js";
import { DicePickerDialog } from "./dice/dice-picker-dialog.js";
import { RollnKeepDialog } from "./dice/roll-n-keep-dialog.js";
import { ChiaroscuroDiceDialog } from "./dice/chiaroscuro-dice-dialog.js";
import { CombatL5r5e } from "./combat.js";
// Items
import { ItemL5r5e } from "./item.js";
import { ItemSheetL5r5e } from "./items/item-sheet.js";
import { ArmorSheetL5r5e } from "./items/armor-sheet.js";
import { WeaponSheetL5r5e } from "./items/weapon-sheet.js";
import { AdvancementSheetL5r5e } from "./items/advancement-sheet.js";
import { PeculiaritySheetL5r5e } from "./items/peculiarity-sheet.js";
import { ArcaneSheetL5r5e } from "./items/arcane-sheet.js";
import { MystereSheetL5r5e } from "./items/mystere-sheet.js";
import { TechniqueEcoleSheetL5r5e } from "./items/technique-ecole-sheet.js";
import { MotInvocationSheetL5r5e } from "./items/mot-invocation-sheet.js";
// JournalEntry
import { JournalL5r5e } from "./journal.js";
import { BaseJournalSheetL5r5e } from "./journals/base-journal-sheet.js";
// Compendium
import { CompendiumDirectoryL5r5e } from "./compendium/l5r5e-compendium-directory.js";
// Specific
import { MigrationL5r5e } from "./migration.js";
import { Storage } from "./storage.js";
// Misc
import { L5r5eHtmlMultiSelectElement } from "./misc/l5r5e-multiselect.js";
import { L5R5eHtmlComboBoxElement } from "./misc/l5r5e-combo-box.js";

window.customElements.define(L5r5eHtmlMultiSelectElement.tagName, L5r5eHtmlMultiSelectElement);
window.customElements.define(L5R5eHtmlComboBoxElement.tagName, L5R5eHtmlComboBoxElement);

/* ------------------------------------ */
/* Initialize system                    */
/* ------------------------------------ */
Hooks.once("init", async () => {
    // ***** Initializing l5r5e *****
    // Ascii art :p
    console.log(
        "  _    ___ ___   ___\n" +
            " | |  | __| _ \\ | __| ___ \n" +
            " | |__|__ \\   / |__ \\/ -_)\n" +
            " |____|___/_|_\\ |___/\\___|\n" +
            " "
    );

    // ***** Config *****
    // Global access to L5R Config
    CONFIG.l5r5e = L5R5E;

    // Setting up sidebar icons
    CONFIG.ChatMessage.sidebarIcon = "l5r5e chatIcon";
    CONFIG.Combat.sidebarIcon = "l5r5e combatIcon";
    CONFIG.Scene.sidebarIcon = "l5r5e sceneIcon";
    CONFIG.Actor.sidebarIcon = "l5r5e actorIcon";
    CONFIG.Item.sidebarIcon = "l5r5e itemIcon";
    CONFIG.JournalEntry.sidebarIcon = "l5r5e journalIcon";
    CONFIG.RollTable.sidebarIcon = "l5r5e rolltableIcon";
    CONFIG.Playlist.sidebarIcon = "l5r5e playlistIcon";
    // Note: We don't have any custom icons here so just append l5r5e and type
    CONFIG.Cards.sidebarIcon += " l5r5e cardsIcon";
    CONFIG.Macro.sidebarIcon += " l5r5e macroIcon";
    
    // The compendium and the settings menu is registered a little different.
    foundry.applications.sidebar.Sidebar.TABS.compendium.icon = "l5r5e compendiumIcon";
    foundry.applications.sidebar.Sidebar.TABS.settings.icon = "l5r5e settingsIcon";

    // Assign custom classes and constants here
    CONFIG.Combat.documentClass = CombatL5r5e;
    CONFIG.Actor.documentClass = ActorL5r5e;
    CONFIG.Actor.sheetClasses = CharacterSheetL5r5e;
    CONFIG.Item.documentClass = ItemL5r5e;
    CONFIG.JournalEntry.documentClass = JournalL5r5e;
    CONFIG.JournalEntry.sheetClass = BaseJournalSheetL5r5e;
    CONFIG.Token.rulerClass = TokenRulerL5r5e;
    CONFIG.Canvas.rulerClass = RulerL5r5e;

    CONFIG.ui.compendium = CompendiumDirectoryL5r5e;

    // Define custom Roll class
    CONFIG.Dice.rolls.unshift(RollL5r5e);

    // Define DiceTerms
    CONFIG.Dice.terms[AbilityDie.DENOMINATION] = AbilityDie;
    CONFIG.Dice.terms[RingDie.DENOMINATION] = RingDie;

    // Add some classes in game
    game.l5r5e = {
        L5rBaseDie,
        RingDie,
        AbilityDie,
        HelpersL5r5e,
        ItemL5r5e,
        JournalL5r5e,
        RollL5r5e,
        ActorL5r5e,
        DicePickerDialog,
        RollnKeepDialog,
        ChiaroscuroDiceDialog,
        storage: new Storage(),
        sockets: new SocketHandlerL5r5e(),
        migrations: MigrationL5r5e,
    };

    // Register custom system settings
    RegisterSettings();

    // Register custom Handlebars Helpers
    RegisterHandlebars();

    // Preload Handlebars templates (Important : Do not await ! It's sometime break the css in clients)
    PreloadTemplates().then(() => {});

    // ***** Register custom sheets *****
    const fdc = foundry.documents.collections;
    const fav1s = foundry.appv1.sheets;

    // Actors
    fdc.Actors.unregisterSheet("core", fav1s.ActorSheet);
    fdc.Actors.registerSheet(L5R5E.namespace, CharacterSheetL5r5e, {
        types: ["character"],
        label: "TYPES.Actor.character",
        makeDefault: true,
    });
    fdc.Actors.registerSheet(L5R5E.namespace, NpcSheetL5r5e, {
        types: ["npc"],
        label: "TYPES.Actor.npc",
        makeDefault: true,
    });

    // Items
    fdc.Items.unregisterSheet("core", fav1s.ItemSheet);
    fdc.Items.registerSheet(L5R5E.namespace, ItemSheetL5r5e, {
        types: ["item"],
        label: "TYPES.Item.item",
        makeDefault: true,
    });
    fdc.Items.registerSheet(L5R5E.namespace, ArmorSheetL5r5e, {
        types: ["armor"],
        label: "TYPES.Item.armor",
        makeDefault: true,
    });
    fdc.Items.registerSheet(L5R5E.namespace, WeaponSheetL5r5e, {
        types: ["weapon"],
        label: "TYPES.Item.weapon",
        makeDefault: true,
    });
    fdc.Items.registerSheet(L5R5E.namespace, PeculiaritySheetL5r5e, {
        types: ["peculiarity"],
        label: "TYPES.Item.peculiarity",
        makeDefault: true,
    });
    fdc.Items.registerSheet(L5R5E.namespace, AdvancementSheetL5r5e, {
        types: ["advancement"],
        label: "TYPES.Item.advancement",
        makeDefault: true,
    });
    fdc.Items.registerSheet(L5R5E.namespace, ArcaneSheetL5r5e, {
        types: ["arcane"],
        label: "TYPES.Item.arcane",
        makeDefault: true,
    });
    fdc.Items.registerSheet(L5R5E.namespace, MystereSheetL5r5e, {
        types: ["mystere"],
        label: "TYPES.Item.mystere",
        makeDefault: true,
    });
    fdc.Items.registerSheet(L5R5E.namespace, TechniqueEcoleSheetL5r5e, {
        types: ["technique_ecole"],
        label: "TYPES.Item.technique_ecole",
        makeDefault: true,
    });
    fdc.Items.registerSheet(L5R5E.namespace, MotInvocationSheetL5r5e, {
        types: ["mot_invocation"],
        label: "TYPES.Item.mot_invocation",
        makeDefault: true,
    });

    // Journal
    fdc.Journal.unregisterSheet("core", fav1s.JournalSheet);
    fdc.Journal.registerSheet(L5R5E.namespace, BaseJournalSheetL5r5e, {
        label: "TYPES.Journal.journal",
        makeDefault: true,
    });

    // Override enrichHTML for Symbol replacement
    const oldEnrichHTML = foundry.applications.ux.TextEditor.implementation.prototype.constructor.enrichHTML;
    foundry.applications.ux.TextEditor.implementation.prototype.constructor.enrichHTML = async function (content, options = {}) {
        return HelpersL5r5e.convertSymbols(await oldEnrichHTML.call(this, content, options), true);
    };

    // Override the default Token _drawBar function to allow fatigue bar reversing.
    foundry.canvas.placeables.Token.prototype._drawBar = function (number, bar, data) {
        const barSettings = game.settings.get(L5R5E.namespace, "token-reverse-token-bars");
        const reverseBar = barSettings === 'both' || barSettings === data.attribute;

        // Bar value
        const pct = Math.clamp(Number(data.value), 0, data.max) / data.max;

        // Modify color
        let color = number === 0 ? [pct / 1.2, 1 - pct, 0] : [0.5 * pct, 0.7 * pct, 0.5 + pct / 2];

        // Red if compromised
        if (data.attribute === "strife" && data.value > data.max) {
            color = [1, 0.1, 0.1];
        }

        // Enlarge the bar for large tokens
        let h = Math.max(canvas.dimensions.size / 12, 8);
        if (this.height >= 2) {
            h *= 1.6;
        }

        // Draw the bar
        bar.clear()
            .beginFill(0x000000, 0.5)
            .lineStyle(2, 0x000000, 0.9)
            .drawRoundedRect(0, 0, this.w, h, 3)
            .beginFill(PIXI.utils.rgb2hex(color), 0.8)
            .lineStyle(1, 0x000000, 0.8)
            .drawRoundedRect(1, 1, (reverseBar ? 1 - pct : pct) * (this.w - 2), h - 2, 2);

        // Set position
        bar.position.set(0, number === 0 ? this.h - h : 0);
    };
});

/* ------------------------------------ */
/* Hooks Once                           */
/* ------------------------------------ */
Hooks.once("setup", HooksL5r5e.setup);
Hooks.once("ready", HooksL5r5e.ready);
Hooks.once("init", HooksL5r5e.init);
Hooks.once("diceSoNiceReady", (dice3d) => HooksL5r5e.diceSoNiceReady(dice3d));

/* ------------------------------------ */
/* Hooks On                             */
/* ------------------------------------ */
Hooks.on("renderSidebarTab", (app, html, data) => HooksL5r5e.renderSidebarTab(app, html, data));
Hooks.on("activateSettings", async (app)=> HooksL5r5e.activateSettings(app));
Hooks.on("renderChatMessageHTML", (message, html, data) => HooksL5r5e.renderChatMessage(message, html, data));
Hooks.on("renderCombatTracker", (app, html, data) => HooksL5r5e.renderCombatTracker(app, html, data));
Hooks.on("diceSoNiceRollStart", (messageId, context) => HooksL5r5e.diceSoNiceRollStart(messageId, context));
