import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
// time before bot damage from player hits resets
const reset_seconds = 1;
export class BotHealthComponent extends utils.ActorComponent {
    constructor() {
        super(...arguments);
        this.playerHitBinding = this.onPlayerHit.bind(this);
        this.healthChangeBinding = this.onHealthChanged.bind(this);
    }
    get Hits() {
        var _a;
        return (_a = utils.readDynamicProperty(this.actor, "hits")) !== null && _a !== void 0 ? _a : 0;
    }
    set Hits(v) {
        //if (!this.actor.isValid()) return;
        utils.abortEvents('resetHits');
        if (v > 2) {
            this.actor.selfDestruct();
            return;
        }
        else {
            this.actor.raiseEvent("player_hit");
        }
        utils.writeDynamicProperty(this.actor, "hits", utils.clamp(v, 0, 2));
        utils.waitForEvent(1, this.updateSkinId.bind(this));
    }
    resetHits() {
        if (!this.actor.isValid())
            return;
        utils.waitForEvent(reset_seconds * 20, () => utils.writeDynamicProperty(this.actor, "hits", 0), 'resetHits');
        utils.waitForEvent((reset_seconds * 20) + 1, this.updateSkinId.bind(this), 'resetHits');
    }
    onComponentAdded() {
        mc.system.afterEvents.scriptEventReceive.subscribe(this.playerHitBinding);
        mc.world.afterEvents.entityHealthChanged.subscribe(this.healthChangeBinding, { entities: [this.actor.UnsafeEntity] });
    }
    onComponentRemoved() {
        mc.system.afterEvents.scriptEventReceive.unsubscribe(this.playerHitBinding);
        mc.world.afterEvents.entityHealthChanged.unsubscribe(this.healthChangeBinding);
    }
    onPlayerHit(event) {
        var _a;
        if (event.id === utils.addName("player_hit") && event.message === "victim" && ((_a = event.sourceEntity) === null || _a === void 0 ? void 0 : _a.id) === this.actor.id) {
            const subScript = (subEvent) => {
                if (subEvent.id === utils.addName("player_hit") && subEvent.message === "player" && subEvent.sourceEntity instanceof mc.Player) {
                    mc.system.afterEvents.scriptEventReceive.unsubscribe(subScript);
                    const player = new utils.SafetyCheckedPlayer(subEvent.sourceEntity);
                    this.actor.playAnimation(`animation.ldz_240927.bot.wobble`);
                    this.actor.runCommand(`playsound ldz_240927:entity.bot.hit @a ~~~`);
                    this.Hits++;
                    this.resetHits();
                }
            };
            mc.system.afterEvents.scriptEventReceive.subscribe(subScript);
            utils.waitForEvent(1, () => mc.system.afterEvents.scriptEventReceive.unsubscribe(subScript));
        }
    }
    onHealthChanged(event) {
        this.updateSkinId();
    }
    updateSkinId() {
        if (!this.actor.isValid())
            return;
        const health = this.actor.Health;
        const healthTier = Math.round(utils.normalize(health.effectiveMax, 0, health.currentValue) * 2);
        this.actor.triggerEvent(`add_skin_id_${Math.max(healthTier, this.Hits)}`);
    }
}
