#include <Block/SpriteBlock/Crystal/Crystal.hpp>
#include <World/World.hpp>
#include <BlockRegistry/BlockRegistry.hpp>
#include <ItemRegistry/ItemRegistry.hpp>
#include <ItemStack/ItemStack.hpp>

std::vector<Crystal::Sprite> Crystal::makeSprites(std::string textureName)
{
    return std::vector<Sprite>{
        Sprite{
            {textureName, textureName, textureName, textureName, textureName, textureName},
            {
                BoundingBox<int>{0, 1, 1, 6, 4, 4},
                BoundingBox<int>{0, 5, 3, 7, 8, 6},
                BoundingBox<int>{0, 0, 5, 8, 3, 8},
            },
            "attachedWest",
        },

        Sprite{
            {textureName, textureName, textureName, textureName, textureName, textureName},
            {
                BoundingBox<int>{2, 1, 1, 8, 4, 4},
                BoundingBox<int>{1, 5, 3, 8, 8, 6},
                BoundingBox<int>{0, 0, 5, 8, 3, 8},
            },
            "attachedEast",
        },

        Sprite{
            {textureName, textureName, textureName, textureName, textureName, textureName},
            {
                BoundingBox<int>{1, 0, 1, 4, 6, 4},
                BoundingBox<int>{5, 0, 3, 8, 7, 6},
                BoundingBox<int>{0, 0, 5, 3, 8, 8},
            },
            "attachedSouth",
        },

        Sprite{
            {textureName, textureName, textureName, textureName, textureName, textureName},
            {
                BoundingBox<int>{1, 2, 1, 4, 8, 4},
                BoundingBox<int>{5, 1, 3, 8, 8, 6},
                BoundingBox<int>{0, 0, 5, 3, 8, 8},
            },
            "attachedNorth",
        },

        Sprite{
            {textureName, textureName, textureName, textureName, textureName, textureName},
            {
                BoundingBox<int>{1, 1, 0, 4, 4, 6},
                BoundingBox<int>{3, 5, 0, 6, 8, 7},
                BoundingBox<int>{5, 0, 0, 8, 3, 8},
            },
            "attachedBottom",
        },

        Sprite{
            {textureName, textureName, textureName, textureName, textureName, textureName},
            {
                BoundingBox<int>{1, 1, 2, 4, 4, 8},
                BoundingBox<int>{3, 5, 1, 6, 8, 8},
                BoundingBox<int>{5, 0, 0, 8, 3, 8},
            },
            "attachedTop",
        },
    };
}

Crystal::Crystal(std::string name, std::string displayName, std::string textureName, glm::vec3 light, std::string drops) :
    SpriteBlock(
        name,
        displayName,
        500.0f,
        textureName,
        makeSprites(textureName)
    ),
    light(light),
    drops(drops)
{
    withGlassSounds();
}

glm::vec3 Crystal::getLightColor(uint8_t meta)
{
    return light;
}

void Crystal::getHarvestedStacks(LocalWorld &world, glm::ivec3 pos, std::vector<ItemStack> &stacks)
{
    if (drops.size() == 0)
    {
        Block::getHarvestedStacks(world, pos, stacks);
    }
    else
    {
        Item *item = itemRegistry.getItemByName(drops);
        if (item != nullptr)
        {
            stacks.push_back(ItemStack(*item, 1));
        }
        else
        {
            stacks.push_back(blockRegistry.getBlockByName(drops).toItemStack(1));
        }
    }
}