import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { Achievement } from "./achievement";
export class MechAchievement extends Achievement {
    constructor() {
        super(...arguments);
        this.SkinIndex = 10;
        this.total = 10;
    }
    onSetup() {
        mc.world.afterEvents.dataDrivenEntityTrigger.subscribe(this.buildGolem.bind(this), { entityTypes: ["minecraft:iron_golem"], eventTypes: ["minecraft:from_player"] });
    }
    buildGolem() {
        const callback = (event) => {
            if (!event.itemStack.typeId.includes("pumpkin"))
                return;
            const player = new utils.SafetyCheckedPlayer(event.source);
            const progress = this.getProgress(player);
            progress.count++;
            utils.mclog(`Built ${progress.count}/${this.total} Iron Golems`);
            if (progress.count >= this.total) {
                this.setStatus(player, true);
            }
            this.setProgress(player, progress);
        };
        mc.world.afterEvents.itemUseOn.subscribe(callback);
        utils.waitForEvent(1, () => mc.world.afterEvents.itemUseOn.unsubscribe(callback));
    }
    getProgress(player) {
        var _a;
        return (_a = utils.readDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievement.mech")) !== null && _a !== void 0 ? _a : { count: 0 };
    }
    setProgress(player, progress) {
        utils.writeDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievement.mech", 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: "entity.iron_golem.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: "entity.iron_golem.name" },
                            ] } },
                    { text: "\n\n" },
                ] };
        }
    }
}
new MechAchievement();
