/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1799
 *  net.minecraft.class_1802
 *  net.minecraft.class_1935
 *  net.minecraft.class_2561
 *  net.minecraft.class_327
 *  net.minecraft.class_332
 *  net.minecraft.class_368
 *  net.minecraft.class_368$class_369
 *  net.minecraft.class_374
 */
package meteordevelopment.meteorclient.systems.modules.donut;

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.concurrent.CompletableFuture;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.widgets.WWidget;
import meteordevelopment.meteorclient.gui.widgets.containers.WVerticalList;
import meteordevelopment.meteorclient.gui.widgets.pressable.WButton;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.EnumSetting;
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.orbit.EventHandler;
import net.minecraft.class_1799;
import net.minecraft.class_1802;
import net.minecraft.class_1935;
import net.minecraft.class_2561;
import net.minecraft.class_327;
import net.minecraft.class_332;
import net.minecraft.class_368;
import net.minecraft.class_374;

public class RainNoti
extends Module {
    private final SettingGroup sgGeneral;
    private final SettingGroup sgWebhook;
    private final Setting<Boolean> sendNotifications;
    private final Setting<Mode> notificationMode;
    private final Setting<Boolean> enableWebhook;
    private final Setting<String> webhookUrl;
    private final Setting<Boolean> selfPing;
    private final Setting<String> discordId;
    private boolean wasRaining;
    private boolean wasThundering;
    private final HttpClient httpClient;

    public RainNoti() {
        super(Categories.Donut, "rain-notify", "Notifies when it starts raining or thundering in-game.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.sgWebhook = this.settings.createGroup("Webhook");
        this.sendNotifications = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("notifications")).description("Send in-game notifications when it starts raining or thundering.")).defaultValue(true)).build());
        this.notificationMode = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("notification-mode")).description("The mode to use for notifications.")).defaultValue(Mode.Both)).visible(this.sendNotifications::get)).build());
        this.enableWebhook = this.sgWebhook.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("webhook")).description("Send webhook notifications when it starts raining or thundering")).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.wasRaining = false;
        this.wasThundering = false;
        this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10L)).build();
    }

    @EventHandler
    private void onTick(TickEvent.Post event) {
        if (this.mc.field_1687 != null) {
            boolean raining = this.mc.field_1687.method_8419();
            boolean thundering = this.mc.field_1687.method_8546();
            if (raining && !this.wasRaining && !thundering) {
                this.notifyRainStart(false);
            }
            if (thundering && !this.wasThundering) {
                this.notifyRainStart(true);
            }
            this.wasRaining = raining;
            this.wasThundering = thundering;
        }
    }

    private void notifyRainStart(boolean thunder) {
        if (this.sendNotifications.get().booleanValue()) {
            switch (this.notificationMode.get().ordinal()) {
                case 0: {
                    this.info("It has started " + (thunder ? "thundering!" : "raining!"), new Object[0]);
                    break;
                }
                case 1: {
                    this.mc.method_1566().method_1999((class_368)(thunder ? new ThunderToast(new class_1799((class_1935)class_1802.field_27051), (class_2561)class_2561.method_43470((String)"Weather Alert"), (class_2561)class_2561.method_43470((String)"Thunderstorm!")) : new RainToast(new class_1799((class_1935)class_1802.field_8705), (class_2561)class_2561.method_43470((String)"Weather Alert"), (class_2561)class_2561.method_43470((String)"Rain!"))));
                    break;
                }
                case 2: {
                    this.info("It has started " + (thunder ? "thundering!" : "raining!"), new Object[0]);
                    this.mc.method_1566().method_1999((class_368)(thunder ? new ThunderToast(new class_1799((class_1935)class_1802.field_27051), (class_2561)class_2561.method_43470((String)"Weather Alert"), (class_2561)class_2561.method_43470((String)"Thunderstorm!")) : new RainToast(new class_1799((class_1935)class_1802.field_8705), (class_2561)class_2561.method_43470((String)"Weather Alert"), (class_2561)class_2561.method_43470((String)"Rain!"))));
                }
            }
        }
        if (this.enableWebhook.get().booleanValue()) {
            this.sendWebhookNotification(thunder);
        }
    }

    private void sendWebhookNotification(boolean thunder) {
        String url = this.webhookUrl.get().trim();
        if (url.isEmpty()) {
            this.warning("Webhook URL not configured!", new Object[0]);
            return;
        }
        CompletableFuture.runAsync(() -> {
            try {
                String messageContent = "";
                if (this.selfPing.get().booleanValue() && !this.discordId.get().trim().isEmpty()) {
                    messageContent = String.format("<@%s>", this.discordId.get().trim());
                }
                String serverInfo = this.mc.method_1558() != null ? this.mc.method_1558().field_3761 : "Singleplayer";
                String jsonPayload = String.format("{\"content\":\"%s\",\"username\":\"RainNoti\",\"embeds\":[{\"title\":\"%s Started!\",\"description\":\"It has started %s in-game.\",\"color\":3447003,\"fields\":[{\"name\":\"Server\",\"value\":\"%s\",\"inline\":true},{\"name\":\"Time\",\"value\":\"<t:%d:R>\",\"inline\":true}],\"footer\":{\"text\":\"RainNoti\"}}]}", messageContent, thunder ? "\u26a1 Thunderstorm" : "\ud83c\udf27 Rain", thunder ? "thundering" : "raining", serverInfo, 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 (Exception e) {
                this.error("Failed to send webhook: " + e.getMessage(), new Object[0]);
            }
        });
    }

    @Override
    public WWidget getWidget(GuiTheme theme) {
        WVerticalList list = theme.verticalList();
        WButton reset = list.add(theme.button("Reset Weather State")).widget();
        reset.action = () -> {
            this.wasRaining = false;
            this.wasThundering = false;
            this.info("Weather state reset.", new Object[0]);
        };
        return list;
    }

    public static enum Mode {
        Chat,
        Toast,
        Both;

    }

    private static class ThunderToast
    implements class_368 {
        private final class_1799 icon;
        private final class_2561 title;
        private final class_2561 description;
        private long startTime = -1L;
        private class_368.class_369 visibility = class_368.class_369.field_2210;

        public ThunderToast(class_1799 icon, class_2561 title, class_2561 description) {
            this.icon = icon;
            this.title = title;
            this.description = description;
        }

        public class_368.class_369 method_61988() {
            return this.visibility;
        }

        public void method_61989(class_374 manager, long time) {
            if (this.startTime == -1L) {
                this.startTime = time;
            }
            if (time - this.startTime > 5000L) {
                this.visibility = class_368.class_369.field_2209;
            }
        }

        public void method_1986(class_332 context, class_327 textRenderer, long startTime) {
            context.method_25294(0, 0, 160, 32, -1442840576);
            int yellow = -10496;
            context.method_25294(0, 0, 160, 1, yellow);
            context.method_25294(0, 31, 160, 32, yellow);
            context.method_25294(0, 1, 1, 31, yellow);
            context.method_25294(159, 1, 160, 31, yellow);
            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, -10496, false);
            }
        }
    }

    private static class RainToast
    implements class_368 {
        private final class_1799 icon;
        private final class_2561 title;
        private final class_2561 description;
        private long startTime = -1L;
        private class_368.class_369 visibility = class_368.class_369.field_2210;

        public RainToast(class_1799 icon, class_2561 title, class_2561 description) {
            this.icon = icon;
            this.title = title;
            this.description = description;
        }

        public class_368.class_369 method_61988() {
            return this.visibility;
        }

        public void method_61989(class_374 manager, long time) {
            if (this.startTime == -1L) {
                this.startTime = time;
            }
            if (time - this.startTime > 5000L) {
                this.visibility = class_368.class_369.field_2209;
            }
        }

        public void method_1986(class_332 context, class_327 textRenderer, long startTime) {
            context.method_25294(0, 0, 160, 32, -1442840576);
            int blue = -12490271;
            context.method_25294(0, 0, 160, 1, blue);
            context.method_25294(0, 31, 160, 32, blue);
            context.method_25294(0, 1, 1, 31, blue);
            context.method_25294(159, 1, 160, 31, blue);
            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, -12490271, false);
            }
        }
    }
}

