/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_243
 *  net.minecraft.class_3532
 *  org.joml.Vector3d
 *  org.joml.Vector3dc
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import meteordevelopment.meteorclient.events.meteor.KeyEvent;
import meteordevelopment.meteorclient.events.world.ChunkOcclusionEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.settings.DoubleSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.misc.input.Input;
import meteordevelopment.meteorclient.utils.misc.input.KeyAction;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_243;
import net.minecraft.class_3532;
import org.joml.Vector3d;
import org.joml.Vector3dc;

public class DonutFreecam
extends Module {
    private final SettingGroup sgGeneral;
    private final Setting<Double> speed;
    public final Vector3d pos;
    public final Vector3d prevPos;
    public float yaw;
    public float pitch;
    public float prevYaw;
    public float prevPitch;
    private boolean forward;
    private boolean backward;
    private boolean left;
    private boolean right;
    private boolean up;
    private boolean down;

    public DonutFreecam() {
        super(Categories.Donut, "Freecam (Donut)", "Freecam with donut features.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.speed = this.sgGeneral.add(((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("speed")).description("Camera movement speed.")).defaultValue(1.0).min(0.1).sliderMax(5.0).build());
        this.pos = new Vector3d();
        this.prevPos = new Vector3d();
    }

    @Override
    public void onActivate() {
        if (this.mc.field_1724 == null || this.mc.field_1773 == null || this.mc.field_1690 == null) {
            return;
        }
        class_243 eyePos = this.mc.field_1773.method_19418().method_71156();
        this.pos.set(eyePos.field_1352, eyePos.field_1351, eyePos.field_1350);
        this.prevPos.set((Vector3dc)this.pos);
        this.yaw = this.mc.field_1724.method_36454();
        this.pitch = this.mc.field_1724.method_36455();
        this.prevYaw = this.yaw;
        this.prevPitch = this.pitch;
        this.mc.field_1690.field_1894.method_23481(false);
        this.mc.field_1690.field_1881.method_23481(false);
        this.mc.field_1690.field_1913.method_23481(false);
        this.mc.field_1690.field_1849.method_23481(false);
        this.mc.field_1690.field_1903.method_23481(false);
        this.mc.field_1690.field_1832.method_23481(false);
        this.mc.field_1690.field_1867.method_23481(false);
        this.mc.field_1724.method_5728(false);
        this.unpress();
        if (this.mc.field_1769 != null) {
            this.mc.field_1769.method_3279();
        }
    }

    @Override
    public void onDeactivate() {
        if (this.mc.field_1769 != null) {
            this.mc.field_1769.method_3279();
        }
    }

    private void unpress() {
        this.down = false;
        this.up = false;
        this.right = false;
        this.left = false;
        this.backward = false;
        this.forward = false;
    }

    @EventHandler
    private void onChunkOcclusion(ChunkOcclusionEvent event) {
        event.cancel();
    }

    public void changeLookDirection(double dx, double dy) {
        double s = (Double)this.mc.field_1690.method_42495().method_41753() * 0.6 + 0.2;
        double multiplier = s * s * s * 8.0;
        this.yaw += (float)(dx * multiplier * 0.15);
        this.pitch += (float)(dy * multiplier * 0.15);
        this.pitch = class_3532.method_15363((float)this.pitch, (float)-90.0f, (float)90.0f);
    }

    @EventHandler
    private void onTick(TickEvent.Pre event) {
        this.prevPos.set((Vector3dc)this.pos);
        this.prevYaw = this.yaw;
        this.prevPitch = this.pitch;
        class_243 fwd = class_243.method_1030((float)0.0f, (float)this.yaw);
        class_243 side = class_243.method_1030((float)0.0f, (float)(this.yaw + 90.0f));
        double s = this.speed.get() * (this.mc.field_1690.field_1867.method_1434() ? 2.0 : 1.0) * 0.2;
        double x = 0.0;
        double y = 0.0;
        double z = 0.0;
        if (this.forward) {
            x += fwd.field_1352 * s;
            z += fwd.field_1350 * s;
        }
        if (this.backward) {
            x -= fwd.field_1352 * s;
            z -= fwd.field_1350 * s;
        }
        if (this.right) {
            x += side.field_1352 * s;
            z += side.field_1350 * s;
        }
        if (this.left) {
            x -= side.field_1352 * s;
            z -= side.field_1350 * s;
        }
        if (this.up) {
            y += s;
        }
        if (this.down) {
            y -= s;
        }
        this.pos.add(x, y, z);
    }

    public double getX(float tickDelta) {
        return class_3532.method_16436((double)tickDelta, (double)this.prevPos.x, (double)this.pos.x);
    }

    public double getY(float tickDelta) {
        return class_3532.method_16436((double)tickDelta, (double)this.prevPos.y, (double)this.pos.y);
    }

    public double getZ(float tickDelta) {
        return class_3532.method_16436((double)tickDelta, (double)this.prevPos.z, (double)this.pos.z);
    }

    public float getYaw(float tickDelta) {
        return class_3532.method_16439((float)tickDelta, (float)this.prevYaw, (float)this.yaw);
    }

    public float getPitch(float tickDelta) {
        return class_3532.method_16439((float)tickDelta, (float)this.prevPitch, (float)this.pitch);
    }

    @EventHandler
    private void onKey(KeyEvent event) {
        boolean isPressed;
        if (this.mc.field_1755 != null || this.mc.field_1690 == null) {
            return;
        }
        boolean bl = isPressed = event.action != KeyAction.Release;
        if (Input.getKey(this.mc.field_1690.field_1894) == event.key()) {
            this.forward = isPressed;
        } else if (Input.getKey(this.mc.field_1690.field_1881) == event.key()) {
            this.backward = isPressed;
        } else if (Input.getKey(this.mc.field_1690.field_1913) == event.key()) {
            this.left = isPressed;
        } else if (Input.getKey(this.mc.field_1690.field_1849) == event.key()) {
            this.right = isPressed;
        } else if (Input.getKey(this.mc.field_1690.field_1903) == event.key()) {
            this.up = isPressed;
        } else if (Input.getKey(this.mc.field_1690.field_1832) == event.key()) {
            this.down = isPressed;
        } else {
            return;
        }
        event.cancel();
    }
}

