import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { CostumeChangeTask, PlayerCompanion } from "../playerCompanion/index";
export class Achievement {
    static getAchievement(id) {
        return this.Achievements[id.name];
    }
    static initializeEvents() {
        if (!this.setupEvents) {
            mc.system.afterEvents.scriptEventReceive.subscribe(event => {
                if (event.id === utils.GlobalNamespace.Namespace + "reset" && event.sourceEntity instanceof mc.Player) {
                    const player = new utils.SafetyCheckedPlayer(event.sourceEntity);
                    const achievement = this.Achievements[event.message];
                    if (achievement) {
                        achievement.onReset(player);
                    }
                    else if (event.message.toLowerCase() === "all") {
                        Object.values(this.Achievements).forEach(achievement => achievement.onReset(player));
                    }
                    else {
                        player.sendMessage(`§cInvalid Achievement, Options Are: §e${Object.keys(this.Achievements)}`);
                    }
                }
            }, { namespaces: [utils.GlobalNamespace.NamespaceUnformatted] });
            mc.system.afterEvents.scriptEventReceive.subscribe(event => {
                if (event.id === utils.GlobalNamespace.Namespace + "complete" && event.sourceEntity instanceof mc.Player) {
                    const player = new utils.SafetyCheckedPlayer(event.sourceEntity);
                    const achievement = this.Achievements[event.message];
                    if (achievement) {
                        achievement.setStatus(player, true);
                    }
                    else if (event.message.toLowerCase() === "all") {
                        Object.values(this.Achievements).forEach(achievement => achievement.setStatus(player, true));
                    }
                    else {
                        player.sendMessage(`§cInvalid Achievement, Options Are: §e${Object.keys(this.Achievements)}`);
                    }
                }
            }, { namespaces: [utils.GlobalNamespace.NamespaceUnformatted] });
            this.setupEvents = true;
        }
    }
    constructor() {
        this.TypeId = this.constructor.name;
        this.SkinIndex = -1;
        Achievement.Achievements[this.TypeId] = this;
        utils.waitForWorldLoad().then(() => {
            Achievement.initializeEvents();
            this.onSetup();
        });
    }
    getStatus(player) {
        var _a;
        const data = (_a = utils.readDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievements")) !== null && _a !== void 0 ? _a : {};
        return Boolean(data[this.TypeId]);
    }
    setStatus(player, status) {
        var _a;
        const data = (_a = utils.readDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievements")) !== null && _a !== void 0 ? _a : {};
        if (Boolean(data[this.TypeId]) !== status) {
            data[this.TypeId] = status;
            utils.writeDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievements", data);
            utils.mclog(`Set ${this.TypeId} Status To ${status}`);
            if (status) {
                const npcs = PlayerCompanion.getNPCS(player.dimension, { type: "ldz_crcomp:craftee" }).filter(npc => npc.Owner.id === player.id);
                npcs.forEach(npc => npc.broadcastEvent("complete_achievement"));
                player.playSound("ldz_crcomp:craftee.complete_achievement.ping");
            }
        }
    }
    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` },
                    { 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` },
                    { text: `\n\n` },
                ] };
        }
    }
    getDropdownOption() {
        return {
            text: { rawtext: [{ translate: `ldz_crcomp:craftee.achievement.${this.TypeId}.title.dropdown` }] },
            onSelected: (player, params_0) => {
                params_0.startTask(new CostumeChangeTask(params_0, { costumeId: this.SkinIndex }), true);
                utils.mclog(`${this.TypeId} ${this.SkinIndex}`);
            },
            shouldHide: (player, params_0) => {
                return !this.getStatus(player);
            },
        };
    }
    onReset(player) {
        this.setStatus(player, false);
    }
}
Achievement.Achievements = {};
Achievement.setupEvents = false;
