/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.minecraft.class_1268
 *  net.minecraft.class_1297
 *  net.minecraft.class_1304
 *  net.minecraft.class_1657
 *  net.minecraft.class_1799
 *  net.minecraft.class_1802
 *  net.minecraft.class_243
 *  net.minecraft.class_2596
 *  net.minecraft.class_2848
 *  net.minecraft.class_2848$class_2849
 *  net.minecraft.class_310
 */
package meteordevelopment.meteorclient.systems.modules.movement.elytrafly;

import meteordevelopment.meteorclient.events.entity.player.PlayerMoveEvent;
import meteordevelopment.meteorclient.events.packets.PacketEvent;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.systems.modules.movement.elytrafly.ElytraFlightModes;
import meteordevelopment.meteorclient.systems.modules.movement.elytrafly.ElytraFly;
import meteordevelopment.meteorclient.utils.player.FindItemResult;
import meteordevelopment.meteorclient.utils.player.InvUtils;
import meteordevelopment.meteorclient.utils.player.PlayerUtils;
import net.minecraft.class_1268;
import net.minecraft.class_1297;
import net.minecraft.class_1304;
import net.minecraft.class_1657;
import net.minecraft.class_1799;
import net.minecraft.class_1802;
import net.minecraft.class_243;
import net.minecraft.class_2596;
import net.minecraft.class_2848;
import net.minecraft.class_310;

public class ElytraFlightMode {
    protected final class_310 mc;
    protected final ElytraFly elytraFly = Modules.get().get(ElytraFly.class);
    private final ElytraFlightModes type;
    protected boolean lastJumpPressed;
    protected boolean incrementJumpTimer;
    protected boolean lastForwardPressed;
    protected int jumpTimer;
    protected double velX;
    protected double velY;
    protected double velZ;
    protected double ticksLeft;
    protected class_243 forward;
    protected class_243 right;
    protected double acceleration;

    public ElytraFlightMode(ElytraFlightModes type) {
        this.mc = class_310.method_1551();
        this.type = type;
    }

    public void onTick() {
        class_1799 chestStack;
        FindItemResult fireworks;
        if (this.elytraFly.autoReplenish.get().booleanValue() && (fireworks = InvUtils.find(class_1802.field_8639)).found() && !fireworks.isHotbar()) {
            InvUtils.move().from(fireworks.slot()).toHotbar(this.elytraFly.replenishSlot.get() - 1);
        }
        if (this.elytraFly.replace.get().booleanValue() && (chestStack = this.mc.field_1724.method_6118(class_1304.field_6174)).method_7909() == class_1802.field_8833 && chestStack.method_7936() - chestStack.method_7919() <= this.elytraFly.replaceDurability.get()) {
            FindItemResult elytra = InvUtils.find(stack -> stack.method_7936() - stack.method_7919() > this.elytraFly.replaceDurability.get() && stack.method_7909() == class_1802.field_8833);
            InvUtils.move().from(elytra.slot()).toArmor(2);
        }
    }

    public void onPreTick() {
    }

    public void onPacketSend(PacketEvent.Send event) {
    }

    public void onPacketReceive(PacketEvent.Receive event) {
    }

    public void onPlayerMove() {
    }

    public void onActivate() {
        this.lastJumpPressed = false;
        this.jumpTimer = 0;
        this.ticksLeft = 0.0;
        this.acceleration = 0.0;
    }

    public void onDeactivate() {
    }

    public void autoTakeoff() {
        if (this.incrementJumpTimer) {
            ++this.jumpTimer;
        }
        boolean jumpPressed = this.mc.field_1690.field_1903.method_1434();
        if (this.elytraFly.autoTakeOff.get().booleanValue() && this.elytraFly.flightMode.get() != ElytraFlightModes.Pitch40 && this.elytraFly.flightMode.get() != ElytraFlightModes.Bounce || !this.elytraFly.manualTakeoff.get().booleanValue() && this.elytraFly.flightMode.get() == ElytraFlightModes.Bounce && jumpPressed) {
            if (!this.lastJumpPressed && !this.mc.field_1724.method_6128()) {
                this.jumpTimer = 0;
                this.incrementJumpTimer = true;
            }
            if (this.jumpTimer >= 8) {
                this.jumpTimer = 0;
                this.incrementJumpTimer = false;
                this.mc.field_1724.method_6100(false);
                this.mc.field_1724.method_5728(true);
                this.mc.field_1724.method_6043();
                this.mc.method_1562().method_52787((class_2596)new class_2848((class_1297)this.mc.field_1724, class_2848.class_2849.field_12982));
            }
        }
        this.lastJumpPressed = jumpPressed;
    }

