package net.halbear.aotg.custom.WorldGen;

import net.halbear.aotg.AgeoftheGods;
import net.halbear.aotg.registries.ModBlocks;
import net.halbear.aotg.utility.DataGenerators.AgeOfTheGodsDataGeneration;
import net.minecraft.core.RegistrySetBuilder;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstrapContext;
import net.minecraft.data.worldgen.SurfaceRuleData;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
import net.minecraft.world.level.levelgen.Noises;
import net.minecraft.world.level.levelgen.SurfaceRules;
import net.minecraft.world.level.levelgen.VerticalAnchor;
import net.minecraft.world.level.levelgen.placement.CaveSurface;

public class ModNoiseSettings {


    public static SurfaceRules.RuleSource makeRules(BootstrapContext<NoiseGeneratorSettings> context) {

        ResourceKey<net.minecraft.world.level.biome.Biome> garrigueKey =
                ResourceKey.create(Registries.BIOME, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "garrigue"));
        ResourceKey<net.minecraft.world.level.biome.Biome> maquisKey =
                ResourceKey.create(Registries.BIOME, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "maquis_shrubland"));
        ResourceKey<net.minecraft.world.level.biome.Biome> MediterraneanKey =
                ResourceKey.create(Registries.BIOME, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "mediterranean_forest"));

        SurfaceRules.ConditionSource isGarrigueGrassSection = SurfaceRules.noiseCondition(Noises.POWDER_SNOW, 0);
        SurfaceRules.ConditionSource isGarrigueGrassPatch = SurfaceRules.noiseCondition(AgeOfTheGodsDataGeneration.TINY_PATCHY_NOISE, 0.0);
        SurfaceRules.ConditionSource isMaquisGrassPatch = SurfaceRules.noiseCondition(AgeOfTheGodsDataGeneration.TINY_PATCHY_NOISE, 0.0);
        SurfaceRules.ConditionSource isGranite = SurfaceRules.noiseCondition(AgeOfTheGodsDataGeneration.TINY_PATCHY_NOISE, 0.1);
        SurfaceRules.ConditionSource isGraniteSurfacePatch = SurfaceRules.noiseCondition(Noises.PATCH, 0.0,0.15);
        SurfaceRules.ConditionSource isDeepRiver = SurfaceRules.noiseCondition(Noises.PATCH, 0.05,0.1);
        SurfaceRules.ConditionSource isRiverBed = SurfaceRules.noiseCondition(Noises.PATCH, -0.05,0.2);
        SurfaceRules.ConditionSource isPath = SurfaceRules.noiseCondition(Noises.EROSION_LARGE, -1.0,-0.5);
        SurfaceRules.ConditionSource atOrAboveSeaLevel = SurfaceRules.yBlockCheck(VerticalAnchor.absolute(63), 0);
        SurfaceRules.ConditionSource AboveSeaLevel = SurfaceRules.yBlockCheck(VerticalAnchor.absolute(64), 0);
        SurfaceRules.ConditionSource RiverCap = SurfaceRules.not(SurfaceRules.yBlockCheck(VerticalAnchor.absolute(72), 0));

        SurfaceRules.RuleSource MediterraneanSurface = SurfaceRules.sequence(
                SurfaceRules.ifTrue(
                        SurfaceRules.not(SurfaceRules.waterBlockCheck(-1,0)),
                        SurfaceRules.state(ModBlocks.SHALE_BLOCK.get().defaultBlockState())
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGraniteSurfacePatch,
                                SurfaceRules.ifTrue(
                                        AboveSeaLevel,
                                        SurfaceRules.ifTrue(
                                                RiverCap,
                                                SurfaceRules.state(Blocks.AIR.defaultBlockState())
                                        )
                                )
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, true, 2, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGraniteSurfacePatch,
                                SurfaceRules.ifTrue(
                                        RiverCap,
                                        SurfaceRules.state(Blocks.WATER.defaultBlockState())
                                )
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(1, true, 2, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isDeepRiver,
                                SurfaceRules.ifTrue(
                                        RiverCap,
                                        SurfaceRules.state(Blocks.WATER.defaultBlockState())
                                )
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(6, true, 3, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGraniteSurfacePatch,
                                SurfaceRules.ifTrue(
                                        RiverCap,
                                        SurfaceRules.state(ModBlocks.SHALE_BLOCK.get().defaultBlockState())
                                )
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(1, true, 2, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isRiverBed,
                                SurfaceRules.state(Blocks.SAND.defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGarrigueGrassSection,
                                SurfaceRules.state(ModBlocks.LIMESTONE_GRASS_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(SurfaceRules.stoneDepthCheck(10, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGarrigueGrassSection,
                                SurfaceRules.state(ModBlocks.LIMESTONE_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGarrigueGrassPatch,
                                SurfaceRules.state(ModBlocks.LIMESTONE_GRASS_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(SurfaceRules.stoneDepthCheck(10, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGarrigueGrassPatch,
                                SurfaceRules.state(ModBlocks.LIMESTONE_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.state(ModBlocks.SHALE_GRASS_BLOCK.get().defaultBlockState())
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(4, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.state(ModBlocks.SHALE_BLOCK.get().defaultBlockState())
                ),
                SurfaceRules.ifTrue(SurfaceRules.stoneDepthCheck(10, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.state(ModBlocks.LIMESTONE_BLOCK.get().defaultBlockState()))
        );

        SurfaceRules.RuleSource garrigueSurface = SurfaceRules.sequence(
                SurfaceRules.ifTrue(
                        SurfaceRules.not(SurfaceRules.waterBlockCheck(-1,0)),
                        SurfaceRules.state(Blocks.SANDSTONE.defaultBlockState())
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGraniteSurfacePatch,
                                SurfaceRules.state(Blocks.SAND.defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGarrigueGrassSection,
                                SurfaceRules.state(ModBlocks.LIMESTONE_GRASS_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(SurfaceRules.stoneDepthCheck(10, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGarrigueGrassSection,
                                SurfaceRules.state(ModBlocks.LIMESTONE_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGarrigueGrassPatch,
                                SurfaceRules.state(ModBlocks.LIMESTONE_GRASS_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(SurfaceRules.stoneDepthCheck(10, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGarrigueGrassPatch,
                        SurfaceRules.state(ModBlocks.LIMESTONE_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.state(Blocks.SANDSTONE.defaultBlockState())
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(4, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.state(Blocks.SANDSTONE.defaultBlockState())
                ),
                SurfaceRules.ifTrue(SurfaceRules.stoneDepthCheck(10, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.state(ModBlocks.LIMESTONE_BLOCK.get().defaultBlockState()))
        );

        SurfaceRules.RuleSource maquisSurface = SurfaceRules.sequence(
                SurfaceRules.ifTrue(
                        SurfaceRules.not(SurfaceRules.waterBlockCheck(-1,0)),
                        SurfaceRules.state(ModBlocks.SHALE_BLOCK.get().defaultBlockState())
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(6, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGraniteSurfacePatch,
                                SurfaceRules.state(Blocks.GRANITE.defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isPath,
                                SurfaceRules.state(ModBlocks.SHALE_GRASS_BLOCK.get().defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isMaquisGrassPatch,
                                SurfaceRules.state(ModBlocks.SHALE_GRASS_BLOCK.get().defaultBlockState())
                        )
                ),

                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(0, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.state(ModBlocks.SHALE_BLOCK.get().defaultBlockState())
                ),
                SurfaceRules.ifTrue(
                        SurfaceRules.stoneDepthCheck(10, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.ifTrue(
                                isGranite,
                                SurfaceRules.state(Blocks.GRANITE.defaultBlockState())
                        )
                ),
                SurfaceRules.ifTrue(SurfaceRules.stoneDepthCheck(10, false, 0, CaveSurface.FLOOR),
                        SurfaceRules.state(ModBlocks.SHALE_BLOCK.get().defaultBlockState()))
        );

        return SurfaceRules.sequence(
                SurfaceRules.ifTrue(SurfaceRules.isBiome(garrigueKey), garrigueSurface),
                SurfaceRules.ifTrue(SurfaceRules.isBiome(maquisKey), maquisSurface),
                SurfaceRules.ifTrue(SurfaceRules.isBiome(MediterraneanKey), MediterraneanSurface),
                SurfaceRuleData.overworld()
        );
    }
}