/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1268
 *  net.minecraft.class_1304
 *  net.minecraft.class_1657
 *  net.minecraft.class_1799
 *  net.minecraft.class_1802
 *  net.minecraft.class_1893
 *  net.minecraft.class_9274
 */
package meteordevelopment.meteorclient.systems.modules.combat;

import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.EnumSetting;
import meteordevelopment.meteorclient.settings.IntSetting;
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.player.FindItemResult;
import meteordevelopment.meteorclient.utils.player.InvUtils;
import meteordevelopment.meteorclient.utils.player.Rotations;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1268;
import net.minecraft.class_1304;
import net.minecraft.class_1657;
import net.minecraft.class_1799;
import net.minecraft.class_1802;
import net.minecraft.class_1893;
import net.minecraft.class_9274;

public class AutoEXP
extends Module {
    private final SettingGroup sgGeneral;
    private final Setting<Mode> mode;
    private final Setting<Boolean> replenish;
    private final Setting<Boolean> onlyGround;
    private final Setting<Integer> slot;
    private final Setting<Integer> minThreshold;
    private final Setting<Integer> maxThreshold;
    private int repairingI;

    public AutoEXP() {
        super(Categories.PvP, "auto-exp", "Automatically repairs your armor and tools in pvp.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.mode = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("mode")).description("Which items to repair.")).defaultValue(Mode.Both)).build());
        this.replenish = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("replenish")).description("Automatically replenishes exp into a selected hotbar slot.")).defaultValue(true)).build());
        this.onlyGround = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("only-on-ground")).description("Only throw when the player is on the ground.")).defaultValue(false)).build());
        this.slot = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("exp-slot")).description("The slot to replenish exp into.")).visible(this.replenish::get)).defaultValue(6)).range(1, 9).sliderRange(1, 9).build());
        this.minThreshold = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("min-threshold")).description("The minimum durability percentage that an item needs to fall to, to be repaired.")).defaultValue(30)).range(1, 100).sliderRange(1, 100).build());
        this.maxThreshold = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("max-threshold")).description("The maximum durability percentage to repair items to.")).defaultValue(80)).range(1, 100).sliderRange(1, 100).build());
    }

    @Override
    public void onActivate() {
        this.repairingI = -1;
    }

    @EventHandler
    private void onTick(TickEvent.Pre event) {
        if (this.onlyGround.get().booleanValue() && !this.mc.field_1724.method_24828()) {
            return;
        }
        if (this.repairingI == -1) {
            if (this.mode.get() != Mode.Hands) {
                for (class_1304 slot : class_9274.field_49224) {
                    class_1799 stack = this.mc.field_1724.method_6118(slot);
                    if (!this.needsRepair(stack, this.minThreshold.get().intValue())) continue;
                    this.repairingI = 36 + slot.method_5927();
                    break;
                }
            }
            if (this.mode.get() != Mode.Armor && this.repairingI == -1) {
                for (class_1268 hand : class_1268.values()) {
                    if (!this.needsRepair(this.mc.field_1724.method_5998(hand), this.minThreshold.get().intValue())) continue;
                    this.repairingI = hand == class_1268.field_5808 ? this.mc.field_1724.method_31548().method_67532() : 40;
                    break;
                }
            }
        }
        if (this.repairingI != -1) {
            if (!this.needsRepair(this.mc.field_1724.method_31548().method_5438(this.repairingI), this.maxThreshold.get().intValue())) {
                this.repairingI = -1;
                return;
            }
            FindItemResult exp = InvUtils.find(class_1802.field_8287);
            if (exp.found()) {
                if (!exp.isHotbar() && !exp.isOffhand()) {
                    if (!this.replenish.get().booleanValue()) {
                        return;
                    }
                    InvUtils.move().from(exp.slot()).toHotbar(this.slot.get() - 1);
                }
                Rotations.rotate((double)this.mc.field_1724.method_36454(), 90.0, () -> {
                    if (exp.getHand() != null) {
                        this.mc.field_1761.method_2919((class_1657)this.mc.field_1724, exp.getHand());
                    } else {
                        InvUtils.swap(exp.slot(), true);
                        this.mc.field_1761.method_2919((class_1657)this.mc.field_1724, class_1268.field_5808);
                        InvUtils.swapBack();
                    }
                });
            }
        }
    }

    private boolean needsRepair(class_1799 itemStack, double threshold) {
        if (itemStack.method_7960() || !Utils.hasEnchantments(itemStack, class_1893.field_9101)) {
            return false;
        }
        return (double)(itemStack.method_7936() - itemStack.method_7919()) / (double)itemStack.method_7936() * 100.0 <= threshold;
    }

    public static enum Mode {
        Armor,
        Hands,
        Both;

    }
}

