/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  javax.annotation.Nullable
 *  net.minecraft.class_1297
 *  net.minecraft.class_1657
 *  net.minecraft.class_1934
 */
package meteordevelopment.meteorclient.utils.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.systems.friends.Friends;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.entity.EntityUtils;
import meteordevelopment.meteorclient.utils.entity.SortPriority;
import meteordevelopment.meteorclient.utils.entity.fakeplayer.FakePlayerEntity;
import meteordevelopment.meteorclient.utils.entity.fakeplayer.FakePlayerManager;
import meteordevelopment.meteorclient.utils.player.PlayerUtils;
import net.minecraft.class_1297;
import net.minecraft.class_1657;
import net.minecraft.class_1934;

public class TargetUtils {
    private static final List<class_1297> ENTITIES = new ArrayList<class_1297>();

    private TargetUtils() {
    }

    @Nullable
    public static class_1297 get(Predicate<class_1297> isGood, SortPriority sortPriority) {
        ENTITIES.clear();
        TargetUtils.getList(ENTITIES, isGood, sortPriority, 1);
        if (!ENTITIES.isEmpty()) {
            return ENTITIES.getFirst();
        }
        return null;
    }

    public static void getList(List<class_1297> targetList, Predicate<class_1297> isGood, SortPriority sortPriority, int maxCount) {
        targetList.clear();
        for (class_1297 entity : MeteorClient.mc.field_1687.method_18112()) {
            if (entity == null || !isGood.test(entity)) continue;
            targetList.add(entity);
        }
        FakePlayerManager.forEach(fp -> {
            if (fp != null && isGood.test((class_1297)fp)) {
                targetList.add((class_1297)fp);
            }
        });
        targetList.sort(sortPriority);
        if (targetList.size() > maxCount) {
            targetList.subList(maxCount, targetList.size()).clear();
        }
    }

    @Nullable
    public static class_1657 getPlayerTarget(double range, SortPriority priority) {
        if (!Utils.canUpdate()) {
            return null;
        }
        return (class_1657)TargetUtils.get(entity -> {
            class_1657 player;
            block8: {
                block7: {
                    if (!(entity instanceof class_1657)) break block7;
                    player = (class_1657)entity;
                    if (entity != MeteorClient.mc.field_1724) break block8;
                }
                return false;
            }
            if (player.method_29504() || player.method_6032() <= 0.0f) {
                return false;
            }
            if (!PlayerUtils.isWithin(entity, range)) {
                return false;
            }
            if (!Friends.get().shouldAttack(player)) {
                return false;
            }
            if (entity instanceof FakePlayerEntity) {
                FakePlayerEntity fakePlayer = (FakePlayerEntity)((Object)entity);
                return !fakePlayer.noHit;
            }
            return EntityUtils.getGameMode(player) == class_1934.field_9215;
        }, priority);
    }

    public static boolean isBadTarget(class_1657 target, double range) {
        if (target == null) {
            return true;
        }
        return !PlayerUtils.isWithin((class_1297)target, range) || !target.method_5805() || target.method_29504() || target.method_6032() <= 0.0f;
    }
}

