/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1922
 *  net.minecraft.class_2338
 *  net.minecraft.class_238
 *  net.minecraft.class_243
 *  net.minecraft.class_2586
 *  net.minecraft.class_2595
 *  net.minecraft.class_2601
 *  net.minecraft.class_2609
 *  net.minecraft.class_2611
 *  net.minecraft.class_2614
 *  net.minecraft.class_2627
 *  net.minecraft.class_2636
 *  net.minecraft.class_265
 *  net.minecraft.class_2680
 *  net.minecraft.class_2818
 *  net.minecraft.class_3719
 */
package meteordevelopment.meteorclient.systems.modules.render;

import java.util.ArrayList;
import java.util.List;
import meteordevelopment.meteorclient.events.render.Render3DEvent;
import meteordevelopment.meteorclient.renderer.ShapeMode;
import meteordevelopment.meteorclient.settings.BoolSetting;
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.render.color.Color;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1922;
import net.minecraft.class_2338;
import net.minecraft.class_238;
import net.minecraft.class_243;
import net.minecraft.class_2586;
import net.minecraft.class_2595;
import net.minecraft.class_2601;
import net.minecraft.class_2609;
import net.minecraft.class_2611;
import net.minecraft.class_2614;
import net.minecraft.class_2627;
import net.minecraft.class_2636;
import net.minecraft.class_265;
import net.minecraft.class_2680;
import net.minecraft.class_2818;
import net.minecraft.class_3719;

