import * as utils from "../../utils/index";
import * as comp from "../components/index";
import { BotBase } from "../bot";
export const HOVER_MAX_FUEL = 99;
export const HOVER_FUEL_ITEM = "minecraft:redstone";
export class HoverBot extends BotBase {
    get EffectExceptions() {
        return {
            "levitation": () => true,
            "slow_falling": () => true,
            "slowness": () => true,
        };
    }
    onConstructed() {
        super.onConstructed();
        this.addActorComponent(comp.BotFuelComponent, { fuelItem: "minecraft:redstone", max: HOVER_MAX_FUEL, canInteractWithoutFuel: true });
        this.disableGroundMovement();
        const rideable = this.getComponent("rideable");
        if (rideable && rideable.getRiders().length > 0) {
            const player = new utils.SafetyCheckedPlayer(rideable.getRiders()[0]);
            this.handleFlight(player);
        }
    }
    async disableGroundMovement() {
        while (this.isValid()) {
            if (this.isOnGround) {
                this.addEffect("slowness", 25, { amplifier: 255, showParticles: false });
            }
            else {
                this.removeEffect("slowness");
            }
            await utils.waitForTicks(20);
        }
    }
    onFuelRestored() {
        const rideable = this.getComponent("rideable");
        if (this.isValid() && rideable && rideable.getRiders().length > 0) {
            const player = new utils.SafetyCheckedPlayer(rideable.getRiders()[0]);
            this.handleFlight(player);
        }
    }
    onFuelExhausted() {
        const rideable = this.getComponent("rideable");
        if (this.isValid() && rideable && rideable.getRiders().length > 0) {
            const player = new utils.SafetyCheckedPlayer(rideable.getRiders()[0]);
            player.sendMessage({ translate: "ldz_240927:hover_bot.out_of_fuel" });
            this.dimension.playSound(utils.addName("entity.bot.power_down"), this.location);
        }
    }
    onNonFuelInteract(player) {
        this.handleFlight(player);
    }
    handleFlight(player) {
        const rideable = this.getComponent("rideable");
        if (!this.isValid() || !rideable)
            return;
        rideable.addRider(player.UnsafeEntity);
        if (this.Fuel > 0) {
            const cancelCondition = () => this.Fuel <= 0 || this.isValid() && rideable.getRiders().length <= 0;
            new utils.FlightSystem({
                flyer: this,
                pilot: player,
                shiftDescend: false,
                cancelCondition,
            });
            utils.waitForLoopEvent(20, () => {
                if (this.isValid() && rideable.getRiders().length > 0) {
                    if (!this.isOnGround) {
                        this.Fuel--;
                        if (this.Fuel === 10) {
                            player.sendMessage({ translate: utils.addName("hover_bot.low_fuel"), with: ["\n"] });
                            this.dimension.playSound(utils.addName("entity.hover_bot.fuel_low_alert"), this.location);
                        }
                    }
                    return cancelCondition();
                }
                return true;
            });
        }
        else {
            player.dimension.playSound(utils.addName("entity.bot.fuel_fail"), player.location);
            player.sendMessage({ translate: "ldz_240927:hover_bot.out_of_fuel" });
        }
    }
}
utils.Actor.makeActors({ type: "ldz_240927:hover_bot" }, HoverBot);
