/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1268
 *  net.minecraft.class_1799
 *  net.minecraft.class_1802
 *  net.minecraft.class_239
 *  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_1799;
import net.minecraft.class_1802;
import net.minecraft.class_239;
import net.minecraft.class_3965;

public class DoubleAnchor
extends Module {
    private final SettingGroup sgGeneral;
    private final Setting<Keybind> activateKey;
    private final Setting<Integer> switchDelay;
    private final Setting<Integer> totemSlot;
    private int delayCounter;
    private int step;
    private boolean isAnchoring;

    public DoubleAnchor() {
        super(Categories.PvP, "double-anchor", "Automatically places 2 respawn anchors.");
        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 starts double anchoring")).defaultValue(Keybind.fromKey(71))).build());
        this.switchDelay = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("switch-delay")).description("Delay in ticks between actions")).defaultValue(0)).range(0, 20).build());
        this.totemSlot = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("totem-slot")).description("Inventory slot for totem (1-9)")).defaultValue(1)).range(1, 9).build());
        this.delayCounter = 0;
        this.step = 0;
        this.isAnchoring = false;
    }

    @EventHandler
    private void onTick(TickEvent.Pre event) {
        if (this.mc.field_1755 != null) {
            return;
        }
        if (this.mc.field_1724 == null) {
            return;
        }
        if (!this.hasRequiredItems()) {
            return;
        }
        if (!this.isAnchoring && !this.checkActivationKey()) {
            return;
        }
        class_239 crosshairTarget = this.mc.field_1765;
        if (!(crosshairTarget instanceof class_3965)) {
            this.isAnchoring = false;
            this.resetState();
            return;
        }
        class_3965 blockHitResult = (class_3965)crosshairTarget;
        if (this.mc.field_1687.method_8320(blockHitResult.method_17777()).method_26215() || BlockUtils.isClickable(this.mc.field_1687.method_8320(blockHitResult.method_17777()).method_26204())) {
            this.isAnchoring = false;
            this.resetState();
            return;
        }
        if (this.delayCounter < this.switchDelay.get()) {
            ++this.delayCounter;
            return;
        }
        switch (this.step) {
            case 0: {
                this.swapToRespawnAnchor();
                break;
            }
            case 1: {
                BlockUtils.interact(blockHitResult, class_1268.field_5808, true);
                break;
            }
            case 2: {
                this.swapToGlowstone();
                break;
            }
            case 3: {
                BlockUtils.interact(blockHitResult, class_1268.field_5808, true);
                break;
            }
            case 4: {
                this.swapToRespawnAnchor();
                break;
            }
            case 5: {
                BlockUtils.interact(blockHitResult, class_1268.field_5808, true);
                BlockUtils.interact(blockHitResult, class_1268.field_5808, true);
                break;
            }
            case 6: {
                this.swapToGlowstone();
                break;
            }
            case 7: {
                BlockUtils.interact(blockHitResult, class_1268.field_5808, true);
                break;
            }
            case 8: {
                this.swapToTotemSlot();
                break;
            }
            case 9: {
                BlockUtils.interact(blockHitResult, class_1268.field_5808, true);
                break;
            }
            case 10: {
                this.isAnchoring = false;
                this.step = 0;
                this.resetState();
                return;
            }
        }
        ++this.step;
        this.delayCounter = 0;
    }

    private void swapToRespawnAnchor() {
        FindItemResult result = InvUtils.find(class_1802.field_23141);
        if (result.found()) {
            InvUtils.swap(result.slot(), true);
        }
    }

    private void swapToGlowstone() {
        FindItemResult result = InvUtils.find(class_1802.field_8801);
        if (result.found()) {
            InvUtils.swap(result.slot(), true);
        }
    }

    private void swapToTotemSlot() {
        InvUtils.swap(this.totemSlot.get() - 1, true);
    }

    private boolean hasRequiredItems() {
        boolean hasRespawnAnchor = false;
        boolean hasGlowstone = false;
        for (int i = 0; i < 9; ++i) {
            class_1799 itemStack = this.mc.field_1724.method_31548().method_5438(i);
            if (itemStack.method_7909() == class_1802.field_23141) {
                hasRespawnAnchor = true;
            }
            if (itemStack.method_7909() != class_1802.field_8801) continue;
            hasGlowstone = true;
        }
        return hasRespawnAnchor && hasGlowstone;
    }

    private boolean checkActivationKey() {
        if (this.activateKey.get().isPressed()) {
            this.isAnchoring = true;
            return true;
        }
        this.resetState();
        return false;
    }

    private void resetState() {
        this.delayCounter = 0;
    }

    public boolean isAnchoringActive() {
        return this.isAnchoring;
    }

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

