/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1268
 *  net.minecraft.class_1297
 *  net.minecraft.class_1511
 *  net.minecraft.class_1657
 *  net.minecraft.class_1802
 *  net.minecraft.class_2246
 *  net.minecraft.class_2248
 *  net.minecraft.class_2338
 *  net.minecraft.class_238
 *  net.minecraft.class_239$class_240
 *  net.minecraft.class_3965
 */
package meteordevelopment.meteorclient.systems.modules.combat;

import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.settings.IntSetting;
import meteordevelopment.meteorclient.settings.KeybindSetting;
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.Keybind;
import meteordevelopment.meteorclient.utils.player.FindItemResult;
import meteordevelopment.meteorclient.utils.player.InvUtils;
import meteordevelopment.meteorclient.utils.world.BlockUtils;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1268;
import net.minecraft.class_1297;
import net.minecraft.class_1511;
import net.minecraft.class_1657;
import net.minecraft.class_1802;
import net.minecraft.class_2246;
import net.minecraft.class_2248;
import net.minecraft.class_2338;
import net.minecraft.class_238;
import net.minecraft.class_239;
import net.minecraft.class_3965;

public class CrystalMacro
extends Module {
    private static final double RANGE = 5.0;
    private final SettingGroup sgGeneral;
    private final Setting<Keybind> activateKey;
    private final Setting<Integer> placeDelay;
    private final Setting<Integer> breakDelay;
    public boolean isActive;
    private int placeDelayCounter;
    private int breakDelayCounter;

    public CrystalMacro() {
        super(Categories.PvP, "crystal-macro", "Automatically places and breaks end crystals.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.activateKey = this.sgGeneral.add(((KeybindSetting.Builder)((KeybindSetting.Builder)((KeybindSetting.Builder)new KeybindSetting.Builder().name("activate-key")).description("Key that activates crystal macro")).defaultValue(Keybind.fromButton(1))).build());
        this.placeDelay = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("place-delay")).description("Delay in ticks between crystal placements")).defaultValue(0)).range(0, 20).build());
        this.breakDelay = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("break-delay")).description("Delay in ticks between crystal breaks")).defaultValue(0)).range(0, 20).build());
    }

    @EventHandler
    private void onTick(TickEvent.Pre event) {
        class_1297 entity2;
        if (this.mc.field_1755 != null) {
            return;
        }
        this.updateCounters();
        if (!this.isKeyActive()) {
            return;
        }
        class_1511 nearestCrystal = null;
        double minDistance = 1000.0;
        for (class_1297 entity2 : this.mc.field_1687.method_18112()) {
            if (!(entity2 instanceof class_1511)) continue;
            class_1511 crystal = (class_1511)entity2;
            double distance = this.mc.field_1724.method_5739(entity2);
            if (!(distance <= 5.0) || !(distance < minDistance)) continue;
            minDistance = distance;
            nearestCrystal = crystal;
        }
        if (nearestCrystal != null && this.breakDelayCounter == 0) {
            this.mc.field_1761.method_2918((class_1657)this.mc.field_1724, nearestCrystal);
            this.mc.field_1724.method_6104(class_1268.field_5808);
            this.breakDelayCounter = this.breakDelay.get();
        }
        if ((entity2 = this.mc.field_1765) instanceof class_3965) {
            class_2338 pos;
            class_3965 blockHitResult = (class_3965)entity2;
            if (this.mc.field_1765.method_17783() == class_239.class_240.field_1332 && this.mc.field_1724.method_5707((pos = blockHitResult.method_17777()).method_46558()) <= 25.0 && this.isValidCrystalPlacement(pos)) {
                FindItemResult crystalResult = InvUtils.find(class_1802.field_8301);
                if (!this.mc.field_1724.method_6047().method_7909().equals(class_1802.field_8301)) {
                    if (crystalResult.found()) {
                        InvUtils.swap(crystalResult.slot(), true);
                    } else {
                        return;
                    }
                }
                if (this.placeDelayCounter == 0) {
                    BlockUtils.interact(blockHitResult, class_1268.field_5808, true);
                    this.placeDelayCounter = this.placeDelay.get();
                }
            }
        }
    }

    private void updateCounters() {
        if (this.placeDelayCounter > 0) {
            --this.placeDelayCounter;
        }
        if (this.breakDelayCounter > 0) {
            --this.breakDelayCounter;
        }
    }

    private boolean isKeyActive() {
        if (!this.activateKey.get().isPressed()) {
            this.resetCounters();
            this.isActive = false;
            return false;
        }
        this.isActive = true;
        return true;
    }

    private void resetCounters() {
        this.placeDelayCounter = 0;
        this.breakDelayCounter = 0;
    }

    private boolean isValidCrystalPlacement(class_2338 blockPos) {
        if (!this.isBlock(blockPos, class_2246.field_10540) && !this.isBlock(blockPos, class_2246.field_9987)) {
            return false;
        }
        class_2338 up = blockPos.method_10084();
        if (!this.mc.field_1687.method_22347(up)) {
            return false;
        }
        return this.mc.field_1687.method_8335(null, new class_238((double)up.method_10263(), (double)up.method_10264(), (double)up.method_10260(), (double)up.method_10263() + 1.0, (double)up.method_10264() + 2.0, (double)up.method_10260() + 1.0)).isEmpty();
    }

    private boolean isBlock(class_2338 pos, class_2248 block) {
        return this.mc.field_1687.method_8320(pos).method_26204() == block;
    }

    @Override
    public String getInfoString() {
        return this.isActive ? "active" : null;
    }
}

