package me.km.world;

import me.km.utils.ReflectionUtils;
import net.minecraft.world.World;
import net.minecraft.world.biome.provider.BiomeProviderType;
import net.minecraft.world.biome.provider.OverworldBiomeProviderSettings;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.dimension.OverworldDimension;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.ChunkGeneratorType;
import net.minecraft.world.gen.GenerationSettings;
import net.minecraft.world.gen.OverworldChunkGenerator;
import net.minecraft.world.gen.OverworldGenSettings;
import org.apache.logging.log4j.LogManager;

public class ModOverworldDimension extends OverworldDimension {
    public ModOverworldDimension(World worldIn, DimensionType typeIn) {
        super(worldIn, typeIn);
    }

    @Override
    public ChunkGenerator<? extends GenerationSettings> createChunkGenerator() {
        LogManager.getLogger().warn("Try swapping " + world + " " + world.getDimension().getType());
        if(world.getDimension().getType().isVanilla()) {
            LogManager.getLogger().warn("no swap in vanilla world");
            return super.createChunkGenerator();
        }

        ModWorldInfo info = new ModWorldInfo(world.getWorldInfo(), WorldManager.getName(world), world.getServer());
        ReflectionUtils.setWorldInfo(world, info);

        ModWorldInfo.Type type = info.getType();
        if(type != ModWorldInfo.Type.VOID && type != ModWorldInfo.Type.VANILLA) {
            ChunkGeneratorType<OverworldGenSettings, OverworldChunkGenerator> chunkGenType = ChunkGeneratorType.SURFACE;
            BiomeProviderType<OverworldBiomeProviderSettings, ModBiomeProvider> provider = info.getType().getProvider();
            OverworldGenSettings genSettings = chunkGenType.createSettings();
            OverworldBiomeProviderSettings providerSettings = provider.func_226840_a_(this.world.getWorldInfo()).setGeneratorSettings(genSettings);
            return chunkGenType.create(this.world, provider.create(providerSettings), genSettings);
        }

        return super.createChunkGenerator();
    }

    @Override
    public boolean isDaytime() {
        return getWorld().getSkylightSubtracted() < 4;
    }
}
