/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1297
 *  net.minecraft.class_1309
 *  net.minecraft.class_1657
 *  net.minecraft.class_2189
 *  net.minecraft.class_2246
 *  net.minecraft.class_2248
 *  net.minecraft.class_2338
 *  net.minecraft.class_3532
 */
package meteordevelopment.meteorclient.systems.modules.render;

import java.util.HashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import meteordevelopment.meteorclient.events.render.Render3DEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.renderer.ShapeMode;
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.Utils;
import meteordevelopment.meteorclient.utils.render.color.Color;
import meteordevelopment.meteorclient.utils.render.color.RainbowColor;
import meteordevelopment.meteorclient.utils.render.color.RainbowColors;
import meteordevelopment.meteorclient.utils.render.color.SettingColor;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1297;
import net.minecraft.class_1309;
import net.minecraft.class_1657;
import net.minecraft.class_2189;
import net.minecraft.class_2246;
import net.minecraft.class_2248;
import net.minecraft.class_2338;
import net.minecraft.class_3532;

public class HitParticles
extends Module {
    private final SettingGroup sgGeneral;
    private final Setting<Mode> mode;
    private final Setting<Physics> physics;
    private final Setting<SettingColor> colorrr;
    private final Setting<Boolean> onlySelf;
    private final Setting<Integer> amount;
    private final Setting<Integer> lifeTime;
    private final Setting<Integer> speed;
    private final Setting<Double> starsScale;
    private final Setting<ColorMode> colorMode;
    private final Setting<SettingColor> colorH;
    private final Setting<SettingColor> colorD;
    private final HashMap<Integer, Float> healthMap;
    private final CopyOnWriteArrayList<Particle> particles;

    public HitParticles() {
        super(Categories.Render, "hit-particles", "Show particles when players take damage.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.mode = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("mode")).description("Particle style.")).defaultValue(Mode.Stars)).build());
        this.physics = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("physics")).description("Particle physics style.")).defaultValue(Physics.Fall)).build());
        this.colorrr = this.sgGeneral.add(((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("color")).description("Custom particle color.")).defaultValue(new SettingColor(-2013200640)).build());
        this.onlySelf = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("self")).description("Only show particles for the local player.")).defaultValue(false)).build());
        this.amount = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("amount")).description("Number of particles spawned per hit.")).defaultValue(2)).min(1).max(5).build());
        this.lifeTime = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("life-time")).description("How long particles last in seconds.")).defaultValue(2)).min(1).max(10).build());
        this.speed = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("speed")).description("Particle speed.")).defaultValue(2)).min(1).max(20).build());
        this.starsScale = this.sgGeneral.add(((DoubleSetting.Builder)((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("scale")).description("Particle scale.")).defaultValue(3.0).min(1.0).max(10.0).visible(() -> this.mode.get() != Mode.Orbiz)).build());
        this.colorMode = this.sgGeneral.add(((EnumSetting.Builder)((EnumSetting.Builder)((EnumSetting.Builder)new EnumSetting.Builder().name("color-mode")).description("Choose synced or custom color.")).defaultValue(ColorMode.Sync)).build());
        this.colorH = this.sgGeneral.add(((ColorSetting.Builder)((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("heal-color")).description("Color for heal text particles.")).defaultValue(new SettingColor(3142544)).visible(() -> this.mode.get() == Mode.Text)).build());
        this.colorD = this.sgGeneral.add(((ColorSetting.Builder)((ColorSetting.Builder)((ColorSetting.Builder)new ColorSetting.Builder().name("damage-color")).description("Color for damage text particles.")).defaultValue(new SettingColor(15811379)).visible(() -> this.mode.get() == Mode.Text)).build());
        this.healthMap = new HashMap();
        this.particles = new CopyOnWriteArrayList();
    }

    @EventHandler
    private void onTick(TickEvent.Post event) {
        this.particles.removeIf(Particle::update);
        if (this.mc.field_1724 == null || this.mc.field_1687 == null) {
            return;
        }
        if (this.mode.get() == Mode.Text) {
            for (class_1297 entity : this.mc.field_1687.method_18112()) {
                if (entity == null || !entity.method_5805() || !(entity instanceof class_1309)) continue;
                class_1309 lent = (class_1309)entity;
                if (this.mc.field_1724.method_5858(entity) > 256.0) continue;
                float health = lent.method_6032() + lent.method_6067();
                float lastHealth = this.healthMap.getOrDefault(entity.method_5628(), Float.valueOf(health)).floatValue();
                this.healthMap.put(entity.method_5628(), Float.valueOf(health));
                if (lastHealth == health) continue;
                RainbowColor c = this.colorMode.get() == ColorMode.Sync ? RainbowColors.GLOBAL : this.colorrr.get().copy();
                float diff = lastHealth - health;
                float y = (float)Utils.random(lent.method_23318(), lent.method_23318() + (double)lent.method_17682());
                this.particles.add(new Particle((float)lent.method_23317(), y, (float)lent.method_23321(), c, Utils.random(0, 180), Utils.random(10, 60), diff));
            }
            return;
        }
        for (class_1657 player : this.mc.field_1687.method_18456()) {
            if (player == null || this.onlySelf.get().booleanValue() && player != this.mc.field_1724 || player.field_6235 <= 0) continue;
            RainbowColor c = this.colorMode.get() == ColorMode.Sync ? RainbowColors.GLOBAL : this.colorrr.get().copy();
            for (int i = 0; i < this.amount.get(); ++i) {
                float y = (float)Utils.random(player.method_23318(), player.method_23318() + (double)player.method_17682());
                this.particles.add(new Particle((float)player.method_23317(), y, (float)player.method_23321(), c, Utils.random(0, 180), Utils.random(10, 60), 0.0f));
            }
        }
    }

    @EventHandler
    private void onRender3D(Render3DEvent event) {
        if (this.mc.field_1724 == null || this.mc.field_1687 == null) {
            return;
        }
        for (Particle particle : this.particles) {
            particle.render(event);
        }
    }

    private Color getHealthColor(float health) {
        return health > 0.0f ? this.colorH.get().copy() : this.colorD.get().copy();
    }

    private float randFloat(float min, float max) {
        return (float)Utils.random(min, max);
    }

    private boolean posBlock(double x, double y, double z) {
        class_2248 b = this.mc.field_1687.method_8320(class_2338.method_49637((double)x, (double)y, (double)z)).method_26204();
        return !(b instanceof class_2189) && b != class_2246.field_10382 && b != class_2246.field_10164;
    }

    public static enum Mode {
        Orbiz,
        Stars,
        Hearts,
        Bloom,
        Text;

    }

    public static enum Physics {
        Fall,
        Fly;

    }

    public static enum ColorMode {
        Custom,
        Sync;

    }

    public class Particle {
        private float x;
        private float y;
        private float z;
        private float px;
        private float py;
        private float pz;
        private float motionX;
        private float motionY;
        private float motionZ;
        private float rotationAngle;
        private final float rotationSpeed;
        private final float health;
        private final long time;
        private final Color color;

        public Particle(float x, float y, float z, Color color, float rotationAngle, float rotationSpeed, float health) {
            this.x = x;
            this.y = y;
            this.z = z;
            this.px = x;
            this.py = y;
            this.pz = z;
            this.motionX = HitParticles.this.randFloat((float)(-HitParticles.this.speed.get().intValue()) / 50.0f, (float)HitParticles.this.speed.get().intValue() / 50.0f);
            this.motionY = HitParticles.this.randFloat((float)(-HitParticles.this.speed.get().intValue()) / 50.0f, (float)HitParticles.this.speed.get().intValue() / 50.0f);
            this.motionZ = HitParticles.this.randFloat((float)(-HitParticles.this.speed.get().intValue()) / 50.0f, (float)HitParticles.this.speed.get().intValue() / 50.0f);
            this.rotationAngle = rotationAngle;
            this.rotationSpeed = rotationSpeed;
            this.health = health;
            this.time = System.currentTimeMillis();
            this.color = color;
        }

        public boolean update() {
            if (((HitParticles)HitParticles.this).mc.field_1724 == null || ((HitParticles)HitParticles.this).mc.field_1687 == null) {
                return true;
            }
            double sp = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
            this.px = this.x;
            this.py = this.y;
            this.pz = this.z;
            this.x += this.motionX;
            this.y += this.motionY;
            this.z += this.motionZ;
            if (HitParticles.this.posBlock(this.x, this.y - HitParticles.this.starsScale.get().floatValue() / 10.0f, this.z)) {
                this.motionY = -this.motionY / 1.1f;
                this.motionX /= 1.1f;
                this.motionZ /= 1.1f;
            } else if (HitParticles.this.posBlock((double)this.x - sp, this.y, (double)this.z - sp) || HitParticles.this.posBlock((double)this.x + sp, this.y, (double)this.z + sp) || HitParticles.this.posBlock((double)this.x + sp, this.y, (double)this.z - sp) || HitParticles.this.posBlock((double)this.x - sp, this.y, (double)this.z + sp) || HitParticles.this.posBlock((double)this.x + sp, this.y, this.z) || HitParticles.this.posBlock((double)this.x - sp, this.y, this.z) || HitParticles.this.posBlock(this.x, this.y, (double)this.z + sp) || HitParticles.this.posBlock(this.x, this.y, (double)this.z - sp)) {
                this.motionX = -this.motionX;
                this.motionZ = -this.motionZ;
            }
            if (HitParticles.this.physics.get() == Physics.Fall) {
                this.motionY -= 0.035f;
            }
            this.motionX /= 1.005f;
            this.motionZ /= 1.005f;
            this.motionY /= 1.005f;
            this.rotationAngle += this.rotationSpeed * 0.02f;
            return System.currentTimeMillis() - this.time > (long)HitParticles.this.lifeTime.get().intValue() * 1000L;
        }

        public void render(Render3DEvent event) {
            Color drawColor = HitParticles.this.mode.get() == Mode.Text ? HitParticles.this.getHealthColor(this.health) : this.color.copy();
            float alpha = Math.max(0.0f, 1.0f - (float)(System.currentTimeMillis() - this.time) / ((float)HitParticles.this.lifeTime.get().intValue() * 1000.0f));
            drawColor.a((int)((float)drawColor.a * alpha));
            float interpX = class_3532.method_16439((float)event.tickDelta, (float)this.px, (float)this.x);
            float interpY = class_3532.method_16439((float)event.tickDelta, (float)this.py, (float)this.y);
            float interpZ = class_3532.method_16439((float)event.tickDelta, (float)this.pz, (float)this.z);
            float scale = HitParticles.this.starsScale.get().floatValue() * 0.05f;
            if (HitParticles.this.mode.get() == Mode.Text) {
                event.renderer.box((double)interpX - 0.05, interpY, (double)interpZ - 0.05, (double)interpX + 0.05, (double)interpY + 0.1, (double)interpZ + 0.05, drawColor, drawColor, ShapeMode.Both, 0);
            } else if (HitParticles.this.mode.get() == Mode.Orbiz) {
                float[] radii;
                for (float r : radii = new float[]{0.06f, 0.1f, 0.14f}) {
                    for (int i = 0; i < 24; ++i) {
                        float a1 = (float)(Math.PI * 2 * (double)i / 24.0);
                        float a2 = (float)(Math.PI * 2 * (double)(i + 1) / 24.0);
                        event.renderer.line((double)interpX + Math.cos(a1) * (double)r, interpY, (double)interpZ + Math.sin(a1) * (double)r, (double)interpX + Math.cos(a2) * (double)r, interpY, (double)interpZ + Math.sin(a2) * (double)r, drawColor);
                    }
                }
            } else {
                int arms = HitParticles.this.mode.get() == Mode.Bloom ? 8 : 4;
                for (int i = 0; i < arms; ++i) {
                    double angle = Math.PI * 2 * (double)i / (double)arms + Math.toRadians(this.rotationAngle);
                    double dx = Math.cos(angle) * (double)scale;
                    double dy = Math.sin(angle) * (double)scale;
                    event.renderer.line(interpX, interpY, interpZ, (double)interpX + dx, (double)interpY + dy, interpZ, drawColor);
                }
            }
        }
    }
}

