/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_2244
 *  net.minecraft.class_2246
 *  net.minecraft.class_2248
 *  net.minecraft.class_2338
 *  net.minecraft.class_2480
 *  net.minecraft.class_2818
 *  net.minecraft.class_2826
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import meteordevelopment.meteorclient.events.render.Render3DEvent;
import meteordevelopment.meteorclient.events.world.ChunkDataEvent;
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.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.render.color.SettingColor;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_2244;
import net.minecraft.class_2246;
import net.minecraft.class_2248;
import net.minecraft.class_2338;
import net.minecraft.class_2480;
import net.minecraft.class_2818;
import net.minecraft.class_2826;

public class StorageFinder
extends Module {
    private final SettingGroup sgFilter;
    private final SettingGroup sgRender;
    private final Setting<Boolean> chests;
    private final Setting<Boolean> barrels;
    private final Setting<Boolean> shulkers;
    private final Setting<Boolean> furnaces;
    private final Setting<Boolean> hoppers;
    private final Setting<Boolean> dispensers;
    private final Setting<Boolean> beacons;
    private final Setting<Boolean> enderChests;
    private final Setting<Boolean> craftingTables;
    private final Setting<Boolean> beds;
    private final Setting<Boolean> anvils;
    private final Setting<Integer> renderRange;
    private final Setting<ShapeMode> shapeMode;
    private final Setting<SettingColor> chestColor;
    private final Setting<SettingColor> shulkerColor;
    private final Setting<SettingColor> furnaceColor;
    private final Setting<SettingColor> beaconColor;
    private final Setting<SettingColor> otherColor;
    private final Map<class_2338, StorageCategory> found;

    public StorageFinder() {
        super(Categories.Donut, "storage-finder", "Highlights storage blocks in loaded chunks to help locate player bases.");
        this.sgFilter = this.settings.createGroup("Filter");
        this.sgRender = this.settings.createGroup("Render");
        this.chests = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("chests")).description("Detect chests and trapped chests.")).defaultValue(true)).build());
        this.barrels = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("barrels")).description("Detect barrels.")).defaultValue(true)).build());
        this.shulkers = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("shulker-boxes")).description("Detect shulker boxes.")).defaultValue(true)).build());
        this.furnaces = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("furnaces")).description("Detect furnaces, blast furnaces, and smokers.")).defaultValue(true)).build());
        this.hoppers = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("hoppers")).description("Detect hoppers.")).defaultValue(true)).build());
        this.dispensers = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("dispensers")).description("Detect dispensers and droppers.")).defaultValue(true)).build());
        this.beacons = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("beacons")).description("Detect beacons.")).defaultValue(true)).build());
        this.enderChests = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("ender-chests")).description("Detect ender chests.")).defaultValue(false)).build());
        this.craftingTables = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("crafting-tables")).description("Detect crafting tables.")).defaultValue(true)).build());
        this.beds = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("beds")).description("Detect beds.")).defaultValue(true)).build());
        this.anvils = this.sgFilter.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("anvils")).description("Detect anvils.")).defaultValue(false)).build());
        this.renderRange = this.sgRender.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("render-range")).description("Max distance to render storage blocks.")).defaultValue(128)).range(16, 512).sliderRange(16, 256).build());
        this.shapeMode = this.sgRender.add(((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("shape-mode")).defaultValue(ShapeMode.Both)).build());
        this.chestColor = this.sgRender.add(((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("chest-color")).description("Color for chests/barrels.")).defaultValue(new SettingColor(255, 160, 0, 120)).build());
        this.shulkerColor = this.sgRender.add(((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("shulker-color")).description("Color for shulker boxes.")).defaultValue(new SettingColor(148, 0, 211, 120)).build());
        this.furnaceColor = this.sgRender.add(((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("furnace-color")).description("Color for furnaces.")).defaultValue(new SettingColor(255, 80, 0, 120)).build());
        this.beaconColor = this.sgRender.add(((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("beacon-color")).description("Color for beacons.")).defaultValue(new SettingColor(0, 255, 200, 180)).build());
        this.otherColor = this.sgRender.add(((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("other-color")).description("Color for other storage blocks.")).defaultValue(new SettingColor(255, 255, 255, 100)).build());
        this.found = new ConcurrentHashMap<class_2338, StorageCategory>();
    }

    @Override
    public void onActivate() {
        this.found.clear();
        if (this.mc.field_1687 == null) {
            return;
        }
        this.mc.field_1687.method_2935().method_14151();
        for (int cx = -16; cx <= 16; ++cx) {
            for (int cz = -16; cz <= 16 && this.mc.field_1724 != null; ++cz) {
                int chunkX = this.mc.field_1724.method_31476().field_9181 + cx;
                int chunkZ = this.mc.field_1724.method_31476().field_9180 + cz;
                class_2818 chunk = this.mc.field_1687.method_2935().method_21730(chunkX, chunkZ);
                if (chunk == null) continue;
                this.scanChunk(chunk);
            }
        }
    }

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

    @EventHandler
    private void onChunkLoad(ChunkDataEvent event) {
        this.scanChunk(event.chunk());
    }

    @EventHandler
    private void onRender3D(Render3DEvent event) {
        if (this.mc.field_1724 == null) {
            return;
        }
        double rangeSq = (double)this.renderRange.get().intValue() * (double)this.renderRange.get().intValue();
        for (Map.Entry<class_2338, StorageCategory> entry : this.found.entrySet()) {
            class_2338 pos = entry.getKey();
            if (this.mc.field_1724.method_5649((double)pos.method_10263() + 0.5, (double)pos.method_10264() + 0.5, (double)pos.method_10260() + 0.5) > rangeSq) continue;
            SettingColor sideColor = this.getColor(entry.getValue());
            SettingColor lineColor = new SettingColor(sideColor.r, sideColor.g, sideColor.b, 255);
            event.renderer.box(pos.method_10263(), pos.method_10264(), pos.method_10260(), pos.method_10263() + 1, pos.method_10264() + 1, pos.method_10260() + 1, sideColor, lineColor, this.shapeMode.get(), 0);
        }
    }

    private SettingColor getColor(StorageCategory cat) {
        return switch (cat.ordinal()) {
            case 0 -> this.chestColor.get();
            case 1 -> this.shulkerColor.get();
            case 2 -> this.furnaceColor.get();
            case 5 -> this.beaconColor.get();
            default -> this.otherColor.get();
        };
    }

    private void scanChunk(class_2818 chunk) {
        class_2826[] sections = chunk.method_12006();
        for (int si = 0; si < sections.length; ++si) {
            class_2826 sec = sections[si];
            if (sec == null || sec.method_38292()) continue;
            int baseY = chunk.method_31607() + si * 16;
            for (int x = 0; x < 16; ++x) {
                for (int z = 0; z < 16; ++z) {
                    for (int y = 0; y < 16; ++y) {
                        class_2248 block = sec.method_12254(x, y, z).method_26204();
                        StorageCategory cat = this.classify(block);
                        if (cat == null) continue;
                        class_2338 world = new class_2338(chunk.method_12004().method_8326() + x, baseY + y, chunk.method_12004().method_8328() + z);
                        this.found.put(world, cat);
                    }
                }
            }
        }
    }

    private StorageCategory classify(class_2248 b) {
        if (this.chests.get().booleanValue() && (b == class_2246.field_10034 || b == class_2246.field_10380)) {
            return StorageCategory.CHEST;
        }
        if (this.barrels.get().booleanValue() && b == class_2246.field_16328) {
            return StorageCategory.CHEST;
        }
        if (this.enderChests.get().booleanValue() && b == class_2246.field_10443) {
            return StorageCategory.CHEST;
        }
        if (this.shulkers.get().booleanValue() && b instanceof class_2480) {
            return StorageCategory.SHULKER;
        }
        if (this.furnaces.get().booleanValue() && (b == class_2246.field_10181 || b == class_2246.field_16333 || b == class_2246.field_16334)) {
            return StorageCategory.FURNACE;
        }
        if (this.hoppers.get().booleanValue() && b == class_2246.field_10312) {
            return StorageCategory.HOPPER;
        }
        if (this.dispensers.get().booleanValue() && (b == class_2246.field_10200 || b == class_2246.field_10228)) {
            return StorageCategory.DISPENSER;
        }
        if (this.beacons.get().booleanValue() && b == class_2246.field_10327) {
            return StorageCategory.BEACON;
        }
        if (this.craftingTables.get().booleanValue() && b == class_2246.field_9980) {
            return StorageCategory.OTHER;
        }
        if (this.beds.get().booleanValue() && b instanceof class_2244) {
            return StorageCategory.OTHER;
        }
        if (this.anvils.get().booleanValue() && (b == class_2246.field_10535 || b == class_2246.field_10105 || b == class_2246.field_10414)) {
            return StorageCategory.OTHER;
        }
        return null;
    }

    public static enum StorageCategory {
        CHEST,
        SHULKER,
        FURNACE,
        HOPPER,
        DISPENSER,
        BEACON,
        OTHER;

    }
}

