/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_124
 *  net.minecraft.class_1297
 *  net.minecraft.class_1304
 *  net.minecraft.class_1657
 *  net.minecraft.class_1937
 *  net.minecraft.class_2338
 *  net.minecraft.class_2350
 *  net.minecraft.class_2374
 *  net.minecraft.class_238
 *  net.minecraft.class_243
 *  net.minecraft.class_2561
 *  net.minecraft.class_2596
 *  net.minecraft.class_2661
 *  net.minecraft.class_2848
 *  net.minecraft.class_2848$class_2849
 *  net.minecraft.class_9334
 *  org.joml.Vector3d
 */
package meteordevelopment.meteorclient.systems.modules.movement;

import java.util.List;
import meteordevelopment.meteorclient.events.entity.player.PlayerMoveEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.mixin.AbstractBlockAccessor;
import meteordevelopment.meteorclient.mixin.DirectionAccessor;
import meteordevelopment.meteorclient.mixininterface.IVec3d;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.DoubleSetting;
import meteordevelopment.meteorclient.settings.EnumSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.settings.Vector3dSetting;
import meteordevelopment.meteorclient.systems.friends.Friends;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.entity.SortPriority;
import meteordevelopment.meteorclient.utils.entity.TargetUtils;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_124;
import net.minecraft.class_1297;
import net.minecraft.class_1304;
import net.minecraft.class_1657;
import net.minecraft.class_1937;
import net.minecraft.class_2338;
import net.minecraft.class_2350;
import net.minecraft.class_2374;
import net.minecraft.class_238;
import net.minecraft.class_243;
import net.minecraft.class_2561;
import net.minecraft.class_2596;
import net.minecraft.class_2661;
import net.minecraft.class_2848;
import net.minecraft.class_9334;
import org.joml.Vector3d;

