#ifndef BELT_MOTOR_HPP_
#define BELT_MOTOR_HPP_

#include <Block/SpriteBlock/SpriteBlock.hpp>
#include <Block/MachineBlockMixin.hpp>
#include <Machine/MBeltMotor/MBeltMotor.hpp>

class BeltMotor :
    public SpriteBlock,
    public MachineBlockMixin<MBeltMotor>
{
private:
    static const std::vector<std::string>& getStateNames();

    void onInteractWithRollers(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey);
    void onInteractWithBelt(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey);

public:
    /*

        #            Belt Motor Meta Byte
        +-------+---+---+---+---+---+---+---+---+
        | Bit   | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
        +-------+---+---+---+---+---+---+---+---+
        | Field |   |   |   | A | frame | orient|
        +-------+---+---+---+---+---+---+---+---+

        orient
            The side towards which the conveyor belt points.
        
        frame
            The frame offset to apply to the animation.
        
        A
            If set, the motor is active.
    */

    static constexpr uint8_t metaActive = 1 << 4;
    static constexpr uint8_t numStates = 1 << 5;

    static constexpr Side getMetaOrientation(uint8_t meta)
    {
        return Side(meta & 3);
    }

    static constexpr int getMetaFrameOffset(uint8_t meta)
    {
        return (meta >> 2) & 3;
    }

    BeltMotor(std::string name);

    virtual bool canManuallyDig() override;
    virtual bool canPlace(LocalWorld &world, IWorldActor &actor, glm::ivec3 pos, Side side, uint8_t &meta) override;
    virtual Sound& getBackgroundSound(uint8_t meta) override;
    virtual void onWalkingOver(World &world, glm::ivec3 pos, PhysicsEntity &entity) override;
    virtual void onInteract(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey) override;
};

#endif