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

Substation::Substation(std::string name) : MultiBlock(name, "Substation", {"lvl0", "lvl1", "lvl2", "lvl3"})
{
    for (int z=-2; z<=2; z++)
    {
        for (int v=0; v<=2; v++)
        {
            for (int u=-1; u<=1; u++)
            {
                if (u == 0 && v == 1 && z != -2 && z != 2)
                {
                    continue;
                }

                if (u == 0 && v == 0 && z == 0)
                {
                    continue;
                }

                structure.add(u, v, z, "main:steelCasing");
            }
        }
    }

    withMetalSounds();
}

void Substation::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 Substation::onInteract(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey)
{
    player.connectController<SubstationController>("main:SubstationController", world.getMachine(pos));
}