import * as utils from "../../utils/index";
import * as comp from "../components/index";
import { BotBase } from "../bot";
export const MEDIC_MAX_FUEL = 20;
export const MEDIC_FUEL_ITEM = "minecraft:redstone";
export class MedicBot extends BotBase {
    get Owner() {
        if (this.owner)
            return this.owner;
        const tame = this.getComponent("tameable");
        if (tame && tame.tamedToPlayerId) {
            const owner = utils.getAllPlayersSafe().find(player => player.id === tame.tamedToPlayerId);
            this.owner = owner;
            return owner;
        }
    }
    onConstructed() {
        super.onConstructed();
        this.addActorComponent(comp.BotFuelComponent, { fuelItem: MEDIC_FUEL_ITEM, max: MEDIC_MAX_FUEL });
        this.addActorComponent(comp.AutotameComponent);
        this.healLoop();
    }
    async healLoop() {
        while (this.isValid()) {
            if (this.Owner) {
                if (this.Owner.Health.currentValue < this.Owner.Health.effectiveMax && this.Fuel > 0) {
                    try {
                        await this.moveToOwner();
                        await this.healOwner();
                    }
                    catch (error) {
                        utils.mcerror(`Failed to move to owner`, this.ActorId);
                    }
                }
                else {
                    this.triggerEvent("remove_move_to_owner");
                }
            }
            await utils.waitForTicks(20);
        }
    }
    async moveToOwner() {
        this.runCommand(`stopsound @a ldz_240927:entity.bot.hover`);
        this.removeActorComponent(comp.MoveToOwnerComponent);
        if ((this.Owner && this.Owner.Health.currentValue >= this.Owner.Health.effectiveMax) || this.Fuel <= 0) {
            this.triggerEvent("remove_move_to_owner");
            return;
        }
        return new Promise((resolve, reject) => {
            this.addActorComponent(comp.MoveToOwnerComponent, {
                startEvent: "add_move_to_owner",
                stopEvent: "",
                owner: this.Owner,
                onSuccess: () => {
                    this.runCommand(`stopsound @a ldz_240927:entity.bot.hover`);
                    this.removeActorComponent(comp.MoveToOwnerComponent);
                    resolve();
                },
                onFailure: reject,
            });
        });
    }
    async healOwner() {
        if (!this.Owner)
            return;
        while (this.isValid() && this.Owner.isValid() && this.Owner.Health.currentValue < this.Owner.Health.effectiveMax && this.Owner.location.distance(this.location) < 5) {
            if (this.Fuel > 0) {
                this.playAnimation(`animation.${utils.GlobalNamespace.NamespaceUnformatted}.generic.attack`);
                this.Owner.addEffect("minecraft:regeneration", 25, { amplifier: 2 });
                this.dimension.playSound(utils.addName("entity.medic_bot.heal"), this.location);
                this.Fuel--;
                utils.mclog(`Healing ${this.Owner.name} with ${this.Fuel} fuel left`, this.ActorId);
            }
            else {
                this.triggerEvent("remove_move_to_owner");
            }
            await utils.waitForTicks(40);
        }
        this.triggerEvent("remove_move_to_owner");
    }
    onFuelExhausted() {
        this.runCommand(`stopsound @a ldz_240927:entity.miner_bot.move`);
        this.dimension.playSound(utils.addName("entity.bot.power_down"), this.location);
    }
}
utils.Actor.makeActors({ type: "ldz_240927:medic_bot" }, MedicBot);