public class AutoWasp
extends Module {
    private final SettingGroup sgGeneral;
    private final Setting<Double> horizontalSpeed;
    private final Setting<Double> verticalSpeed;
    private final Setting<Boolean> avoidLanding;
    private final Setting<Boolean> predictMovement;
    private final Setting<Boolean> onlyFriends;
    private final Setting<Action> action;
    private final Setting<Vector3d> offset;
    public class_1657 target;
    private int jumpTimer;
    private boolean incrementJumpTimer;

    public AutoWasp() {
        super(Categories.Movement, "auto-wasp", "Wasps for you. Unable to traverse around blocks, assumes a clear straight line to the target.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.horizontalSpeed = this.sgGeneral.add(((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("horizontal-speed")).description("Horizontal elytra speed.")).defaultValue(2.0).build());
        this.verticalSpeed = this.sgGeneral.add(((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("vertical-speed")).description("Vertical elytra speed.")).defaultValue(3.0).build());
        this.avoidLanding = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("avoid-landing")).description("Will try to avoid landing if your target is on the ground.")).defaultValue(true)).build());
        this.predictMovement = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("predict-movement")).description("Tries to predict the targets position according to their movement.")).defaultValue(true)).build());
        this.onlyFriends = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("only-friends")).description("Will only follow friends.")).defaultValue(false)).build());
        this.action = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("action-on-target-loss")).description("What to do if you lose the target.")).defaultValue(Action.TOGGLE)).build());
        this.offset = this.sgGeneral.add(((Vector3dSetting.Builder)((Vector3dSetting.Builder)new Vector3dSetting.Builder().name("offset")).description("How many blocks offset to wasp at from the target.")).defaultValue(0.0, 0.0, 0.0).build());
        this.jumpTimer = 0;
        this.incrementJumpTimer = false;
    }

    @Override
    public void onActivate() {
        if (this.target == null || this.target.method_31481()) {
            this.target = (class_1657)TargetUtils.get(entity -> {
                class_1657 player;
                block5: {
                    block4: {
                        if (!(entity instanceof class_1657)) break block4;
                        player = (class_1657)entity;
                        if (entity != this.mc.field_1724) break block5;
                    }
                    return false;
                }
                if (player.method_29504() || player.method_6032() <= 0.0f) {
                    return false;
                }
                return this.onlyFriends.get() == false || Friends.get().get(player) != null;
            }, SortPriority.LowestDistance);
            if (this.target == null) {
                this.error("No valid targets.", new Object[0]);
                this.toggle();
                return;
            }
            this.info(this.target.method_5477().getString() + " set as target.", new Object[0]);
        }
        this.jumpTimer = 0;
        this.incrementJumpTimer = false;
    }

    @Override
    public void onDeactivate() {
        this.target = null;
    }

    @EventHandler
    private void onTick(TickEvent.Pre event) {
        if (this.target.method_31481()) {
            this.warning("Lost target!", new Object[0]);
            switch (this.action.get().ordinal()) {
                case 1: {
                    this.onActivate();
                    break;
                }
                case 0: {
                    this.toggle();
                    break;
                }
                case 2: {
                    this.mc.field_1724.field_3944.method_52781(new class_2661((class_2561)class_2561.method_43470((String)"%s[%sAuto Wasp%s] Lost target.".formatted(class_124.field_1080, class_124.field_1078, class_124.field_1080))));
                }
            }
            if (!this.isActive()) {
                return;
            }
        }
        if (!this.mc.field_1724.method_6118(class_1304.field_6174).method_57826(class_9334.field_54197)) {
            return;
        }
        if (this.incrementJumpTimer) {
            ++this.jumpTimer;
        }
        if (!this.mc.field_1724.method_6128()) {
            if (!this.incrementJumpTimer) {
                this.incrementJumpTimer = true;
            }
            if (this.mc.field_1724.method_24828() && this.incrementJumpTimer) {
                this.mc.field_1724.method_6043();
                return;
            }
            if (this.jumpTimer >= 4) {
                this.jumpTimer = 0;
                this.mc.field_1724.method_6100(false);
                this.mc.field_1724.method_5728(true);
                this.mc.method_1562().method_52787((class_2596)new class_2848((class_1297)this.mc.field_1724, class_2848.class_2849.field_12982));
            }
        } else {
            this.incrementJumpTimer = false;
            this.jumpTimer = 0;
        }
    }

    @EventHandler
    private void onMove(PlayerMoveEvent event) {
        double yDist;
        if (!this.mc.field_1724.method_6118(class_1304.field_6174).method_57826(class_9334.field_54197)) {
            return;
        }
        if (!this.mc.field_1724.method_6128()) {
            return;
        }
        double xVel = 0.0;
        double yVel = 0.0;
        double zVel = 0.0;
        class_243 targetPos = this.target.method_73189().method_1031(this.offset.get().x, this.offset.get().y, this.offset.get().z);
        if (this.predictMovement.get().booleanValue()) {
            targetPos.method_1019(class_1657.method_20736((class_1297)this.target, (class_243)this.target.method_18798(), (class_238)this.target.method_5829(), (class_1937)this.mc.field_1687, (List)this.mc.field_1687.method_20743((class_1297)this.target, this.target.method_5829().method_18804(this.target.method_18798()))));
        }
        if (this.avoidLanding.get().booleanValue()) {
            double d = this.target.method_5829().method_17939() / 2.0;
            for (class_2350 dir : DirectionAccessor.meteor$getHorizontal()) {
                class_2338 pos = class_2338.method_49638((class_2374)targetPos.method_43206(dir, d).method_43206(dir.method_10170(), d)).method_10074();
                if (!((AbstractBlockAccessor)this.mc.field_1687.method_8320(pos).method_26204()).meteor$isCollidable() || !(Math.abs(targetPos.method_10214() - (double)(pos.method_10264() + 1)) <= 0.25)) continue;
                targetPos = new class_243(targetPos.field_1352, (double)pos.method_10264() + 1.25, targetPos.field_1350);
                break;
            }
        }
        double xDist = targetPos.method_10216() - this.mc.field_1724.method_23317();
        double zDist = targetPos.method_10215() - this.mc.field_1724.method_23321();
        double absX = Math.abs(xDist);
        double absZ = Math.abs(zDist);
        double diag = 0.0;
        if (absX > (double)1.0E-5f && absZ > (double)1.0E-5f) {
            diag = 1.0 / Math.sqrt(absX * absX + absZ * absZ);
        }
        if (absX > (double)1.0E-5f) {
            xVel = absX < this.horizontalSpeed.get() ? xDist : this.horizontalSpeed.get() * Math.signum(xDist);
            if (diag != 0.0) {
                xVel *= absX * diag;
            }
        }
        if (absZ > (double)1.0E-5f) {
            zVel = absZ < this.horizontalSpeed.get() ? zDist : this.horizontalSpeed.get() * Math.signum(zDist);
            if (diag != 0.0) {
                zVel *= absZ * diag;
            }
        }
        if (Math.abs(yDist = targetPos.method_10214() - this.mc.field_1724.method_23318()) > (double)1.0E-5f) {
            yVel = Math.abs(yDist) < this.verticalSpeed.get() ? yDist : this.verticalSpeed.get() * Math.signum(yDist);
        }
        ((IVec3d)event.movement).meteor$set(xVel, yVel, zVel);
    }

    public static enum Action {
        TOGGLE,
        CHOOSE_NEW_TARGET,
        DISCONNECT;


        public String toString() {
            return switch (this.ordinal()) {
                default -> throw new MatchException(null, null);
                case 0 -> "Toggle module";
                case 1 -> "Choose new target";
                case 2 -> "Disconnect";
            };
        }
    }
}

