/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1297
 *  net.minecraft.class_1604
 *  net.minecraft.class_1799
 *  net.minecraft.class_1802
 *  net.minecraft.class_1935
 *  net.minecraft.class_238
 *  net.minecraft.class_243
 *  net.minecraft.class_2561
 *  net.minecraft.class_327
 *  net.minecraft.class_332
 *  net.minecraft.class_3532
 *  net.minecraft.class_368
 *  net.minecraft.class_642
 */
package meteordevelopment.meteorclient.systems.modules.render;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import meteordevelopment.meteorclient.events.render.Render3DEvent;
import meteordevelopment.meteorclient.renderer.ShapeMode;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.ColorSetting;
import meteordevelopment.meteorclient.settings.EnumSetting;
import meteordevelopment.meteorclient.settings.IntSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.settings.StringSetting;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.render.MeteorToast;
import meteordevelopment.meteorclient.utils.render.RenderUtils;
import meteordevelopment.meteorclient.utils.render.color.Color;
import meteordevelopment.meteorclient.utils.render.color.SettingColor;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1297;
import net.minecraft.class_1604;
import net.minecraft.class_1799;
import net.minecraft.class_1802;
import net.minecraft.class_1935;
import net.minecraft.class_238;
import net.minecraft.class_243;
import net.minecraft.class_2561;
import net.minecraft.class_327;
import net.minecraft.class_332;
import net.minecraft.class_3532;
import net.minecraft.class_368;
import net.minecraft.class_642;

