import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
export class BotFuelComponent extends utils.ActorComponent {
    constructor() {
        super(...arguments);
        this.fuel = 0;
        this.max = this.options.max;
    }
    initialFuel(amount) {
        this.actor.runCommand(`stopsound @a ldz_240927:entity.bot.power_down`);
        if (amount === -1)
            amount = this.options.max;
        this.fuel = amount;
        amount = utils.clamp(amount, 0, this.options.max);
        this.actor.setProperty(utils.addName("fuel"), amount);
    }
    get Fuel() {
        return this.fuel;
    }
    set Fuel(v) {
        try {
            v = utils.clamp(v, 0, this.options.max);
            const oldFuel = this.Fuel;
            this.actor.setProperty(utils.addName("fuel"), v);
            this.fuel = v;
            if (oldFuel > 0 && v <= 0) {
                this.actor.onFuelExhausted();
            }
            else if (oldFuel <= 0 && v > 0) {
                this.actor.onFuelRestored();
            }
        }
        catch (error) { }
    }
    onComponentAdded() {
        var _a;
        this.actor.addTag('fuelType ' + this.options.fuelItem);
        this.fuel = (_a = this.actor.getProperty(utils.addName("fuel"))) !== null && _a !== void 0 ? _a : 0;
        utils.waitForEvent(1, () => {
            if (this.Fuel <= 0) {
                this.actor.onFuelExhausted();
            }
            else {
                this.actor.onFuelRestored();
            }
        });
    }
    fuelInteraction(player) {
        var _a;
        if (((_a = player.MainHand) === null || _a === void 0 ? void 0 : _a.typeId) === this.options.fuelItem) {
            const amount = player.MainHand.amount;
            if (player.matches({ excludeGameModes: [mc.GameMode.creative] })) {
                const remainder = (amount + this.Fuel) - this.options.max;
                if (remainder > 0) {
                    const item = player.MainHand.clone();
                    item.amount = remainder;
                    player.MainHand = item;
                }
                else {
                    player.MainHand = undefined;
                }
            }
            if (this.Fuel < this.options.max) {
                switch (this.options.fuelItem) {
                    case "minecraft:arrow":
                        this.actor.runCommand(`playsound ldz_240927:entity.guard_bot.load_quiver @a ~~~`);
                        break;
                    case "minecraft:bone_meal":
                        this.actor.runCommand(`playsound ldz_240927:entity.bot.add_bone_meal_fuel @a ~~~`);
                        break;
                    case "minecraft:bone_meal":
                        this.actor.runCommand(`playsound ldz_240927:entity.bot.add_bone_meal_fuel @a ~~~`);
                        break;
                    case "minecraft:chorus_fruit":
                        this.actor.runCommand(`playsound ldz_240927:entity.bot.add_chorus_fruit_fuel @a ~~~`);
                        break;
                    case "minecraft:redstone":
                        this.actor.runCommand(`playsound ldz_240927:entity.bot.add_redstone_fuel @a ~~~`);
                        break;
                    default:
                        break;
                }
            }
            this.Fuel += amount;
            return;
        }
        else if (this.Fuel > 0 || this.options.canInteractWithoutFuel) {
            this.actor.onNonFuelInteract(player);
        }
        else {
            player.dimension.playSound(utils.addName("entity.bot.fuel_fail"), player.location);
            switch (this.actor.ActorId) {
                case "MedicBot":
                    player.sendMessage({ translate: "ldz_240927:medic_bot.out_of_fuel" });
                    break;
                case "DrillBot":
                    player.sendMessage({ translate: "ldz_240927:drill_bot.out_of_fuel" });
                    break;
                case "GuardBot":
                    player.sendMessage({ translate: "ldz_240927:guard_bot.out_of_fuel" });
                    break;
                case "HoverBot":
                    player.sendMessage({ translate: "ldz_240927:hover_bot.out_of_fuel" });
                    break;
                case "MinerBot":
                    player.sendMessage({ translate: "ldz_240927:miner_bot.out_of_fuel" });
                    break;
                case "SprinklerBot":
                    player.sendMessage({ translate: "ldz_240927:sprinkler_bot.out_of_fuel" });
                    break;
                case "portalBot":
                    player.sendMessage({ translate: "ldz_240927:portal_bot.out_of_fuel" });
                    break;
                case "TorchBot":
                    player.sendMessage({ translate: "ldz_240927:torch_bot.out_of_fuel" });
                    break;
                default:
                    player.sendMessage({ translate: "ldz_240927:generic.out_of_fuel" });
                    break;
            }
        }
    }
    dropFuel() {
        while (this.Fuel > 0) {
            const item = new mc.ItemStack(this.options.fuelItem);
            item.amount = Math.min(this.Fuel, item.maxAmount);
            this.actor.dimension.spawnItem(item, this.actor.location);
            this.Fuel -= item.amount;
        }
    }
}
