import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { Achievement } from "./achievement";
export class GingerbreadAchievement extends Achievement {
    constructor() {
        super(...arguments);
        this.SkinIndex = 7;
        this.total = 100;
    }
    onSetup() {
        mc.world.afterEvents.itemCompleteUse.subscribe(this.eatCookie.bind(this));
    }
    eatCookie(event) {
        if (event.itemStack.typeId === "minecraft:cookie") {
            const player = new utils.SafetyCheckedPlayer(event.source);
            const progress = this.getProgress(player);
            progress.count++;
            utils.mclog(`Ate ${progress.count}/${this.total} cookies`);
            if (progress.count >= this.total) {
                this.setStatus(player, true);
            }
            this.setProgress(player, progress);
        }
    }
    getProgress(player) {
        var _a;
        return (_a = utils.readDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievement.gingerbread")) !== null && _a !== void 0 ? _a : { count: 0 };
    }
    setProgress(player, progress) {
        utils.writeDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievement.gingerbread", progress);
    }
    onReset(player) {
        super.onReset(player);
        this.setProgress(player, { count: 0 });
    }
    getStatusText(player) {
        if (this.getStatus(player)) {
            return { rawtext: [
                    { translate: `ldz_crcomp:craftee.achievement.${this.TypeId}.title.completed` },
                    { text: "\n" },
                    { translate: `ldz_crcomp:craftee.achievement.${this.TypeId}.status.completed`, with: { rawtext: [
                                { text: String(this.total) },
                                { translate: "item.cookie.name" },
                            ] } },
                    { text: "\n\n" },
                ] };
        }
        else {
            return { rawtext: [
                    { translate: `ldz_crcomp:craftee.achievement.${this.TypeId}.title.in_progress` },
                    { text: "\n" },
                    { translate: `ldz_crcomp:craftee.achievement.${this.TypeId}.status.in_progress`, with: { rawtext: [
                                { text: String(this.total - this.getProgress(player).count) },
                                { translate: "item.cookie.name" },
                            ] } },
                    { text: "\n\n" },
                ] };
        }
    }
}
new GingerbreadAchievement();
