#include <Block/SpriteBlock/BasicBlock/Crate/Crate.hpp>
#include <BlockRegistry/BlockRegistry.hpp>
#include <Controller/InventoryController/CrateController/CrateController.hpp>
#include <Milestone/CarpentryMilestone/CarpentryMilestone.hpp>
#include <World/LocalWorld/LocalWorld.hpp>

Crate::Crate(std::string name) : BasicBlock(
    name,
    "Crate",
    90.0f,
    {"crateSide", "crateSide", "crateSide", "crateSide", "crateTopBottom", "crateTopBottom"}
)
{
    withWoodSounds();
}

void Crate::getHarvestedStacks(LocalWorld &world, glm::ivec3 pos, std::vector<ItemStack> &stacks)
{
    BasicBlock::getHarvestedStacks(world, pos, stacks);
    
    std::shared_ptr<IInventory> inv = std::dynamic_pointer_cast<IInventory>(world.getMachine(pos));
    if (inv != nullptr)
    {
        int i;
        for (i=0; i<inv->getNumSlots(); i++)
        {
            ItemStack stack = inv->getStackInSlot(i);
            if (!stack.isEmpty())
            {
                stacks.push_back(stack);
            }
        }
    }
}

bool Crate::canManuallyDig()
{
    return true;
}

void Crate::onInteract(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey)
{
    player.achieve(CarpentryMilestone::ID, CarpentryMilestone::Crate);
    player.connectController<CrateController>("main:CrateController", world.getMachine(pos));
}