/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.minecraft.class_1294
 *  net.minecraft.class_1297
 *  net.minecraft.class_1304
 *  net.minecraft.class_1309
 *  net.minecraft.class_1799
 *  net.minecraft.class_2596
 *  net.minecraft.class_2680
 *  net.minecraft.class_2708
 *  net.minecraft.class_2848
 *  net.minecraft.class_2848$class_2849
 *  net.minecraft.class_3481
 *  net.minecraft.class_746
 */
package meteordevelopment.meteorclient.systems.modules.movement.elytrafly.modes;

import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.events.packets.PacketEvent;
import meteordevelopment.meteorclient.systems.modules.movement.elytrafly.ElytraFlightMode;
import meteordevelopment.meteorclient.systems.modules.movement.elytrafly.ElytraFlightModes;
import meteordevelopment.meteorclient.systems.modules.player.Rotation;
import net.minecraft.class_1294;
import net.minecraft.class_1297;
import net.minecraft.class_1304;
import net.minecraft.class_1309;
import net.minecraft.class_1799;
import net.minecraft.class_2596;
import net.minecraft.class_2680;
import net.minecraft.class_2708;
import net.minecraft.class_2848;
import net.minecraft.class_3481;
import net.minecraft.class_746;

public class Bounce
extends ElytraFlightMode {
    boolean rubberbanded = false;
    int tickDelay;
    double prevFov;

    public Bounce() {
        super(ElytraFlightModes.Bounce);
        this.tickDelay = this.elytraFly.restartDelay.get();
    }

    @Override
    public void onTick() {
        super.onTick();
        if (this.mc.field_1690.field_1903.method_1434() && !this.mc.field_1724.method_6128() && !this.elytraFly.manualTakeoff.get().booleanValue()) {
            this.mc.method_1562().method_52787((class_2596)new class_2848((class_1297)this.mc.field_1724, class_2848.class_2849.field_12982));
        }
        if (Bounce.checkConditions(this.mc.field_1724)) {
            if (!this.rubberbanded) {
                if (this.prevFov != 0.0 && !this.elytraFly.sprint.get().booleanValue()) {
                    this.mc.field_1690.method_42454().method_41748((Object)0.0);
                }
                if (this.elytraFly.autoJump.get().booleanValue()) {
                    this.mc.field_1690.field_1903.method_23481(true);
                }
                this.mc.field_1690.field_1894.method_23481(true);
                this.mc.field_1724.method_36456(this.getYawDirection());
                if (this.elytraFly.lockPitch.get().booleanValue()) {
                    this.mc.field_1724.method_36457(this.elytraFly.pitch.get().floatValue());
                }
            }
            if (!this.elytraFly.sprint.get().booleanValue()) {
                if (this.mc.field_1724.method_6128()) {
                    this.mc.field_1724.method_5728(this.mc.field_1724.method_24828());
                } else {
                    this.mc.field_1724.method_5728(true);
                }
            }
            if (this.rubberbanded && this.elytraFly.restart.get().booleanValue()) {
                if (this.tickDelay > 0) {
                    --this.tickDelay;
                } else {
                    this.mc.method_1562().method_52787((class_2596)new class_2848((class_1297)this.mc.field_1724, class_2848.class_2849.field_12982));
                    this.rubberbanded = false;
                    this.tickDelay = this.elytraFly.restartDelay.get();
                }
            }
        }
    }

    @Override
    public void onPreTick() {
        super.onPreTick();
        if (Bounce.checkConditions(this.mc.field_1724) && this.elytraFly.sprint.get().booleanValue()) {
            this.mc.field_1724.method_5728(true);
        }
    }

    private void unpress() {
        this.mc.field_1690.field_1894.method_23481(false);
        if (this.elytraFly.autoJump.get().booleanValue()) {
            this.mc.field_1690.field_1903.method_23481(false);
        }
    }

    @Override
    public void onPacketReceive(PacketEvent.Receive event) {
        if (event.packet instanceof class_2708) {
            this.rubberbanded = true;
            this.mc.field_1724.method_66281();
        }
    }

    @Override
    public void onPacketSend(PacketEvent.Send event) {
        if (event.packet instanceof class_2848 && ((class_2848)event.packet).method_12365().equals((Object)class_2848.class_2849.field_12982) && !this.elytraFly.sprint.get().booleanValue()) {
            this.mc.field_1724.method_5728(true);
        }
    }

    public static boolean recastElytra(class_746 player) {
        if (Bounce.checkConditions(player) && Bounce.startGliding(player)) {
            player.field_3944.method_52787((class_2596)new class_2848((class_1297)player, class_2848.class_2849.field_12982));
            return true;
        }
        return false;
    }

    public static boolean checkConditions(class_746 player) {
        class_2680 blockState = player.method_55667();
        boolean isClimbing = blockState.method_26164(class_3481.field_22414) && !blockState.method_26164(class_3481.field_63247);
        return !player.method_31549().field_7479 && !player.method_5765() && !isClimbing && !player.method_5799() && !player.method_6059(class_1294.field_5902);
    }

    private static boolean startGliding(class_746 player) {
        for (class_1304 equipmentSlot : class_1304.field_54086) {
            if (!class_1309.method_63624((class_1799)player.method_6118(equipmentSlot), (class_1304)equipmentSlot)) continue;
            MeteorClient.mc.method_40000(() -> ((class_746)player).method_23669());
            return true;
        }
        return false;
    }

    private float getYawDirection() {
        return switch (this.elytraFly.yawLockMode.get()) {
            default -> throw new MatchException(null, null);
            case Rotation.LockMode.None -> this.mc.field_1724.method_36454();
            case Rotation.LockMode.Smart -> (float)Math.round((this.mc.field_1724.method_36454() + 1.0f) / 45.0f) * 45.0f;
            case Rotation.LockMode.Simple -> this.elytraFly.yaw.get().floatValue();
        };
    }

    @Override
    public void onActivate() {
        this.prevFov = (Double)this.mc.field_1690.method_42454().method_41753();
    }

    @Override
    public void onDeactivate() {
        this.unpress();
        this.rubberbanded = false;
        if (this.prevFov != 0.0 && !this.elytraFly.sprint.get().booleanValue()) {
            this.mc.field_1690.method_42454().method_41748((Object)this.prevFov);
        }
    }
}