    public void handleAutopilot() {
        if (!this.mc.field_1724.method_6128()) {
            return;
        }
        if (this.elytraFly.autoPilot.get().booleanValue() && this.mc.field_1724.method_23318() > this.elytraFly.autoPilotMinimumHeight.get() && this.elytraFly.flightMode.get() != ElytraFlightModes.Bounce) {
            this.mc.field_1690.field_1894.method_23481(true);
            this.lastForwardPressed = true;
        }
        if (this.elytraFly.useFireworks.get().booleanValue()) {
            if (this.ticksLeft <= 0.0) {
                this.ticksLeft = this.elytraFly.autoPilotFireworkDelay.get() * 20.0;
                FindItemResult itemResult = InvUtils.findInHotbar(class_1802.field_8639);
                if (!itemResult.found()) {
                    return;
                }
                if (itemResult.isOffhand()) {
                    this.mc.field_1761.method_2919((class_1657)this.mc.field_1724, class_1268.field_5810);
                    this.mc.field_1724.method_6104(class_1268.field_5810);
                } else {
                    InvUtils.swap(itemResult.slot(), true);
                    this.mc.field_1761.method_2919((class_1657)this.mc.field_1724, class_1268.field_5808);
                    this.mc.field_1724.method_6104(class_1268.field_5808);
                    InvUtils.swapBack();
                }
            }
            this.ticksLeft -= 1.0;
        }
    }

    public void handleHorizontalSpeed(PlayerMoveEvent event) {
        boolean a = false;
        boolean b = false;
        if (this.mc.field_1690.field_1894.method_1434()) {
            this.velX += this.forward.field_1352 * this.getSpeed() * 10.0;
            this.velZ += this.forward.field_1350 * this.getSpeed() * 10.0;
            a = true;
        } else if (this.mc.field_1690.field_1881.method_1434()) {
            this.velX -= this.forward.field_1352 * this.getSpeed() * 10.0;
            this.velZ -= this.forward.field_1350 * this.getSpeed() * 10.0;
            a = true;
        }
        if (this.mc.field_1690.field_1849.method_1434()) {
            this.velX += this.right.field_1352 * this.getSpeed() * 10.0;
            this.velZ += this.right.field_1350 * this.getSpeed() * 10.0;
            b = true;
        } else if (this.mc.field_1690.field_1913.method_1434()) {
            this.velX -= this.right.field_1352 * this.getSpeed() * 10.0;
            this.velZ -= this.right.field_1350 * this.getSpeed() * 10.0;
            b = true;
        }
        if (a && b) {
            double diagonal = 1.0 / Math.sqrt(2.0);
            this.velX *= diagonal;
            this.velZ *= diagonal;
        }
    }

    public void handleVerticalSpeed(PlayerMoveEvent event) {
        if (this.mc.field_1690.field_1903.method_1434()) {
            this.velY += 0.5 * this.elytraFly.verticalSpeed.get();
        } else if (this.mc.field_1690.field_1832.method_1434()) {
            this.velY -= 0.5 * this.elytraFly.verticalSpeed.get();
        }
    }

    public void handleFallMultiplier() {
        if (this.velY < 0.0) {
            this.velY *= this.elytraFly.fallMultiplier.get().doubleValue();
        } else if (this.velY > 0.0) {
            this.velY = 0.0;
        }
    }

    public void handleAcceleration() {
        if (this.elytraFly.acceleration.get().booleanValue()) {
            if (!PlayerUtils.isMoving()) {
                this.acceleration = 0.0;
            }
            this.acceleration = Math.min(this.acceleration + this.elytraFly.accelerationMin.get() + this.elytraFly.accelerationStep.get() * 0.1, this.elytraFly.horizontalSpeed.get());
        } else {
            this.acceleration = 0.0;
        }
    }

    public void zeroAcceleration() {
        this.acceleration = 0.0;
    }

    protected double getSpeed() {
        return this.elytraFly.acceleration.get() != false ? this.acceleration : this.elytraFly.horizontalSpeed.get();
    }

    public String getHudString() {
        return this.type.name();
    }
}

