package net.minecraft.world.level.levelgen.material.rule;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.SharedConstants;
import net.minecraft.resources.Identifier;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.PositionalRandomFactory;
import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
import net.minecraft.world.level.levelgen.material.MaterialRuleContext;
import net.minecraft.world.level.levelgen.material.MaterialRules;

public record OreVeinRule(
	BlockState oreBlock,
	BlockState rawOreBlock,
	BlockState fillerBlock,
	float rawOreChance,
	DensityFunction density,
	DensityFunction richness,
	DensityFunction fillerGap
) implements MaterialRule {
	public static final float VEININESS_THRESHOLD = 0.4F;
	public static final int EDGE_ROUNDOFF_BEGIN = 20;
	public static final float MAX_EDGE_ROUNDOFF = 0.2F;
	public static final float VEIN_SOLIDNESS = 0.7F;
	public static final float MIN_RICHNESS = 0.1F;
	public static final float MAX_RICHNESS = 0.3F;
	public static final float MAX_RICHNESS_THRESHOLD = 0.6F;
	private static final float CHANCE_OF_RAW_ORE_BLOCK = 0.02F;
	public static final float SKIP_ORE_IF_GAP_NOISE_IS_BELOW = -0.3F;
	public static final MapCodec<OreVeinRule> CODEC = RecordCodecBuilder.mapCodec(
		i -> i.group(
				BlockState.CODEC.fieldOf("ore_block").forGetter(OreVeinRule::oreBlock),
				BlockState.CODEC.fieldOf("raw_ore_block").forGetter(OreVeinRule::rawOreBlock),
				BlockState.CODEC.fieldOf("filler_block").forGetter(OreVeinRule::fillerBlock),
				Codec.floatRange(0.0F, 1.0F).fieldOf("raw_ore_chance").forGetter(OreVeinRule::rawOreChance),
				DensityFunction.CODEC.fieldOf("density").forGetter(OreVeinRule::density),
				DensityFunction.CODEC.fieldOf("richness").forGetter(OreVeinRule::richness),
				DensityFunction.CODEC.fieldOf("filler_gap").forGetter(OreVeinRule::fillerGap)
			)
			.apply(i, OreVeinRule::new)
	);

	@Override
	public RuleEvaluator compile(final MaterialRuleContext context) {
		if (SharedConstants.DEBUG_DISABLE_ORE_VEINS) {
			return (var0, var1, var2x) -> null;
		}

		BlockState defaultState = SharedConstants.DEBUG_ORE_VEINS ? Blocks.AIR.defaultBlockState() : null;
		MaterialRules.DensityGetter densitySampler = context.getDensitiesInChunk(this.density, true);
		MaterialRules.DensityGetter richnessSampler = context.getDensitiesInChunk(this.richness, true);
		MaterialRules.DensityGetter fillerGapSampler = context.getDensitiesInChunk(this.fillerGap, false);
		PositionalRandomFactory randomFactory = context.getOrCreateRandomFactory(Identifier.withDefaultNamespace("ore"));
		BlockState fillerBlock = SharedConstants.DEBUG_ORE_VEINS ? Blocks.OAK_BUTTON.defaultBlockState() : this.fillerBlock;
		return (blockX, blockY, blockZ) -> {
			float density = densitySampler.get();
			if (density <= 0.0F) {
				return defaultState;
			} else {
				RandomSource random = randomFactory.at(blockX, blockY, blockZ);
				if (random.nextFloat() > density) {
					return defaultState;
				} else {
					float richness = richnessSampler.get();
					if (random.nextFloat() < richness && fillerGapSampler.get() < 0.0F) {
						return random.nextFloat() < this.rawOreChance ? this.rawOreBlock : this.oreBlock;
					} else {
						return fillerBlock;
					}
				}
			}
		};
	}

	@Override
	public MapCodec<OreVeinRule> codec() {
		return CODEC;
	}

	public enum VeinType {
		COPPER(Blocks.COPPER_ORE.defaultBlockState(), Blocks.RAW_COPPER_BLOCK.defaultBlockState(), Blocks.GRANITE.defaultBlockState(), 0, 50),
		IRON(Blocks.DEEPSLATE_IRON_ORE.defaultBlockState(), Blocks.RAW_IRON_BLOCK.defaultBlockState(), Blocks.TUFF.defaultBlockState(), -60, -8);

		private final BlockState oreBlock;
		private final BlockState rawOreBlock;
		private final BlockState fillerBlock;
		public final int minY;
		public final int maxY;

		VeinType(final BlockState oreBlock, final BlockState rawOreBlock, final BlockState fillerBlock, final int minY, final int maxY) {
			this.oreBlock = oreBlock;
			this.rawOreBlock = rawOreBlock;
			this.fillerBlock = fillerBlock;
			this.minY = minY;
			this.maxY = maxY;
		}

		public OreVeinRule create(final DensityFunction density, final DensityFunction richness, final DensityFunction fillerGap) {
			return new OreVeinRule(this.oreBlock, this.rawOreBlock, this.fillerBlock, 0.02F, density, richness, fillerGap);
		}
	}
}
