#include <Block/SpriteBlock/ItemDeployer/ItemDeployer.hpp>
#include <World/LocalWorld/LocalWorld.hpp>

const std::vector<std::string>& ItemDeployer::getStateNames()
{
    static std::vector<std::string> stateNames;

    if (stateNames.empty())
    {
        for (uint8_t meta = 0; meta < numStates; meta++)
        {
            const char *signFlag = (meta & metaUseSign) ? "Pos" : "Neg";
            const char *activeFlag = (meta & metaActive) ? "active" : "inactive";
            Side orientation = getMetaOrientation(meta);
            int frameOffset = getMetaFrameOffset(meta);

            stateNames.push_back(formatString("$+$+$+$+", activeFlag, orientation.name(), frameOffset, signFlag));
        }
    }

    return stateNames;
}

ItemDeployer::ItemDeployer(std::string name) :
    SpriteBlock(
        name,
        "Item Deployer",
        90.0f,
        getStateNames()
    )
{
    withMetalSounds();
}

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

void ItemDeployer::onInteract(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey)
{
    if (player.isHoldingScrewdriver())
    {
        world.setBlockMeta(pos, *world.getBlockMeta(pos) ^ metaUseSign, true);
        return;
    }

    SpriteBlock::onInteract(world, player, pos, side, collisionKey);
}

bool ItemDeployer::canPlace(LocalWorld &world, IWorldActor &actor, glm::ivec3 pos, Side side, uint8_t &meta)
{
    meta = actor.getSideFacing2D().asNumber();
    return true;
}