public class PillagerESP
extends Module {
    private final SettingGroup sgGeneral;
    private final SettingGroup sgESP;
    private final SettingGroup sgTracers;
    private final SettingGroup sgWebhook;
    private final Setting<Integer> maxDistance;
    private final Setting<Boolean> showCount;
    private final Setting<NotificationMode> notificationMode;
    private final Setting<Boolean> toggleOnFind;
    private final Setting<Boolean> enableDisconnect;
    private final Setting<SettingColor> espColor;
    private final Setting<ShapeMode> shapeMode;
    private final Setting<Boolean> tracersEnabled;
    private final Setting<SettingColor> tracerColor;
    private final Setting<TracersMode> tracersMode;
    private final Setting<Boolean> enableWebhook;
    private final Setting<String> webhookUrl;
    private final Setting<Boolean> selfPing;
    private final Setting<String> discordId;
    private final List<class_1604> pillagers;
    private final Set<Integer> detectedPillagers;
    private int lastPillagerCount;
    private final HttpClient httpClient;

    public PillagerESP() {
        super(Categories.Render, "pillager-esp", "ESP for pillagers with tracers, webhook notifications and info display");
        this.sgGeneral = this.settings.createGroup("General");
        this.sgESP = this.settings.createGroup("ESP");
        this.sgTracers = this.settings.createGroup("Tracers");
        this.sgWebhook = this.settings.createGroup("Webhook");
        this.maxDistance = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("max-distance")).description("Maximum distance to render pillagers")).defaultValue(128)).range(16, 256).sliderRange(16, 256).build());
        this.showCount = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("show-count")).description("Show pillager count in chat")).defaultValue(true)).build());
        this.notificationMode = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("notification-mode")).description("How to notify when pillagers are detected")).defaultValue(NotificationMode.Both)).build());
        this.toggleOnFind = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("toggle-when-found")).description("Automatically toggles the module when pillagers are detected")).defaultValue(false)).build());
        this.enableDisconnect = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("disconnect")).description("Automatically disconnect when pillagers are detected")).defaultValue(false)).build());
        this.espColor = this.sgESP.add(((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("esp-color")).description("Color of pillager ESP")).defaultValue(new SettingColor(255, 0, 0, 100)).build());
        this.shapeMode = this.sgESP.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("shape-mode")).description("How the ESP shapes are rendered")).defaultValue(ShapeMode.Both)).build());
        this.tracersEnabled = this.sgTracers.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("tracers-enabled")).description("Enable tracers to pillagers")).defaultValue(true)).build());
        this.tracerColor = this.sgTracers.add(((ColorSetting.Builder)((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("tracer-color")).description("Color of tracers")).defaultValue(new SettingColor(255, 0, 0, 255)).visible(this.tracersEnabled::get)).build());
        this.tracersMode = this.sgTracers.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("tracers-mode")).description("How tracers are rendered")).defaultValue(TracersMode.Line)).visible(this.tracersEnabled::get)).build());
        this.enableWebhook = this.sgWebhook.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("webhook")).description("Send webhook notifications when pillagers are detected")).defaultValue(false)).build());
        this.webhookUrl = this.sgWebhook.add(((StringSetting.Builder)((StringSetting.Builder)((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("webhook-url")).description("Discord webhook URL")).defaultValue("")).visible(this.enableWebhook::get)).build());
        this.selfPing = this.sgWebhook.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("self-ping")).description("Ping yourself in the webhook message")).defaultValue(false)).visible(this.enableWebhook::get)).build());
        this.discordId = this.sgWebhook.add(((StringSetting.Builder)((StringSetting.Builder)((StringSetting.Builder)((StringSetting.Builder)new StringSetting.Builder().name("discord-id")).description("Your Discord user ID for pinging")).defaultValue("")).visible(() -> this.enableWebhook.get() != false && this.selfPing.get() != false)).build());
        this.pillagers = new ArrayList<class_1604>();
        this.detectedPillagers = new HashSet<Integer>();
        this.lastPillagerCount = 0;
        this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10L)).build();
    }

    @Override
    public void onActivate() {
        this.pillagers.clear();
        this.detectedPillagers.clear();
        this.lastPillagerCount = 0;
    }

    @Override
    public void onDeactivate() {
        this.pillagers.clear();
        this.detectedPillagers.clear();
    }

    @EventHandler
    private void onRender(Render3DEvent event) {
        if (this.mc.field_1724 == null || this.mc.field_1687 == null) {
            return;
        }
        this.pillagers.clear();
        HashSet<Integer> currentPillagers = new HashSet<Integer>();
        for (class_1297 entity : this.mc.field_1687.method_18112()) {
            class_1604 pillager;
            double distance;
            if (!(entity instanceof class_1604) || !((distance = (double)this.mc.field_1724.method_5739((class_1297)(pillager = (class_1604)entity))) <= (double)this.maxDistance.get().intValue())) continue;
            this.pillagers.add(pillager);
            currentPillagers.add(entity.method_5628());
        }
        if (this.showCount.get().booleanValue() && this.pillagers.size() != this.lastPillagerCount) {
            if (!this.pillagers.isEmpty()) {
                String pillagerText = this.pillagers.size() == 1 ? "pillager" : "pillagers";
                String message = this.pillagers.size() + " " + pillagerText + " nearby";
                switch (this.notificationMode.get().ordinal()) {
                    case 0: {
                        this.info("\u00a75[\u00a7dPillagerESP\u00a75] \u00a7c" + message, new Object[0]);
                        break;
                    }
                    case 1: {
                        this.mc.method_1566().method_1999((class_368)new PillagerToast(new class_1799((class_1935)class_1802.field_8399), (class_2561)class_2561.method_43470((String)"PillagerESP"), (class_2561)class_2561.method_43470((String)(this.pillagers.size() + " pillager" + (this.pillagers.size() != 1 ? "s" : "")))));
                        break;
                    }
                    case 2: {
                        this.info("\u00a75[\u00a7dPillagerESP\u00a75] \u00a7c" + message, new Object[0]);
                        this.mc.method_1566().method_1999((class_368)new PillagerToast(new class_1799((class_1935)class_1802.field_8399), (class_2561)class_2561.method_43470((String)"PillagerESP"), (class_2561)class_2561.method_43470((String)(this.pillagers.size() + " pillager" + (this.pillagers.size() != 1 ? "s" : "")))));
                    }
                }
            }
            this.lastPillagerCount = this.pillagers.size();
        }
        if (!currentPillagers.isEmpty() && !currentPillagers.equals(this.detectedPillagers)) {
            HashSet newPillagers = new HashSet(currentPillagers);
            newPillagers.removeAll(this.detectedPillagers);
            if (!newPillagers.isEmpty()) {
                this.detectedPillagers.addAll(newPillagers);
                this.handlePillagerDetection(this.pillagers.size());
            }
        } else if (currentPillagers.isEmpty()) {
            this.detectedPillagers.clear();
        }
        for (class_1604 pillager : this.pillagers) {
            if (pillager == null || !pillager.method_5805()) continue;
            double x = class_3532.method_16436((double)event.tickDelta, (double)pillager.field_6014, (double)pillager.method_23317());
            double y = class_3532.method_16436((double)event.tickDelta, (double)pillager.field_6036, (double)pillager.method_23318());
            double z = class_3532.method_16436((double)event.tickDelta, (double)pillager.field_5969, (double)pillager.method_23321());
            class_243 pos = new class_243(x, y, z);
            class_238 box = new class_238(x - (double)(pillager.method_17681() / 2.0f), y, z - (double)(pillager.method_17681() / 2.0f), x + (double)(pillager.method_17681() / 2.0f), y + (double)pillager.method_17682(), z + (double)(pillager.method_17681() / 2.0f));
            this.renderESP(box, event);
            if (!this.tracersEnabled.get().booleanValue()) continue;
            this.renderTracers(pillager, pos, event);
        }
    }

    private void handlePillagerDetection(int pillagerCount) {
        if (this.enableWebhook.get().booleanValue()) {
            this.sendWebhookNotification(pillagerCount);
        }
        if (this.toggleOnFind.get().booleanValue()) {
            this.toggle();
        }
        if (this.enableDisconnect.get().booleanValue()) {
            String message = pillagerCount == 1 ? "Pillager detected!" : String.format("%d pillagers detected!", pillagerCount);
            this.disconnectFromServer(message);
        }
    }

    private void sendWebhookNotification(int pillagerCount) {
        String url = this.webhookUrl.get().trim();
        if (url.isEmpty()) {
            this.warning("Webhook URL not configured!", new Object[0]);
            return;
        }
        CompletableFuture.runAsync(() -> {
            try {
                class_642 server = this.mc.method_1558();
                String serverInfo = server != null ? server.field_3761 : "Unknown Server";
                String messageContent = "";
                if (this.selfPing.get().booleanValue() && !this.discordId.get().trim().isEmpty()) {
                    messageContent = String.format("<@%s>", this.discordId.get().trim());
                }
                String pillagerText = pillagerCount == 1 ? "pillager" : "pillagers";
                String description = String.format("%d %s detected", pillagerCount, pillagerText);
                String coordinates = "Unknown";
                if (this.mc.field_1724 != null) {
                    coordinates = String.format("X: %.0f, Y: %.0f, Z: %.0f", this.mc.field_1724.method_23317(), this.mc.field_1724.method_23318(), this.mc.field_1724.method_23321());
                }
                String jsonPayload = String.format("{\"content\":\"%s\",\"username\":\"PillagerESP\",\"avatar_url\":\"https://minecraft.wiki/images/4/4b/Pillager_JE2_BE2.png\",\"embeds\":[{\"title\":\"\u2694\ufe0f Pillager Alert\",\"description\":\"%s\",\"color\":16711680,\"thumbnail\":{\"url\":\"https://minecraft.wiki/images/4/4b/Pillager_JE2_BE2.png\"},\"fields\":[{\"name\":\"Count\",\"value\":\"%d\",\"inline\":true},{\"name\":\"Server\",\"value\":\"%s\",\"inline\":true},{\"name\":\"Coordinates\",\"value\":\"%s\",\"inline\":false},{\"name\":\"Time\",\"value\":\"<t:%d:R>\",\"inline\":true}],\"footer\":{\"text\":\"Sent by Divine\"}}]}", messageContent.replace("\"", "\\\""), description.replace("\"", "\\\""), pillagerCount, serverInfo.replace("\"", "\\\""), coordinates.replace("\"", "\\\""), System.currentTimeMillis() / 1000L);
                HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(jsonPayload)).timeout(Duration.ofSeconds(30L)).build();
                HttpResponse<String> response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
                if (response.statusCode() == 204) {
                    this.info("Webhook notification sent successfully", new Object[0]);
                } else {
                    this.error("Webhook failed with status: " + response.statusCode(), new Object[0]);
                }
            }
            catch (IOException | InterruptedException e) {
                this.error("Failed to send webhook: " + e.getMessage(), new Object[0]);
            }
        });
    }

    private void disconnectFromServer(String reason) {
        if (this.mc.field_1687 != null && this.mc.method_1562() != null) {
            this.mc.method_1562().method_48296().method_10747((class_2561)class_2561.method_43470((String)reason));
            this.info("Disconnected from server - " + reason, new Object[0]);
        }
    }

    private void renderESP(class_238 box, Render3DEvent event) {
        Color color = this.espColor.get();
        event.renderer.box(box, color, color, this.shapeMode.get(), 0);
    }

    private void renderTracers(class_1604 pillager, class_243 pos, Render3DEvent event) {
        if (this.mc.field_1724 == null) {
            return;
        }
        Color color = this.tracerColor.get();
        class_243 pillagerCenter = pos.method_1031(0.0, (double)pillager.method_17682() / 2.0, 0.0);
        switch (this.tracersMode.get().ordinal()) {
            case 0: {
                event.renderer.line(RenderUtils.center.field_1352, RenderUtils.center.field_1351, RenderUtils.center.field_1350, pillagerCenter.field_1352, pillagerCenter.field_1351, pillagerCenter.field_1350, color);
                break;
            }
            case 1: {
                class_238 dotBox = new class_238(pillagerCenter.field_1352 - 0.1, pillagerCenter.field_1351 - 0.1, pillagerCenter.field_1350 - 0.1, pillagerCenter.field_1352 + 0.1, pillagerCenter.field_1351 + 0.1, pillagerCenter.field_1350 + 0.1);
                event.renderer.box(dotBox, color, color, ShapeMode.Both, 0);
                break;
            }
            case 2: {
                event.renderer.line(RenderUtils.center.field_1352, RenderUtils.center.field_1351, RenderUtils.center.field_1350, pillagerCenter.field_1352, pillagerCenter.field_1351, pillagerCenter.field_1350, color);
                class_238 dotBox = new class_238(pillagerCenter.field_1352 - 0.1, pillagerCenter.field_1351 - 0.1, pillagerCenter.field_1350 - 0.1, pillagerCenter.field_1352 + 0.1, pillagerCenter.field_1351 + 0.1, pillagerCenter.field_1350 + 0.1);
                event.renderer.box(dotBox, color, color, ShapeMode.Both, 0);
            }
        }
    }

    @Override
    public String getInfoString() {
        return String.valueOf(this.pillagers.size());
    }

    public static enum NotificationMode {
        Chat,
        Toast,
        Both;

    }

    public static enum TracersMode {
        Line,
        Dot,
        Both;

    }

    private static class PillagerToast
    extends MeteorToast {
        private final class_1799 icon;
        private final class_2561 title;
        private final class_2561 description;

        public PillagerToast(class_1799 icon, class_2561 title, class_2561 description) {
            super(new MeteorToast.Builder(title.getString()).icon(icon.method_7909()).text(description.getString()));
            this.icon = icon;
            this.title = title;
            this.description = description;
        }

        @Override
        public void method_1986(class_332 context, class_327 textRenderer, long startTime) {
            context.method_25294(0, 0, 160, 32, -1442840576);
            int red = -65536;
            context.method_25294(0, 0, 160, 1, red);
            context.method_25294(0, 31, 160, 32, red);
            context.method_25294(0, 1, 1, 31, red);
            context.method_25294(159, 1, 160, 31, red);
            if (this.icon != null && !this.icon.method_7960()) {
                context.method_51427(this.icon, 8, 8);
            }
            if (this.title != null) {
                context.method_51439(textRenderer, this.title, 32, 7, 0xFFFFFF, false);
            }
            if (this.description != null) {
                context.method_51439(textRenderer, this.description, 32, 18, 0xFF0000, false);
            }
        }
    }
}

