#include <GeneratorNode/GeneratorNode.hpp>
#include <WorldGenerator/FlatWorldGenerator/FlatWorldGenerator.hpp>

FlatWorldGenerator::FlatWorldGenerator(
    LocalWorld &world,
    const std::vector<LayerSpec> &layers,
    const std::string &bottomBlockName
)
{
    bottomBlockId = world.getBlockId(bottomBlockName);

    for (const LayerSpec &spec : layers)
    {
        int id = world.getBlockId(spec.blockName);
        for (uint32_t i = 0; i < spec.depth; i++)
        {
            layerBlockIds.push_back(id);
        }
    }
}

void FlatWorldGenerator::generateChunk(Chunk &chunk)
{
    if (chunk.base.z >= 0)
    {
        return;
    }

    glm::ivec3 offset;
    for (offset.z = 0; offset.z < 16; offset.z++)
    {
        int absZ = offset.z + chunk.base.z;
        size_t layerIndex = (size_t) (-absZ) - 1;
        int id = layerIndex < layerBlockIds.size() ? layerBlockIds[layerIndex] : bottomBlockId;

        for (offset.y = 0; offset.y < 16; offset.y++)
        {
            for (offset.x = 0; offset.x < 16; offset.x++)
            {
                chunk.setBlockAndMeta(offset, id, 0);
            }
        }
    }
}

GeneratorNode FlatWorldGenerator::getWeatherGenNode()
{
    return GeneratorNode(0);
}