/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1792
 *  net.minecraft.class_1802
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.settings.BoolSetting;
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.Utils;
import meteordevelopment.meteorclient.utils.misc.Keybind;
import meteordevelopment.meteorclient.utils.player.InvUtils;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1792;
import net.minecraft.class_1802;

public class WindPearlMacro
extends Module {
    private final SettingGroup sgGeneral;
    private final Setting<Keybind> activateKey;
    private final Setting<Boolean> autoRotate;
    private final Setting<Integer> windDelay;
    private int prevSlot;
    private int timer;
    private boolean active;
    private boolean prevPressed;
    private Stage stage;

    public WindPearlMacro() {
        super(Categories.Donut, "wind-pearl-macro", "Hardcoded Keybind: Pearl + Wind Charge.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.activateKey = this.sgGeneral.add(((KeybindSetting.Builder)((KeybindSetting.Builder)new KeybindSetting.Builder().name("activate-key")).defaultValue(Keybind.fromKey(71))).build());
        this.autoRotate = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("auto-rotate")).defaultValue(true)).build());
        this.windDelay = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("wind-delay")).defaultValue(1)).min(1).max(10).build());
        this.prevSlot = -1;
        this.timer = 0;
        this.active = false;
        this.prevPressed = false;
        this.stage = Stage.WAITING;
    }

    @Override
    public void onActivate() {
        this.resetMacro();
        this.prevPressed = false;
    }

    @Override
    public void onDeactivate() {
        this.releaseKeys();
    }

    @EventHandler
    private void onTick(TickEvent.Pre event) {
        if (this.mc.field_1724 != null) {
            boolean isPressed = this.activateKey.get().isPressed();
            boolean risingEdge = isPressed && !this.prevPressed;
            this.prevPressed = isPressed;
            if (risingEdge && this.mc.field_1755 == null) {
                this.startMacro();
            }
            if (this.active) {
                switch (this.stage.ordinal()) {
                    case 1: {
                        int slot = this.findItem(class_1802.field_8634);
                        if (slot != -1) {
                            this.hardwareFullSwap(slot);
                            if (this.autoRotate.get().booleanValue()) {
                                this.mc.field_1724.method_36457(-90.0f);
                            }
                            this.mc.field_1690.field_1904.method_23481(true);
                            Utils.rightClick();
                            this.stage = Stage.DELAY;
                            this.timer = 0;
                            break;
                        }
                        this.resetMacro();
                        break;
                    }
                    case 2: {
                        if (this.timer == 1) {
                            this.mc.field_1690.field_1904.method_23481(false);
                        }
                        if (this.timer >= this.windDelay.get() + 1) {
                            this.stage = Stage.THROW_WIND;
                        }
                        ++this.timer;
                        break;
                    }
                    case 3: {
                        int slot = this.findItem(class_1802.field_49098);
                        if (slot != -1) {
                            this.hardwareFullSwap(slot);
                            if (this.autoRotate.get().booleanValue()) {
                                this.mc.field_1724.method_36457(-90.0f);
                            }
                            this.mc.field_1690.field_1904.method_23481(true);
                            Utils.rightClick();
                            this.stage = Stage.CLEANUP;
                            this.timer = 0;
                            break;
                        }
                        this.resetMacro();
                        break;
                    }
                    case 4: {
                        if (this.timer >= 1) {
                            this.releaseKeys();
                            if (this.prevSlot != -1) {
                                this.hardwareFullSwap(this.prevSlot);
                            }
                            this.resetMacro();
                        }
                        ++this.timer;
                    }
                }
            }
        }
    }

    private void hardwareFullSwap(int slot) {
        if (this.mc.field_1724 != null && slot >= 0 && slot <= 8) {
            InvUtils.swap(slot, false);
            this.mc.field_1690.field_1852[slot].method_23481(true);
            this.mc.field_1690.field_1852[slot].method_23481(false);
        }
    }

    private void releaseKeys() {
        if (this.mc.field_1690 != null) {
            this.mc.field_1690.field_1904.method_23481(false);
        }
    }

    private int findItem(class_1792 item) {
        for (int i = 0; i < 9; ++i) {
            if (!this.mc.field_1724.method_31548().method_5438(i).method_31574(item)) continue;
            return i;
        }
        return -1;
    }

    private void startMacro() {
        if (!this.active) {
            this.active = true;
            this.prevSlot = this.mc.field_1724.method_31548().method_67532();
            this.stage = Stage.THROW_PEARL;
            this.timer = 0;
        }
    }

    private void resetMacro() {
        this.active = false;
        this.stage = Stage.WAITING;
        this.releaseKeys();
    }

    private static enum Stage {
        WAITING,
        THROW_PEARL,
        DELAY,
        THROW_WIND,
        CLEANUP;

    }
}

