#include <Block/SpriteBlock/MultiBlock/MiningRig/MiningRig.hpp>
#include <Controller/InventoryController/MiningRigController/MiningRigController.hpp>
#include <Entity/PhysicsEntity/CameraEntity/Player/Player.hpp>
#include <World/LocalWorld/LocalWorld.hpp>

MiningRig::MiningRig(std::string name) : MultiBlock(name, "Mining Rig", {"default"})
{
    for (int i = 0; i < 5; i++)
    {
        structure.add(6, 0, i, "main:steelCasing");
        structure.add(0, 6, i, "main:steelCasing");
        structure.add(6, 6, i, "main:steelCasing");

        if (i != 0)
        {
            structure.add(0, 0, i, "main:steelCasing");
        }
    }

    for (int i = 1; i < 6; i++)
    {
        structure.add(i, 0, 4, "main:steelCasing");
        structure.add(0, i, 4, "main:steelCasing");
        structure.add(i, 6, 4, "main:steelCasing");
        structure.add(6, i, 4, "main:steelCasing");
    }

    withMetalSounds();
}

void MiningRig::getHarvestedStacks(LocalWorld &world, glm::ivec3 pos, std::vector<ItemStack> &stacks)
{
    MultiBlock::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);
            }
        }
    }
}

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

Sound& MiningRig::getBackgroundSound(uint8_t meta)
{
    return Sound::NONE;
}