#include <Block/SpriteBlock/Stairs/Stairs.hpp>
#include <Entity/PhysicsEntity/CameraEntity/Player/Player.hpp>
#include <World/LocalWorld/LocalWorld.hpp>

Stairs::Stairs(std::string name, SpriteBlock &base) :
    SpriteBlock(
        name,
        base.displayName + " Stairs",
        base.hardness,
        {"entryWest", "entryEast", "entrySouth", "entryNorth"}
    ),
    base(base)
{}

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

void Stairs::onInteract(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey)
{
    if (player.isHoldingScrewdriver())
    {
        uint8_t meta = *world.getBlockMeta(pos);
        world.setBlockMeta(pos, (meta + 1) & 3, true);
    }
    else
    {
        SpriteBlock::onInteract(world, player, pos, side, collisionKey);
    }
}

void Stairs::onDesignerInitializing(BlockSpriteDesigner &designer, ScriptEngine &engine)
{
    withSoundsFrom(base);

    engine.push(designer.addSprite(std::make_shared<VoxelSprite>(base.generatedSprites[0]->frames[0].colorMap)));
    engine.setGlobal("STAIRS_BASE_COLORMAP");
    engine.push(designer.addSprite(std::make_shared<VoxelSprite>(base.generatedSprites[0]->frames[0].biasMap)));
    engine.setGlobal("STAIRS_BASE_BIASMAP");
    engine.push(designer.addSprite(std::make_shared<VoxelSprite>(base.generatedSprites[0]->frames[0].refractMap)));
    engine.setGlobal("STAIRS_BASE_REFRACTMAP");
    engine.push(designer.addSprite(std::make_shared<VoxelSprite>(base.generatedSprites[0]->frames[0].lightSpecMap)));
    engine.setGlobal("STAIRS_BASE_LIGHTSPECMAP");
    engine.push(designer.addSprite(std::make_shared<VoxelSprite>(base.generatedSprites[0]->collisionMask)));
    engine.setGlobal("STAIRS_BASE_COLLISION_MASK");

    designer.useFallbackScript("stairs.lua");
}