package net.minecraft.client.particle;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.particles.PowerParticleOption;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;

@Environment(EnvType.CLIENT)
public class DragonBreathParticle extends SingleQuadParticle {
	private static final int COLOR_MIN = 11993298;
	private static final int COLOR_MAX = 14614777;
	private static final float COLOR_MIN_RED = 0.7176471F;
	private static final float COLOR_MIN_GREEN = 0.0F;
	private static final float COLOR_MIN_BLUE = 0.8235294F;
	private static final float COLOR_MAX_RED = 0.8745098F;
	private static final float COLOR_MAX_GREEN = 0.0F;
	private static final float COLOR_MAX_BLUE = 0.9764706F;
	private boolean hasHitGround;
	private final SpriteSet sprites;

	private DragonBreathParticle(
		final ClientLevel level, final double x, final double y, final double z, final double xa, final double ya, final double za, final SpriteSet sprites
	) {
		super(level, x, y, z, sprites.first());
		this.friction = 0.96F;
		this.xd = xa;
		this.yd = ya;
		this.zd = za;
		this.rCol = Mth.nextFloat(this.random, 0.7176471F, 0.8745098F);
		this.gCol = Mth.nextFloat(this.random, 0.0F, 0.0F);
		this.bCol = Mth.nextFloat(this.random, 0.8235294F, 0.9764706F);
		this.quadSize *= 0.75F;
		this.lifetime = (int)(20.0 / (this.random.nextFloat() * 0.8 + 0.2));
		this.hasHitGround = false;
		this.hasPhysics = false;
		this.sprites = sprites;
		this.setSpriteFromAge(sprites);
	}

	@Override
	public void tick() {
		this.xo = this.x;
		this.yo = this.y;
		this.zo = this.z;
		if (this.age++ >= this.lifetime) {
			this.remove();
		} else {
			this.setSpriteFromAge(this.sprites);
			if (this.onGround) {
				this.yd = 0.0;
				this.hasHitGround = true;
			}

			if (this.hasHitGround) {
				this.yd += 0.002;
			}

			this.move(this.xd, this.yd, this.zd);
			if (this.y == this.yo) {
				this.xd *= 1.1;
				this.zd *= 1.1;
			}

			this.xd = this.xd * this.friction;
			this.zd = this.zd * this.friction;
			if (this.hasHitGround) {
				this.yd = this.yd * this.friction;
			}
		}
	}

	@Override
	public SingleQuadParticle.Layer getLayer() {
		return SingleQuadParticle.Layer.OPAQUE;
	}

	@Override
	public float getQuadSize(final float a) {
		return this.quadSize * Mth.clamp((this.age + a) / this.lifetime * 32.0F, 0.0F, 1.0F);
	}

	@Environment(EnvType.CLIENT)
	public static class Provider implements ParticleProvider<PowerParticleOption> {
		private final SpriteSet sprites;

		public Provider(final SpriteSet sprites) {
			this.sprites = sprites;
		}

		public Particle createParticle(
			final PowerParticleOption options,
			final ClientLevel level,
			final double x,
			final double y,
			final double z,
			final double xAux,
			final double yAux,
			final double zAux,
			final RandomSource random
		) {
			DragonBreathParticle particle = new DragonBreathParticle(level, x, y, z, xAux, yAux, zAux, this.sprites);
			particle.setPower(options.getPower());
			return particle;
		}
	}
}
