/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1297
 *  net.minecraft.class_1309
 *  net.minecraft.class_1792
 *  net.minecraft.class_1799
 *  net.minecraft.class_1820
 *  net.minecraft.class_2202
 *  net.minecraft.class_2211
 *  net.minecraft.class_2397
 *  net.minecraft.class_2680
 *  net.minecraft.class_3481
 *  net.minecraft.class_3489
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import java.util.function.Predicate;
import meteordevelopment.meteorclient.events.entity.player.AttackEntityEvent;
import meteordevelopment.meteorclient.events.entity.player.StartBreakingBlockEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.entity.DamageUtils;
import meteordevelopment.meteorclient.utils.player.InvUtils;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1297;
import net.minecraft.class_1309;
import net.minecraft.class_1792;
import net.minecraft.class_1799;
import net.minecraft.class_1820;
import net.minecraft.class_2202;
import net.minecraft.class_2211;
import net.minecraft.class_2397;
import net.minecraft.class_2680;
import net.minecraft.class_3481;
import net.minecraft.class_3489;

public class DonutAutoTool
extends Module {
    private boolean isToolSwapping;
    private int keybindCounter;
    private int selectedToolSlot = -1;

    public DonutAutoTool() {
        super(Categories.Donut, "Auto Tool", "Automatically switches to best tool or weapon.");
    }

    @EventHandler
    private void onTick(TickEvent.Post event) {
        if (this.mc.field_1724 == null || this.mc.field_1687 == null || this.mc.field_1761 == null) {
            return;
        }
        if (this.keybindCounter <= 0 && this.isToolSwapping && this.selectedToolSlot != -1) {
            InvUtils.swap(this.selectedToolSlot, false);
            this.isToolSwapping = false;
        } else {
            --this.keybindCounter;
        }
    }

    @EventHandler
    private void onStartBreakingBlock(StartBreakingBlockEvent event) {
        double currentScore;
        if (this.mc.field_1724 == null || this.mc.field_1687 == null || event.blockPos == null) {
            return;
        }
        class_2680 blockState = this.mc.field_1687.method_8320(event.blockPos);
        class_1799 currentStack = this.mc.field_1724.method_6047();
        double bestScore = -1.0;
        this.selectedToolSlot = -1;
        for (int i = 0; i < 9; ++i) {
            double score = DonutAutoTool.calculateToolEfficiency(this.mc.field_1724.method_31548().method_5438(i), blockState, stack -> true);
            if (!(score > bestScore)) continue;
            bestScore = score;
            this.selectedToolSlot = i;
        }
        if (this.selectedToolSlot != -1 && (bestScore > (currentScore = DonutAutoTool.calculateToolEfficiency(currentStack, blockState, stack -> true)) || !DonutAutoTool.isToolItemStack(currentStack))) {
            InvUtils.swap(this.selectedToolSlot, false);
        }
    }

    @EventHandler
    private void onAttackEntity(AttackEntityEvent event) {
        class_1309 target;
        block3: {
            block2: {
                class_1297 class_12972 = event.entity;
                if (!(class_12972 instanceof class_1309)) break block2;
                target = (class_1309)class_12972;
                if (this.mc.field_1724 != null) break block3;
            }
            return;
        }
        this.switchToBestWeapon(target);
    }

    public static double calculateToolEfficiency(class_1799 stack, class_2680 state, Predicate<class_1799> predicate) {
        boolean canUse;
        if (!predicate.test(stack) || !DonutAutoTool.isToolItemStack(stack)) {
            return -1.0;
        }
        boolean bl = canUse = stack.method_7951(state) || stack.method_31573(class_3489.field_42611) && (state.method_26204() instanceof class_2211 || state.method_26204() instanceof class_2202) || stack.method_7909() instanceof class_1820 && state.method_26204() instanceof class_2397 || state.method_26164(class_3481.field_15481);
        if (!canUse) {
            return -1.0;
        }
        return (double)stack.method_7924(state) * 1000.0;
    }

    public static boolean isToolItemStack(class_1799 itemStack) {
        return DonutAutoTool.isToolItem(itemStack.method_7909());
    }

    public static boolean isToolItem(class_1792 item) {
        class_1799 stack = item.method_7854();
        return stack.method_31573(class_3489.field_42612) || stack.method_31573(class_3489.field_42613) || stack.method_31573(class_3489.field_42614) || stack.method_31573(class_3489.field_42615) || item instanceof class_1820;
    }

    private void switchToBestWeapon(class_1309 target) {
        int bestSlot = -1;
        double bestDamage = 0.0;
        for (int i = 0; i < 9; ++i) {
            double damage;
            class_1799 stack = this.mc.field_1724.method_31548().method_5438(i);
            if (stack.method_7960() || !((damage = (double)DamageUtils.getAttackDamage((class_1309)this.mc.field_1724, (class_1297)target, stack)) > bestDamage)) continue;
            bestDamage = damage;
            bestSlot = i;
        }
        if (bestSlot != -1) {
            InvUtils.swap(bestSlot, false);
        }
    }
}

