/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_2338
 *  net.minecraft.class_243
 *  net.minecraft.class_3532
 *  net.minecraft.class_4184
 */
package meteordevelopment.meteorclient.systems.modules.render;

import java.util.ArrayList;
import java.util.List;
import meteordevelopment.meteorclient.events.render.Render3DEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.ColorSetting;
import meteordevelopment.meteorclient.settings.DoubleSetting;
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.Color;
import meteordevelopment.meteorclient.utils.render.color.RainbowColors;
import meteordevelopment.meteorclient.utils.render.color.SettingColor;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_2338;
import net.minecraft.class_243;
import net.minecraft.class_3532;
import net.minecraft.class_4184;

public class Particles
extends Module {
    private final SettingGroup sgGeneral;
    private final SettingGroup sgFireFlies;
    private final SettingGroup sgParticles;
    private final Setting<Boolean> fireFlies;
    private final Setting<Integer> ffCount;
    private final Setting<Double> ffSize;
    private final Setting<Mode> mode;
    private final Setting<Integer> count;
    private final Setting<Double> size;
    private final Setting<ColorMode> colorMode;
    private final Setting<SettingColor> color;
    private final Setting<Physics> physics;
    private final List<ParticleBase> fireFliesList;
    private final List<ParticleBase> particlesList;

    public Particles() {
        super(Categories.Render, "particles", "Render decorative particles around you.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.sgFireFlies = this.settings.createGroup("FireFlies");
        this.sgParticles = this.settings.createGroup("Particles");
        this.fireFlies = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("fire-flies")).description("Render firefly particles around you.")).defaultValue(true)).build());
        this.ffCount = this.sgFireFlies.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("ff-count")).description("How many fireflies to keep active.")).defaultValue(30)).min(20).max(200).visible(this.fireFlies::get)).build());
        this.ffSize = this.sgFireFlies.add(((DoubleSetting.Builder)((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("ff-size")).description("Size of the firefly trail.")).defaultValue(1.0).min(0.1).max(2.0).visible(this.fireFlies::get)).build());
        this.mode = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("mode")).description("Particle style.")).defaultValue(Mode.SnowFlake)).build());
        this.count = this.sgParticles.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("count")).description("How many particles to render.")).defaultValue(100)).min(20).max(800).visible(() -> this.mode.get() != Mode.Off)).build());
        this.size = this.sgParticles.add(((DoubleSetting.Builder)((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("size")).description("Particle size.")).defaultValue(1.0).min(0.1).max(6.0).visible(() -> this.mode.get() != Mode.Off)).build());
        this.colorMode = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("color-mode")).description("Color mode for the particles.")).defaultValue(ColorMode.Sync)).build());
        this.color = this.sgGeneral.add(((ColorSetting.Builder)((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("color")).description("Particle color when using custom color mode.")).defaultValue(new SettingColor(255, 255, 255, 255)).visible(() -> this.colorMode.get() == ColorMode.Custom)).build());
        this.physics = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("physics")).description("How gravity affects the particles.")).defaultValue(Physics.Fly)).visible(() -> this.mode.get() != Mode.Off)).build());
        this.fireFliesList = new ArrayList<ParticleBase>();
        this.particlesList = new ArrayList<ParticleBase>();
    }

    @EventHandler
    private void onTick(TickEvent.Post event) {
        int i;
        if (this.mc.field_1724 == null || this.mc.field_1687 == null) {
            return;
        }
        this.fireFliesList.removeIf(ParticleBase::tick);
        this.particlesList.removeIf(ParticleBase::tick);
        if (this.fireFlies.get().booleanValue()) {
            for (i = this.fireFliesList.size(); i < this.ffCount.get(); ++i) {
                this.fireFliesList.add(new FireFly((float)(this.mc.field_1724.method_23317() + (double)this.random(-25.0f, 25.0f)), (float)(this.mc.field_1724.method_23318() + (double)this.random(2.0f, 15.0f)), (float)(this.mc.field_1724.method_23321() + (double)this.random(-25.0f, 25.0f)), this.random(-0.2f, 0.2f), this.random(-0.1f, 0.1f), this.random(-0.2f, 0.2f)));
            }
        }
        if (this.mode.get() != Mode.Off) {
            for (i = this.particlesList.size(); i < this.count.get(); ++i) {
                boolean drop = this.physics.get() == Physics.Drop;
                this.particlesList.add(new ParticleBase((float)(this.mc.field_1724.method_23317() + (double)this.random(-48.0f, 48.0f)), (float)(this.mc.field_1724.method_23318() + (double)this.random(2.0f, 48.0f)), (float)(this.mc.field_1724.method_23321() + (double)this.random(-48.0f, 48.0f)), drop ? 0.0f : this.random(-0.4f, 0.4f), drop ? this.random(-0.2f, -0.05f) : this.random(-0.1f, 0.1f), drop ? 0.0f : this.random(-0.4f, 0.4f)));
            }
        }
    }

    @EventHandler
    private void onRender3D(Render3DEvent event) {
        if (this.mc.field_1724 == null || this.mc.field_1687 == null) {
            return;
        }
        if (this.fireFlies.get().booleanValue()) {
            for (ParticleBase particle : this.fireFliesList) {
                if (!(particle instanceof FireFly)) continue;
                ((FireFly)particle).renderFireFly(event);
            }
        }
        if (this.mode.get() != Mode.Off) {
            for (ParticleBase particle : this.particlesList) {
                particle.render(event);
            }
        }
    }

    private double random(double min, double max) {
        return min + Math.random() * (max - min);
    }

    private float random(float min, float max) {
        return min + (float)Math.random() * (max - min);
    }

    private Color getActiveColor() {
        return this.colorMode.get() == ColorMode.Sync ? RainbowColors.GLOBAL : (Color)this.color.get();
    }

    private Color injectAlpha(Color input, int alpha) {
        return new Color(input.r, input.g, input.b, alpha);
    }

    private class_243 interpolatePos(double x1, double y1, double z1, double x2, double y2, double z2, float tickDelta) {
        return new class_243(x1 + (x2 - x1) * (double)tickDelta, y1 + (y2 - y1) * (double)tickDelta, z1 + (z2 - z1) * (double)tickDelta);
    }

    public static enum Mode {
        Off,
        SnowFlake,
        Stars,
        Hearts,
        Dollars,
        Bloom;

    }

    public static enum ColorMode {
        Custom,
        Sync;

    }

    public static enum Physics {
        Drop,
        Fly;

    }

    public class FireFly
    extends ParticleBase {
        private final List<Trail> trails;

        public FireFly(float posX, float posY, float posZ, float motionX, float motionY, float motionZ) {
            super(posX, posY, posZ, motionX, motionY, motionZ);
            this.trails = new ArrayList<Trail>();
        }

        @Override
        public boolean tick() {
            if (((Particles)Particles.this).mc.field_1724 == null || ((Particles)Particles.this).mc.field_1687 == null) {
                return true;
            }
            this.age = ((Particles)Particles.this).mc.field_1724.method_5649((double)this.posX, (double)this.posY, (double)this.posZ) > 100.0 ? (this.age -= 4) : (!((Particles)Particles.this).mc.field_1687.method_8320(new class_2338((int)this.posX, (int)this.posY, (int)this.posZ)).method_26215() ? (this.age -= 8) : --this.age);
            if (this.age < 0) {
                return true;
            }
            this.trails.removeIf(Trail::update);
            this.prevposX = this.posX;
            this.prevposY = this.posY;
            this.prevposZ = this.posZ;
            this.posX += this.motionX;
            this.posY += this.motionY;
            this.posZ += this.motionZ;
            this.trails.add(new Trail(Particles.this, new class_243((double)this.prevposX, (double)this.prevposY, (double)this.prevposZ), new class_243((double)this.posX, (double)this.posY, (double)this.posZ), Particles.this.getActiveColor().copy()));
            this.motionX *= 0.99f;
            this.motionY *= 0.99f;
            this.motionZ *= 0.99f;
            return false;
        }

        public void renderFireFly(Render3DEvent event) {
            if (this.trails.isEmpty()) {
                return;
            }
            class_4184 camera = ((Particles)Particles.this).mc.field_1773.method_19418();
            class_243 camPos = camera.method_71156();
            for (Trail trail : this.trails) {
                class_243 pos = trail.interpolate(1.0f);
                int alpha = (int)(255.0f * ((float)this.age / (float)this.maxAge));
                Color color = Particles.this.injectAlpha(trail.color, (int)((float)alpha * trail.animation(0.0f)));
                float s = Particles.this.ffSize.get().floatValue();
                double rx = pos.field_1352 - camPos.field_1352;
                double ry = pos.field_1351 - camPos.field_1351;
                double rz = pos.field_1350 - camPos.field_1350;
                event.renderer.line(rx - (double)s, ry, rz, rx + (double)s, ry, rz, color);
                event.renderer.line(rx, ry - (double)s, rz, rx, ry + (double)s, rz, color);
            }
        }
    }

    public class ParticleBase {
        protected float prevposX;
        protected float prevposY;
        protected float prevposZ;
        protected float posX;
        protected float posY;
        protected float posZ;
        protected float motionX;
        protected float motionY;
        protected float motionZ;
        protected int age;
        protected int maxAge;

        public ParticleBase(float posX, float posY, float posZ, float motionX, float motionY, float motionZ) {
            this.posX = posX;
            this.posY = posY;
            this.posZ = posZ;
            this.prevposX = posX;
            this.prevposY = posY;
            this.prevposZ = posZ;
            this.motionX = motionX;
            this.motionY = motionY;
            this.motionZ = motionZ;
            this.maxAge = this.age = (int)Particles.this.random(100.0f, 300.0f);
        }

        public boolean tick() {
            if (((Particles)Particles.this).mc.field_1724 == null) {
                return true;
            }
            this.age = ((Particles)Particles.this).mc.field_1724.method_5649((double)this.posX, (double)this.posY, (double)this.posZ) > 4096.0 ? (this.age -= 8) : --this.age;
            if (this.age < 0) {
                return true;
            }
            this.prevposX = this.posX;
            this.prevposY = this.posY;
            this.prevposZ = this.posZ;
            this.posX += this.motionX;
            this.posY += this.motionY;
            this.posZ += this.motionZ;
            this.motionX *= 0.9f;
            if (Particles.this.physics.get() == Physics.Fly) {
                this.motionY *= 0.9f;
            }
            this.motionZ *= 0.9f;
            this.motionY -= 0.001f;
            return false;
        }

        public void render(Render3DEvent event) {
            class_4184 camera = ((Particles)Particles.this).mc.field_1773.method_19418();
            class_243 camPos = camera.method_71156();
            Color activeColor = Particles.this.injectAlpha(Particles.this.getActiveColor(), (int)(255.0f * ((float)this.age / (float)this.maxAge)));
            double rx = (double)class_3532.method_16439((float)event.tickDelta, (float)this.prevposX, (float)this.posX) - camPos.field_1352;
            double ry = (double)class_3532.method_16439((float)event.tickDelta, (float)this.prevposY, (float)this.posY) - camPos.field_1351;
            double rz = (double)class_3532.method_16439((float)event.tickDelta, (float)this.prevposZ, (float)this.posZ) - camPos.field_1350;
            float s = Particles.this.size.get().floatValue();
            event.renderer.line(rx - (double)s, ry, rz, rx + (double)s, ry, rz, activeColor);
            event.renderer.line(rx, ry - (double)s, rz, rx, ry + (double)s, rz, activeColor);
            event.renderer.line(rx, ry, rz - (double)s, rx, ry, rz + (double)s, activeColor);
        }
    }

    private class Trail {
        private final class_243 from;
        private final class_243 to;
        private final Color color;
        private int age;
        private static final int MAX_AGE = 20;

        private Trail(Particles particles, class_243 from, class_243 to, Color color) {
            this.from = from;
            this.to = to;
            this.color = color;
            this.age = 0;
        }

        private boolean update() {
            ++this.age;
            return this.age > 20;
        }

        private class_243 interpolate(float delta) {
            return new class_243(this.from.field_1352 + (this.to.field_1352 - this.from.field_1352) * (double)delta, this.from.field_1351 + (this.to.field_1351 - this.from.field_1351) * (double)delta, this.from.field_1350 + (this.to.field_1350 - this.from.field_1350) * (double)delta);
        }

        private float animation(float delta) {
            return Math.max(0.0f, 1.0f - ((float)this.age + delta) / 20.0f);
        }
    }
}

