import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
import * as comp from "../components/index";
import { BotBase } from "../bot";
const SCUFFLE_HEAL_AMOUNT = 10;
export class ScuffleBot extends BotBase {
    constructor() {
        super(...arguments);
        this.mode = this.getProperty("mode");
        this.aborting = false;
        this.dev = false;
    }
    get Tame() {
        return this.getActorComponent(comp.AutotameComponent);
    }
    get Mode() {
        return this.mode;
    }
    onConstructed() {
        super.onConstructed();
        this.addActorComponent(comp.AutotameComponent);
        //this.addActorComponent(comp.BotFuelComponent, {fuelItem: SCUFFLE_FUEL_ITEM, max: SCUFFLE_MAX_FUEL});
        this.addActorComponent(comp.AttackComponent, {
            type: "melee",
            cooldown: 1,
            damage: 5,
            duration: { dmgTime: 0.5, endTime: 1.0 },
            onHit: this.onAttack.bind(this),
        });
    }
    onAttack(victim) {
        utils.getEntitiesSafe(this.dimension, { location: this.location, maxDistance: 4, excludeTypes: ["minecraft:player"], excludeFamilies: ["ldz_240927:utility_bot"] }).forEach(entity => {
            function sameOwner(self, victim) {
                var _a;
                const tame = victim.getComponent("tameable");
                if (tame && tame.tamedToPlayerId === ((_a = self.Tame) === null || _a === void 0 ? void 0 : _a.OwnerID)) {
                    return true;
                }
                return false;
            }
            if (victim.id !== entity.id && entity.id !== this.id && !sameOwner(this, entity)) {
                try {
                    entity.applyDamage(5, { cause: mc.EntityDamageCause.entityAttack, damagingEntity: this });
                }
                catch (error) { }
            }
        });
    }
    set Mode(v) {
        if (this.Mode === v)
            return;
        this.mode = v;
        this.aborting = true;
        this.setProperty("mode", v);
        utils.abortEvents(this.id + "abort");
        this.removeActorComponent(comp.NavigateComponent);
        if (v === "follow") {
            this.triggerEvent("add_follow_owner");
            this.triggerEvent("remove_free_roam");
        }
        else if (v === "roam") {
            this.triggerEvent("add_free_roam");
            this.triggerEvent("remove_follow_owner");
        }
        else {
            this.triggerEvent("remove_follow_owner");
            this.triggerEvent("remove_free_roam");
        }
    }
    get Owner() {
        if (!this.isValid())
            return;
        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;
        }
    }
    onInteract(player) {
        var _a, _b, _c;
        if (player.id !== ((_a = this.Owner) === null || _a === void 0 ? void 0 : _a.id))
            return;
        if ((this.Health && this.Health.currentValue < this.Health.effectiveMax) && ((_b = player.MainHand) === null || _b === void 0 ? void 0 : _b.typeId) === "minecraft:redstone") {
            (_c = this.Health) === null || _c === void 0 ? void 0 : _c.setCurrentValue(this.Health.currentValue + SCUFFLE_HEAL_AMOUNT);
            this.runCommand('playsound ldz_240927:entity.bot.add_redstone_fuel @a ~ ~ ~');
            if (player.MainHand.amount > 1) {
                const item = player.MainHand;
                item.amount--;
                player.MainHand = item;
            }
            else {
                player.MainHand = undefined;
            }
            return;
        }
        player.playSound(utils.addName("menu.bot.open"));
        new utils.MenuBuilder({
            type: "buttons",
            title: { translate: utils.addName("scuffle_bot.menu.title") },
            contents: [
                {
                    text: { translate: utils.addName("scuffle_bot.menu.stay") },
                    texture: "textures/ldz/240927/ui/torch/stay",
                    shouldHide: () => this.Mode === "stay",
                    onClick: () => {
                        this.Mode = "stay",
                            player.playSound(utils.addName("menu.bot.select_stay"));
                    },
                },
                {
                    text: { translate: utils.addName("scuffle_bot.menu.roam") },
                    texture: "textures/ldz/240927/ui/torch/roam",
                    shouldHide: () => this.Mode === "roam",
                    onClick: () => {
                        this.Mode = "roam",
                            player.playSound(utils.addName("menu.bot.select_roam"));
                    },
                },
                {
                    text: { translate: utils.addName("torch_bot.menu.follow") },
                    texture: "textures/ldz/240927/ui/torch/follow",
                    shouldHide: () => this.Mode === "follow",
                    onClick: () => {
                        this.Mode = "follow",
                            player.playSound(utils.addName("menu.bot.select_follow"));
                    },
                },
                {
                    text: "Toggle Dev Mode",
                    shouldHide: () => { var _a; return !((_a = this.Owner) === null || _a === void 0 ? void 0 : _a.hasTag("dev")); },
                    onClick: () => {
                        this.dev = !this.dev;
                        utils.mclog(`Set ${this.ActorId}'s Dev Mode To ${this.dev}`);
                    },
                },
            ]
        }).showMenuToPlayer(player);
    }
}
utils.Actor.makeActors({ type: "ldz_240927:scuffle_bot" }, ScuffleBot);
