import * as utils from "../utils/index";
import { DeathEffects, DropTiming, MOB_DATA, Settings } from "../shared/index";
import { DeadMob, DeathTally } from "../systems/index";
export class Guidebook extends utils.CustomItem {
    onConstructed() {
        if (!this.owner.hasTag("has_guidebook")) {
            this.owner.runCommand(`give @s ${this.TypeId}`);
            this.owner.addTag("has_guidebook");
        }
    }
    onUse(itemStack) {
        var _a;
        const player_version = (_a = utils.readDynamicProperty(this.owner, "version")) !== null && _a !== void 0 ? _a : "1.0.0";
        const menu = new utils.MenuBuilder({
            type: "page_list",
            title: utils.translate("guidebook.title"),
            contents: {
                overview: {
                    type: "page_list",
                    title: utils.translate("guidebook.overview.title"),
                    icon: "textures/ldz/250121/ui/infobulb",
                    contents: {
                        about: {
                            type: "buttons",
                            title: utils.translate("guidebook.about.title"),
                            body: utils.translate("guidebook.about.body"),
                            icon: "textures/ldz/250121/ui/infobulb",
                            contents: []
                        },
                        player_gravestones: {
                            type: "buttons",
                            title: utils.translate("guidebook.player_gravestones.title"),
                            body: utils.translate("guidebook.player_gravestones.body"),
                            icon: "textures/ldz/250121/ui/gravestone_player",
                            contents: []
                        },
                        mob_gravestones: {
                            type: "buttons",
                            title: utils.translate("guidebook.mob_gravestones.title"),
                            body: utils.translate("guidebook.mob_gravestones.body"),
                            icon: "textures/ldz/250121/ui/gravestone_mob",
                            contents: []
                        },
                        crafting_recipes: {
                            type: "buttons",
                            title: utils.translate("guidebook.crafting_recipes.title"),
                            body: utils.translate("guidebook.crafting_recipes.body"),
                            icon: "textures/ldz/250121/ui/crafting_recipes",
                            contents: []
                        },
                    }
                },
                stastistics: {
                    type: "page_list",
                    buttonLabel: utils.translate("guidebook.statistics.button"),
                    title: utils.translate("guidebook.statistics.title"),
                    icon: "textures/ldz/250121/ui/statistics",
                    contents: {
                        statistics_player: {
                            type: "buttons",
                            title: utils.translate("guidebook.statistics_player.title"),
                            body: () => {
                                var _a, _b, _c, _d;
                                const ownerKillData = DeathTally.getAllKills(this.owner.id);
                                const totalKillData = DeathTally.getAllKillsTotal();
                                const ownerKillCount = ownerKillData.count;
                                const totalKillCount = totalKillData.count;
                                const ownerMobKillCount = ownerKillData.count - ((_a = ownerKillData.kills["minecraft:player"]) !== null && _a !== void 0 ? _a : 0);
                                const totalMobKillCount = totalKillData.count - ((_b = totalKillData.kills["minecraft:player"]) !== null && _b !== void 0 ? _b : 0);
                                const ownerPlayerKillCount = (_c = ownerKillData.kills["minecraft:player"]) !== null && _c !== void 0 ? _c : 0;
                                const totalPlayerKillCount = (_d = totalKillData.kills["minecraft:player"]) !== null && _d !== void 0 ? _d : 0;
                                const ownerDeathCount = DeathTally.getDeaths(this.owner.id);
                                const totalDeathCount = DeathTally.getDeathsTotal();
                                return utils.translate("guidebook.statistics_player.body", [
                                    "\n",
                                    utils.translate("guidebook.statistics.entry", [ownerKillCount.toString(), totalKillCount.toString()]),
                                    utils.translate("guidebook.statistics.entry", [ownerMobKillCount.toString(), totalMobKillCount.toString()]),
                                    utils.translate("guidebook.statistics.entry", [ownerPlayerKillCount.toString(), totalPlayerKillCount.toString()]),
                                    utils.translate("guidebook.statistics.entry", [ownerDeathCount.toString(), totalDeathCount.toString()]),
                                ]);
                            },
                            icon: "textures/ldz/250121/ui/statistics_player",
                            contents: []
                        },
                        mob_kills_overworld: {
                            type: "buttons",
                            buttonLabel: utils.translate("guidebook.mob_kills_overworld.button"),
                            title: utils.translate("guidebook.mob_kills_overworld.title"),
                            body: () => getDimensionStatsBody(this.owner.id, "overworld"),
                            icon: "textures/ldz/250121/ui/dimension_overworld",
                            contents: []
                        },
                        mob_kills_nether: {
                            type: "buttons",
                            buttonLabel: utils.translate("guidebook.mob_kills_nether.button"),
                            title: utils.translate("guidebook.mob_kills_nether.title"),
                            body: () => getDimensionStatsBody(this.owner.id, "nether"),
                            icon: "textures/ldz/250121/ui/dimension_nether",
                            contents: []
                        },
                        mob_kills_end: {
                            type: "buttons",
                            buttonLabel: utils.translate("guidebook.mob_kills_end.button"),
                            title: utils.translate("guidebook.mob_kills_end.title"),
                            body: () => getDimensionStatsBody(this.owner.id, "end"),
                            icon: "textures/ldz/250121/ui/dimension_end",
                            contents: []
                        },
                    }
                },
                changelog: {
                    type: "page_list",
                    buttonLabel: utils.translate("guidebook.changelog.button"),
                    title: utils.translate("update.version.name", [player_version]),
                    body: utils.translate("guidebook.changelog.body"),
                    icon: "textures/ldz/250121/ui/changelog",
                    contents: {}
                },
                settings: {
                    type: "modal",
                    title: utils.translate("guidebook.settings.title"),
                    icon: "textures/ldz/250121/ui/settings",
                    contents: [
                        {
                            type: "dropdown",
                            text: utils.translate("guidebook.settings.mob_loot"),
                            defaultValueIndex: Settings.DropTiming,
                            options: [
                                {
                                    text: utils.translate("guidebook.settings.mob_loot.before"),
                                    onSelected: () => {
                                        Settings.DropTiming = DropTiming.before;
                                    }
                                },
                                {
                                    text: utils.translate("guidebook.settings.mob_loot.after"),
                                    onSelected: () => {
                                        Settings.DropTiming = DropTiming.after;
                                    }
                                }
                            ]
                        },
                        {
                            type: "dropdown",
                            text: utils.translate("guidebook.settings.effects"),
                            defaultValueIndex: Settings.DeathEffects,
                            options: [
                                {
                                    text: utils.translate("guidebook.settings.effects.particles"),
                                    onSelected: () => {
                                        Settings.DeathEffects = DeathEffects.particles;
                                        DeadMob.getActors(DeadMob).forEach(actor => actor.playDeathEffect());
                                    }
                                },
                                {
                                    text: utils.translate("guidebook.settings.effects.disappear"),
                                    onSelected: () => {
                                        Settings.DeathEffects = DeathEffects.disappear;
                                        DeadMob.getActors(DeadMob).forEach(actor => actor.playDeathEffect());
                                    }
                                },
                                {
                                    text: utils.translate("guidebook.settings.effects.none"),
                                    onSelected: () => {
                                        Settings.DeathEffects = DeathEffects.none;
                                        DeadMob.getActors(DeadMob).forEach(actor => actor.playDeathEffect());
                                    }
                                },
                            ]
                        },
                        {
                            type: "toggle",
                            text: utils.translate("guidebook.settings.player_graves"),
                            defaultValue: Settings.PlayerGravestone,
                            onSubmit(player, toggleValue) {
                                Settings.PlayerGravestone = toggleValue;
                            },
                        },
                        {
                            type: "toggle",
                            text: utils.translate("guidebook.settings.mob_graves"),
                            defaultValue: Settings.MobGravestone,
                            onSubmit(player, toggleValue) {
                                Settings.MobGravestone = toggleValue;
                            },
                        }
                    ]
                },
                credits: {
                    type: "page_list",
                    buttonLabel: utils.translate("guidebook.credits.button"),
                    title: utils.translate("update.version.name", [player_version]),
                    body: utils.translate("guidebook.credits.body"),
                    icon: "textures/ldz/250121/ui/credits",
                    contents: {}
                }
            }
        }, {
            defaultBackIcon: "textures/ldz/250121/ui/back",
            defaultSubmitText: utils.addName("menu.button.submit"),
        });
        menu.showMenuToPlayer(this.owner);
    }
}
function getDimensionStatsBody(ownerId, dimension) {
    var _a, _b, _c;
    utils.mclog(`Getting Dimension Stats for ${ownerId} in ${dimension}`, "DimensionStats");
    const ownerKillData = DeathTally.getAllKills(ownerId);
    const totalKillData = DeathTally.getAllKillsTotal();
    utils.mclog(`Recieved response from database ${JSON.stringify({ ownerKillData, totalKillData })}`, "DimensionStats");
    let entries = [];
    let totalsWithData = new Map();
    for (const mobId in totalKillData.kills) {
        if (!((_a = MOB_DATA[mobId]) === null || _a === void 0 ? void 0 : _a.dimensions.includes(dimension))) {
            utils.mclog(`Skipping ${mobId} as it is not in the ${dimension} dimension`, "DimensionStats");
            continue;
        }
        ;
        const ownerCount = (_b = ownerKillData.kills[mobId]) !== null && _b !== void 0 ? _b : 0;
        const totalCount = (_c = totalKillData.kills[mobId]) !== null && _c !== void 0 ? _c : 0;
        utils.mclog(`Adding ${mobId} to the list with ${ownerCount} / ${totalCount}`, "DimensionStats");
        totalsWithData.set(mobId, { count: ownerCount, msg: { rawtext: [
                    utils.translate("guidebook.statistics.entry", [{ translate: `entity.${mobId.replace("minecraft:", "")}.name` }, ownerCount.toString(), totalCount.toString(), "\n"]),
                ] } });
    }
    utils.mclog(`Sorting ${totalsWithData.size} entries`, "DimensionStats");
    const sortedTotals = [...totalsWithData.entries()]
        .sort((a, b) => b[1].count - a[1].count)
        .map(([_, value]) => value);
    for (const { msg } of sortedTotals) {
        entries.push(...msg.rawtext);
    }
    if (!entries.length) {
        utils.mclog(`No entries found for ${dimension}, adding empty entry`, "DimensionStats");
        entries = [utils.translate("guidebook.statistics.error")];
    }
    ;
    const result = utils.translate(`guidebook.mob_kills_${dimension}.body`, [
        "\n",
        { rawtext: entries },
    ]);
    utils.mclog(`Returning ${JSON.stringify(result)}`, "DimensionStats");
    return result;
}
utils.CustomItem.registerItemType("ldz_250121:guidebook", Guidebook);
