import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { Achievement } from "./achievement";
export class CowboyAchievement extends Achievement {
    constructor() {
        super(...arguments);
        this.SkinIndex = 4;
        this.total = 15;
    }
    onSetup() {
        mc.world.afterEvents.dataDrivenEntityTrigger.subscribe(this.onTameHorse.bind(this), { eventTypes: ["minecraft:on_tame"], entityTypes: ["minecraft:horse"] });
    }
    onTameHorse(event) {
        const horse = new utils.SafetyCheckedEntity(event.entity);
        const rideable = horse.getComponent("rideable");
        if (rideable) {
            const unsafePlayer = rideable.getRiders().find(rider => rider instanceof mc.Player);
            if (unsafePlayer) {
                const player = new utils.SafetyCheckedPlayer(unsafePlayer);
                const progress = this.getProgress(player);
                progress.count++;
                utils.mclog(`Tamed ${progress.count}/${this.total} horses`);
                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.cowboy")) !== null && _a !== void 0 ? _a : { count: 0 };
    }
    setProgress(player, progress) {
        utils.writeDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievement.cowboy", 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.horse.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.horse.name" },
                            ] } },
                    { text: "\n\n" },
                ] };
        }
    }
}
new CowboyAchievement();
