package net.yyc12345.teyvatcraft.init;

import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;

import net.minecraft.util.Identifier;
import net.minecraft.world.gen.GenerationStep;
import net.minecraft.world.gen.decorator.Decorator;
import net.minecraft.world.gen.decorator.RangeDecoratorConfig;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.OreFeatureConfig;

public class TerrainsManager {

    // ==================== ore gen
    private static ConfiguredFeature<?, ?> OREGEN_MAGICAL_CRYSTAL_CHUNK_ORE = Feature.ORE
    .configure(new OreFeatureConfig(
        OreFeatureConfig.Rules.BASE_STONE_OVERWORLD,
        BlocksManager.MAGICAL_CRYSTAL_CHUNK_ORE.getDefaultState(),
        1)) // vein size
    .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(0, 0, 12)))
    .spreadHorizontally()
    .repeat(5); // number of veins per chunk

    private static ConfiguredFeature<?, ?> OREGEN_CRYSTAL_CHUNK_ORE = Feature.ORE
    .configure(new OreFeatureConfig(
        OreFeatureConfig.Rules.BASE_STONE_OVERWORLD,
        BlocksManager.CRYSTAL_CHUNK_ORE.getDefaultState(),
        4))
    .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(0, 0, 32)))
    .spreadHorizontally()
    .repeat(12);

    private static ConfiguredFeature<?, ?> OREGEN_WHITE_IRON_CHUNK_ORE = Feature.ORE
    .configure(new OreFeatureConfig(
        OreFeatureConfig.Rules.BASE_STONE_OVERWORLD,
        BlocksManager.WHITE_IRON_CHUNK_ORE.getDefaultState(),
        4))
    .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(0, 0, 64)))
    .spreadHorizontally()
    .repeat(24);

    private static ConfiguredFeature<?, ?> OREGEN_IRON_CHUNK_ORE = Feature.ORE
    .configure(new OreFeatureConfig(
        OreFeatureConfig.Rules.BASE_STONE_OVERWORLD,
        BlocksManager.IRON_CHUNK_ORE.getDefaultState(),
        8))
    .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(0, 0, 64)))
    .spreadHorizontally()
    .repeat(24);

    private static ConfiguredFeature<?, ?> OREGEN_STARSILVER_ORE = Feature.ORE
    .configure(new OreFeatureConfig(
        OreFeatureConfig.Rules.BASE_STONE_OVERWORLD,
        BlocksManager.IRON_CHUNK_ORE.getDefaultState(),
        4))
    .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(0, 0, 64)))
    .spreadHorizontally()
    .repeat(24);

    private static void RegisterOreGeneration(String mIdentifier, ConfiguredFeature<?, ?> genStasticas) {
        RegistryKey<ConfiguredFeature<?, ?>> regKey = RegistryKey.of(Registry.CONFIGURED_FEATURE_WORLDGEN,
            new Identifier("teyvatcraft", mIdentifier));
        Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, regKey.getValue(), genStasticas);
        BiomeModifications.addFeature(BiomeSelectors.foundInOverworld(), GenerationStep.Feature.UNDERGROUND_ORES, regKey);
    }

    public static void RegisterAll() {
        // ore gen
        RegisterOreGeneration("oregen_magical_crystal_ore", OREGEN_MAGICAL_CRYSTAL_CHUNK_ORE);
        RegisterOreGeneration("oregen_crystal_ore", OREGEN_CRYSTAL_CHUNK_ORE);
        RegisterOreGeneration("oregen_white_iron_ore", OREGEN_WHITE_IRON_CHUNK_ORE);
        RegisterOreGeneration("oregen_iron_ore", OREGEN_IRON_CHUNK_ORE);
        RegisterOreGeneration("oregen_starsilver_ore", OREGEN_STARSILVER_ORE);
    }
}