public class StorageESPV2
extends Module {
    private final SettingGroup sgGeneral;
    private final SettingGroup sgToggles;
    private final Setting<Integer> alpha;
    private final Setting<Boolean> tracers;
    private final Setting<Boolean> fill;
    private final Setting<Boolean> outline;
    private final Setting<Boolean> chests;
    private final Setting<Boolean> enderChests;
    private final Setting<Boolean> spawners;
    private final Setting<Boolean> shulkerBoxes;
    private final Setting<Boolean> furnaces;
    private final Setting<Boolean> barrels;
    private final Setting<Boolean> dispensers;
    private final Setting<Boolean> hoppers;
    private static final Color CHEST_COLOR = new Color(156, 91, 0, 255);
    private static final Color TRAPPED_COLOR = new Color(200, 91, 0, 255);
    private static final Color ENDER_COLOR = new Color(117, 0, 255, 255);
    private static final Color SPAWNER_COLOR = new Color(138, 126, 166, 255);
    private static final Color SHULKER_COLOR = new Color(134, 0, 158, 255);
    private static final Color FURNACE_COLOR = new Color(125, 125, 125, 255);
    private static final Color BARREL_COLOR = new Color(255, 140, 140, 255);
    private static final Color DISPENSER_COLOR = new Color(100, 100, 100, 255);
    private static final Color HOPPER_COLOR = new Color(100, 200, 255, 255);

    public StorageESPV2() {
        super(Categories.Render, "storage-esp-v2", "Renders storage blocks through walls with detailed shapes.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.sgToggles = this.settings.createGroup("Storage Types");
        this.alpha = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("alpha")).description("The alpha of the boxes.")).defaultValue(125)).min(0).max(255).build());
        this.tracers = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("tracers")).description("Draws a line from your player to the storage block.")).defaultValue(false)).build());
        this.fill = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("fill")).description("Fills the boxes.")).defaultValue(true)).build());
        this.outline = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("outline")).description("Outlines the boxes.")).defaultValue(true)).build());
        this.chests = this.sgToggles.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("chests")).defaultValue(true)).build());
        this.enderChests = this.sgToggles.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("ender-chests")).defaultValue(true)).build());
        this.spawners = this.sgToggles.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("spawners")).defaultValue(true)).build());
        this.shulkerBoxes = this.sgToggles.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("shulker-boxes")).defaultValue(true)).build());
        this.furnaces = this.sgToggles.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("furnaces")).defaultValue(true)).build());
        this.barrels = this.sgToggles.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("barrels")).defaultValue(true)).build());
        this.dispensers = this.sgToggles.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("dispensers")).defaultValue(false)).build());
        this.hoppers = this.sgToggles.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("hoppers")).defaultValue(false)).build());
    }

    @EventHandler
    private void onRender(Render3DEvent event) {
        if (this.mc.field_1687 == null || this.mc.field_1724 == null || this.mc.field_1773 == null || this.mc.field_1773.method_19418() == null) {
            return;
        }
        class_243 camPos = this.mc.field_1724.method_33571();
        class_243 lookVec = this.mc.field_1724.method_5828(1.0f);
        class_243 centerScreenPos = camPos.method_1019(lookVec.method_1021(10.0));
        int viewDist = (Integer)this.mc.field_1690.method_42503().method_41753();
        int pChunkX = this.mc.field_1724.method_31477() >> 4;
        int pChunkZ = this.mc.field_1724.method_31479() >> 4;
        for (int cx = pChunkX - viewDist; cx <= pChunkX + viewDist; ++cx) {
            for (int cz = pChunkZ - viewDist; cz <= pChunkZ + viewDist; ++cz) {
                class_2818 chunk = this.mc.field_1687.method_2935().method_21730(cx, cz);
                if (chunk == null) continue;
                ArrayList blockEntities = new ArrayList(chunk.method_12214().values());
                for (class_2586 be : blockEntities) {
                    String name;
                    if (!this.isStorage(be) || !this.shouldHighlight(name = be.method_11010().method_26204().method_63499())) continue;
                    class_2338 pos = be.method_11016();
                    class_2680 state = be.method_11010();
                    class_265 shape = state.method_26218((class_1922)this.mc.field_1687, pos);
                    if (shape == null) continue;
                    List boxes = shape.method_1090();
                    if (boxes.isEmpty()) {
                        class_238 fallback = new class_238(pos);
                        this.renderBox(event, fallback, this.getFillColor(name), this.getOutlineColor(name));
                    } else {
                        for (class_238 part : boxes) {
                            class_238 partWorld = part.method_1014(0.002).method_996(pos);
                            this.renderBox(event, partWorld, this.getFillColor(name), this.getOutlineColor(name));
                        }
                    }
                    if (!this.tracers.get().booleanValue()) continue;
                    class_243 blockCenter = new class_243((double)pos.method_10263() + 0.5, (double)pos.method_10264() + 0.5, (double)pos.method_10260() + 0.5);
                    Color tracerColor = this.getOutlineColor(name);
                    event.renderer.line(centerScreenPos.field_1352, centerScreenPos.field_1351, centerScreenPos.field_1350, blockCenter.field_1352, blockCenter.field_1351, blockCenter.field_1350, tracerColor);
                }
            }
        }
    }

    private boolean isStorage(class_2586 be) {
        return be instanceof class_2595 || be instanceof class_2611 || be instanceof class_2627 || be instanceof class_3719 || be instanceof class_2601 || be instanceof class_2614 || be instanceof class_2636 || be instanceof class_2609;
    }

    private boolean shouldHighlight(String name) {
        if (name.contains("chest") && !this.chests.get().booleanValue()) {
            return false;
        }
        if (name.contains("ender_chest") && !this.enderChests.get().booleanValue()) {
            return false;
        }
        if (name.contains("spawner") && !this.spawners.get().booleanValue()) {
            return false;
        }
        if (name.contains("shulker_box") && !this.shulkerBoxes.get().booleanValue()) {
            return false;
        }
        if ((name.contains("furnace") || name.contains("blast_furnace") || name.contains("smoker")) && !this.furnaces.get().booleanValue()) {
            return false;
        }
        if (name.contains("barrel") && !this.barrels.get().booleanValue()) {
            return false;
        }
        if (name.contains("dispenser") && !this.dispensers.get().booleanValue()) {
            return false;
        }
        return !name.contains("hopper") || this.hoppers.get() != false;
    }

    private Color getFillColor(String name) {
        Color base = this.getBaseColor(name);
        return new Color(base.r, base.g, base.b, this.fill.get() != false ? this.alpha.get() : 0);
    }

    private Color getOutlineColor(String name) {
        Color base = this.getBaseColor(name);
        return new Color(base.r, base.g, base.b, this.outline.get() != false ? 255 : 0);
    }

    private Color getBaseColor(String name) {
        if (name.contains("trapped_chest")) {
            return TRAPPED_COLOR;
        }
        if (name.contains("chest")) {
            return CHEST_COLOR;
        }
        if (name.contains("ender_chest")) {
            return ENDER_COLOR;
        }
        if (name.contains("spawner")) {
            return SPAWNER_COLOR;
        }
        if (name.contains("shulker_box")) {
            return SHULKER_COLOR;
        }
        if (name.contains("furnace") || name.contains("blast_furnace") || name.contains("smoker")) {
            return FURNACE_COLOR;
        }
        if (name.contains("barrel")) {
            return BARREL_COLOR;
        }
        if (name.contains("dispenser")) {
            return DISPENSER_COLOR;
        }
        if (name.contains("hopper")) {
            return HOPPER_COLOR;
        }
        return CHEST_COLOR;
    }

    private void renderBox(Render3DEvent event, class_238 box, Color fillColor, Color outlineColor) {
        if (this.fill.get().booleanValue() && fillColor.a > 0) {
            event.renderer.box(box.field_1323, box.field_1322, box.field_1321, box.field_1320, box.field_1325, box.field_1324, fillColor, null, ShapeMode.Sides, 0);
        }
        if (this.outline.get().booleanValue() && outlineColor.a > 0) {
            event.renderer.box(box.field_1323, box.field_1322, box.field_1321, box.field_1320, box.field_1325, box.field_1324, null, outlineColor, ShapeMode.Lines, 0);
        }
    }
}

