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

std::vector<Mushroom::Sprite> Mushroom::makeSprites(std::string texSetName)
{
    std::string topTex = formatString("mushrooms/$+/top", texSetName);
    std::string profileTex = formatString("mushrooms/$+/profile", texSetName);
    std::string bottomTex = formatString("mushrooms/$+/bottom", texSetName);
    std::string iconTex = formatString("mushrooms/$+/icon", texSetName);

    return std::vector<Sprite>{
        Sprite{
            {iconTex, iconTex, iconTex, iconTex, bottomTex, topTex},
            {
                {0, 0, 3, 8, 8, 7},
                {3, 3, 0, 5, 5, 3},
            },
            "upright",
        },
    };
}

Mushroom::Mushroom(std::string name, std::string displayName, std::string texSetName) :
    SpriteBlock(
        name,
        displayName,
        1.0f,
        formatString("mushrooms/$+/icon", texSetName),
        makeSprites(texSetName)
    )
{}

bool Mushroom::canPlace(LocalWorld &world, IWorldActor &actor, glm::ivec3 pos, Side side, uint8_t &meta)
{
    Block *blockUnder = world.getBlockAt(pos + glm::ivec3(0, 0, -1));
    if (blockUnder == nullptr)
    {
        return false;
    }

    return blockUnder->name == "main:dirt" || blockUnder->name == "main:grass";
}

bool Mushroom::isIntersectedBy(World &world, glm::ivec3 pos, const BoundingBox<wfixed> &box)
{
    return false;
}

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

void Mushroom::onNeighborUpdated(LocalWorld &world, glm::ivec3 pos, Side side)
{
    // If the block below us changed, break.
    if (side == Side::Bottom)
    {
        world.breakBlock(pos);
    }
}

bool Mushroom::canSweepAway()
{
    return true;
}