#include <Block/SpriteBlock/Pump/Pump.hpp>
#include <Controller/InventoryController/PumpController/PumpController.hpp>
#include <World/LocalWorld/LocalWorld.hpp>

std::vector<Pump::Sprite> Pump::makeSprites()
{
    std::vector<Sprite> sprites;
    for (uint8_t meta = 0; meta < 6; ++meta)
    {
        Side inputSide((meta + 4) % 6);
        Side outputSide = inputSide.opposite();
        Side electricInputSide = inputSide.isZ() ? Side::North : Side::Bottom;

        Sprite sprite = {
            {
                "pump/side",
                "pump/side",
                "pump/side",
                "pump/side",
                "pump/side",
                "pump/side"
            },
            {{0, 0, 0, 8, 8, 8}},
            formatString("input$+", inputSide),
        };

        sprite.texturesOnSides[outputSide.asNumber()] = "pump/output";
        sprite.texturesOnSides[inputSide.asNumber()] = "pump/input";
        sprite.texturesOnSides[electricInputSide.asNumber()] = "pump/electricInput";
        sprites.push_back(sprite);
    }

    return sprites;
}

Pump::Pump(std::string name) :
    SpriteBlock(
        name,
        "Pump",
        90.0f,
        "pump/side",
        makeSprites()
    )
{
    withMetalSounds();
}

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

void Pump::onInteract(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey)
{
    if (player.isHoldingScrewdriver())
    {
        world.setBlockMeta(pos, (*world.getBlockMeta(pos) + 1) % 6, true);
        return;
    }

    player.connectController<PumpController>(
        "main:PumpController",
        world.getMachine(pos)
    );
}