#include <Item/StatelessItem/BasicItem/Rake/Rake.hpp>
#include <World/LocalWorld/LocalWorld.hpp>
#include <Block/Block.hpp>

Rake::Rake(std::string name) : BasicItem(name, "Rake", Material::rake)
{
    setMaxStackSize(1);
}

void Rake::onUse(LocalWorld &world, IWorldActor &actor, ItemStack &stack, glm::ivec3 pos, Side side)
{
    // Only allow raking the top side.
    if (side != Side::Top)
    {
        return;
    }

    // Only allow raking if there's nothing above the block.
    Block *blockAbove = world.getBlockAt(pos + glm::ivec3(0, 0, 1));
    if (blockAbove == nullptr || blockAbove->name != "main:air")
    {
        return;
    }

    // Figure out the amount of nutrition.
    uint8_t nutrition;
    Block *block = world.getBlockAt(pos);
    if (block->name == "main:dirt")
    {
        nutrition = 1;
    }
    else if (block->name == "main:grass")
    {
        nutrition = 2;
    }
    else
    {
        // Not a block we can rake.
        return;
    }

    // Figure out the metadata.
    uint8_t meta = nutrition | (actor.getSideFacing2D().axis() << 4);

    // Change it to the farmland.
    world.setBlockAt(pos, "main:farmland", meta);
}